例题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 ...
随机推荐
- 小白日记47:kali渗透测试之Web渗透-XSS(一)
XSS [推荐书籍:XSS跨站脚本攻击剖析与防御] xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/ ...
- C#.Net中的转义字符(转)
当声明一个字符串变量时有一些字符是不能以平常的方式包含在变量中的.为了解决这个问题,C#提供了两种不同的方法. 第一种方法是使用’转义序列’.例如,我们想得到如下的字符串 “Hello World H ...
- LOG4NET开源日志dll引用流程,在net3.5中已经实践ok
一,在app.config中配置 <?xml version="1.0"?><configuration> <configSections> & ...
- String、Brush、Color 相互转换
1.String转换成Color Color color = (Color)ColorConverter.ConvertFromString(string); 2.String转换成Brush Bru ...
- 通过cagradientLayer类封装uiimageview动画色度差
#import <UIKit/UIKit.h> typedef NS_ENUM(NSInteger, EcolorDirectionType) { EcolorDirectionUp, / ...
- VMware系统运维(八)vCenter Server安装
1.终于开始安装vCenter Server了,需要配置数据源哦! 2.下一步 3.接受协议,下一步 4.输入许可密钥,也可以后面再输入,下一步 5.选择数据源,即我们前面配置的系统DSN,下一步 6 ...
- Shodan!
Shodan! 简介 首先先介绍一下Shodan CNNMoney的一篇文章写道,虽然目前人们都认为谷歌是最强劲的搜索引擎,但Shodan才是互联网上最可怕的搜索引擎. 与谷歌不同的是,Shodan不 ...
- linux yum install resource - epel
首先现在如下rpm包,然后安装对应的rpm包centos5 32位epel源下载地址: www.lishiming.net/data/attachment/forum/epel-release-5-4 ...
- 关于app上页面和js的调试
不久前折腾了一晚上关于app上的页面和js的调试: 首先得准备几个比较比较常用的工具: 1.chrome(这个都没有你还干啥fe呀) 2.Fiddler(抓包神器,基本也是必备咯) 3.sublime ...
- How to Use Rsync to Sync New or Changed/Modified Files in Linux
As a system administrator or Linux power user, you may have probably come across or even on several ...