例题6-5 Boxes in a line uVa12657
这道题目的解决方案是双向链表,数据结构本身并不复杂,但对于四种情况的处理不够细致,主要体现在以下几点:
- 分类讨论不全面,没有考虑特殊情况(本身不需要操作,需要互换的两元素相邻)
- 没有考虑状态4改变后对其他操作的影响
- 没有灵活运用数学知识(求偶只需要全部减去奇数即可)
以下贴出AC代码
#include <cstdio>
#include <algorithm>
const int maxn = 100000 + 10;
int left[maxn];
int right[maxn];
int s[maxn];
using namespace std;
void link(int x,int y){
right[x] = y;
left[y] = x;
}
int main(){
#ifdef DEBUG
freopen("6.5.in","r",stdin);
#endif
int n, m ,num = 0;
while(scanf("%d %d", &n, &m)==2){
for(int i = 1; i <= n; i++){
right[i]= (i+1);
left[i]=i-1;
}
right[0]=1;
left[0]=n;
int op,X,Y;
int inv = 0;
while(m--){
scanf("%d",&op);
if(op == 4) inv=!inv;
else {
scanf("%d%d",&X, &Y);
if(op == 3 && right[Y] == X) swap(X,Y);
if(op != 3 && inv) op = 3 - op;
if(op == 1 && X == left[Y]) continue;
if(op == 2 && X == right[Y]) continue;int LX= left[X],RX = right[X],LY = left[Y], RY = right[Y];
if(op == 1){
link(LX,RX);link(LY,X);link(X,Y);
}
else if(op == 2){
link(LX,RX);link(Y,X);link(X,RY);
}
else if(op == 3){
if(right[X] == Y){
link(LX, Y);link(Y, X); link(X, RY);
}
else{
link(LX, Y);link(Y, RX); link(LY, X);link(X,RY);
}
}
}
}
long long ans = 0;
int j=0;
for(int i = 1;i<=n; i++){
j=right[j];
if(i % 2 == 1) ans+=j;
}
if(inv && n %2 == 0) ans =(long long )n * (n+1) /2 -ans;
printf("Case %d: %lld\n", ++num, ans);
}
return 0;
}
例题6-5 Boxes in a line uVa12657的更多相关文章
- uva-12657 - Boxes in a Line(双向链表)
12657 - Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to righ ...
- Problem B Boxes in a Line
省赛B题....手写链表..其实很简单的.... 比赛时太急了,各种手残....没搞出来....要不然就有金了...注:对相邻的元素需要特判..... Problem B Boxes in a Li ...
- Boxes in a Line
Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...
- Boxes in a Line(移动盒子)
You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to sim ...
- C - Boxes in a Line 数组模拟链表
You have n boxes in a line on the table numbered 1 . . . n from left to right. Your task is to simul ...
- UVa 12657 Boxes in a Line(应用双链表)
Boxes in a Line You have n boxes in a line on the table numbered 1 . . . n from left to right. Your ...
- Boxes in a Line UVA - 12657
You have n boxes in a line on the table numbered 1...n from left to right. Your task is to simulat ...
- UVA12657 Boxes in a Line:题解
题目链接:https://www.luogu.org/problemnew/show/UVA12657 分析: 此题使用手写链表+模拟即可.(其实可以用list,而且更简便,但是会大大的超时) 肯定是 ...
- UVa12657 - Boxes in a Line(数组模拟链表)
题目大意 你有一行盒子,从左到右依次编号为1, 2, 3,…, n.你可以执行四种指令: 1 X Y表示把盒子X移动到盒子Y左边(如果X已经在Y的左边则忽略此指令).2 X Y表示把盒子X移动到盒子Y ...
随机推荐
- C#中的多态现象
面向对象的特征封装.继承和多态.Polymorphism(多态性)来源于希腊单词,指“多种形态”.多态性的一个重要特征是方法的调用是在运行时确定而不是编译时.在.NET中用于实现多态性的关键词有vir ...
- C++ atol
函数名: atol 功 能: 把字符串转换成长整型数 用 法: long atol(const char *nptr); 简介编辑 相关函数: atof,atoi,strtod,strtol,st ...
- tcpdump常用命令
1. 只抓syn包 tcpdump -i eth1 'tcp[tcpflags] = tcp-syn' 2. 只抓ack包 tcpdump -nni xgbe1 dst host 191.168.10 ...
- property在括号中应该怎样写
property 属性的定义,类似于get set assign: 简单的赋值,不更改索引计数 使用assign是针对基础的数据类型,比如NSinterger,CGFloat和c数据类型(int fl ...
- Java学习笔记——字符串常用函数
class JavaTest4_String { public static void main(String[] args) { String str1 = "IOS,ANDROID,BB ...
- 基本API-StdIn.java
/************************************************************************* * Compilation: javac StdI ...
- eclipse启动问题
今天在公司正上班,突然跳出来一个windows update补丁更新,然后就确认呗,结果更新完成之后, eclipse打不开了,启动报错: could not find the main class, ...
- Python图片处理PIL/pillow/生成验证码/出现KeyError: 和The _imagingft C module is not installed
最近在用Python开发自己的博客,需要用到Python生成验证码,当然肯定要用到Python的图形处理库PIL,因为我用的是windows. 所以在安装好pil之后就开始写,就按照题目所说出现了Th ...
- Linux配置防火墙8080端口
1.查看防火墙状态,哪些端口开放了 /etc/init.d/iptables status 2.配置防火墙 vi /etc/sysconfig/iptables ################### ...
- C#之使用随机数
1.C#自带随机数函数 using System; System.Random ran = new System.Random();int n = ran.Next(100, 1000);//产生10 ...