例题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 ...
随机推荐
- windows下 定时删除tomcat日志和缓存。可以保留天数
forfiles /p "e:\Program Files\Tomcat 7.0\logs" /s /m *.log /d -5 /c "cmd /c del @path ...
- Saving HDU
Problem Description 话说上回讲到海东集团面临内外交困,公司的元老也只剩下XHD夫妇二人了.显然,作为多年拼搏的商人,XHD不会坐以待毙的. 一天,当他正在苦思冥想解困良策的时候, ...
- JD轮播图代码
<!DOCTYPE html> <html> <head> <title>jd网站的轮播图效果</title> <me ...
- 【Java的JNI快速学习教程】
1. JNI简介 JNI是Java Native Interface的英文缩写,意为Java本地接口. 问题来源:由于Java编写底层的应用较难实现,在一些实时性要求非常高的部分Java较难胜任(实时 ...
- C如何使用内存
栈: 自动变量:auto.变量的地址在栈中. C语言函数调用的实现: 在调用方,参数从后往前按顺序被堆积在栈中 和函数调用关联的返回信息(返回地址等)也被堆积在栈中. 一旦函数调用结束,局部变 ...
- ios优化复制大文件时,如何使内存运用最少且效率最高
我也是纠结了好几天,我想自己想个办法,但是数据复制不上去,我现在还不明白,如果有人知道我错在哪了,请留言,如果还有更好的方法,请分享共同进步. ____________________________ ...
- 【AR】Vuforia App key is missing.Please get a valid key
在跑Vuforia 的sample android app 的时候报了下面这个错,找了半天才找到解决方法: "Vuforia App key is missing. Please get a ...
- poj 1141 动态规划进行括号匹配
思路:黑书的例题 #include<iostream> #include<cstring> #include<cstdio> #include<algorit ...
- ASP三种常用传值方式:
ASP 页面(两个aspx页面)传值方式:背景: 两个aspx 页面valuepage.aspx tbusername tbpwdobtainvalue.aspx tbusername tbpwd 1 ...
- ruby学习--block
#当前块 class Block def a_method return yield if block_given? 'no block' end end obj=Block.new puts &qu ...