【例题 6-5 UVA 12657 】Boxes in a Line
in UVa line 例题
2023-09-14 09:03:45 时间
【链接】 我是链接,点我呀:)
【题意】
【题解】
双向链表模拟题。 移动的时候,要注意它就在所需要的位置的情况。那种情况不移动。 (如果已经在所需位置了,还用链表的插入方式强行移动的话,会WA到死..)【代码】
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e5;
int n, m,flag;
pair <int, int> v[N+10];
void cr(int x0,int y0,int z0,int x,int z) //x0,y0,z0 x_z 把y0插入到_位置
{
v[x0].second = z0;v[z0].first = x0;
v[x].second = y0; v[z].first = y0;
v[y0].first = x, v[y0].second = z;
}
int main()
{
/*freopen("F:\\rush.txt", "r", stdin);
freopen("F:\\rush_out.txt", "w", stdout);*/
int kase = 0;
while (~scanf("%d%d", &n, &m))
{
v[0].second = 1;
for (int i = 1; i <= n; i++)
v[i].first = i - 1, v[i].second = i + 1;
v[n + 1].first = n;
flag = 0;
printf("Case %d: ", ++kase);
for (int i = 1; i <= m; i++)
{
int ope,x,y;
scanf("%d", &ope);
if (flag && ope <= 2) ope = 3 - ope;
switch (ope)
{
case 4:
{
flag = !flag;
break;
}
case 1:
{
scanf("%d%d", &x,&y);//x移动到y的左边
if (x == v[y].first) break;
cr(v[x].first, x, v[x].second, v[y].first, y);
break;
}
case 2:
{
scanf("%d%d", &x, &y);//x移动到y的右边
if (x == v[y].second) break;
cr(v[x].first, x, v[x].second, y, v[y].second);
break;
}
case 3:
{
scanf("%d%d", &x, &y);
int temp;
if (v[y].second != x)//y_x || xy
{
//记录x的左边是什么temp
//把x插入到y的右边
//在把y插入到temp的右边
temp = v[x].first;
cr(v[x].first, x, v[x].second, y, v[y].second);
if (y!=v[temp].second)cr(v[y].first, y, v[y].second, temp, v[temp].second);
}
else //yx
{
//把y插入到x的右边
temp = v[x].second;
cr(v[y].first, y, v[y].second, x, v[x].second);
}
break;
}
default:
break;
}
}
ll ans = 0, cnt = 0;
if (!flag)
for (int i = v[0].second;i != n + 1; i = v[i].second)
{
cnt++;
if (cnt & 1) ans += i;
}
else
for (int i = v[n+1].first; i != 0; i = v[i].first)
{
cnt++;
if (cnt & 1) ans += i;
}
printf("%lld\n", ans);
}
return 0;
}
相关文章
- 项目半夜凌晨宕机,报错Invalid character found in method name. HTTP method names must be tokens|the HTTP protoco
- 徐大大seo:All-in-One WP Migration
- ORA-24058: cannot downgrade QUEUE_TABLE that has propagation in a prepared state ORACLE 报错 故障修复 远程处理
- ORA-24400: error occured while creating connections in the pool ORACLE 报错 故障修复 远程处理
- ORA-39918: PLSQL Functional Index string.string in tablespace string not allowed in transportable set. ORACLE 报错 故障修复 远程处理
- ORA-44908: found a loop in XInclude elements while expanding string ORACLE 报错 故障修复 远程处理
- ORA-01205: not a data file – type number in header is string ORACLE 报错 故障修复 远程处理
- MySQL Error number: MY-010498; Symbol: ER_NDB_ERROR_IN_GET_AUTO_INCREMENT; SQLSTATE: HY000 报错 故障修复 远程处理
- ORA-13276: internal error [string] in coordinate transformation ORACLE 报错 故障修复 远程处理
- ORA-14296: Table block size mismatch in ALTER TABLE EXCHANGE [SUB]PARTITION ORACLE 报错 故障修复 远程处理
- ORA-15156: cluster in rolling upgrade from version [string] to [string] ORACLE 报错 故障修复 远程处理
- in Neo4j查询:使用Not In操作(neo4j查询not)
- ZCRM_DAY_IN_WEEK详解编程语言
- 和in的区别深入研究MySQL查询条件中的=与IN的区别(mysql查询条件中)
- 条件不将Oracle中的空值纳入IN条件中(oracle空值in)
- ?MYSQL中 IN 运算符的含义(mysqlin什么意思)
- Batch Renaming in Linux Environment(批量重命名linux)
- 和existsMySQL首选:少用IN和EXISTS方法(mysql少用in)
- 数为多少? Exploring The Limits Of Thread Count In Linux(linux最大线程)
- 探索Oracle中IN关联查询的精彩世界(oracle关联查询in)
- MySQL中IN运算符的使用技巧(mysql中 与in)
- MySQL使用IN查询时数量超限问题的解决方法(mysql中in超过数量)
- MySQL中使用IN类型参数(mysql中in类型参数)
- MySQL中IN语句的限制详解(mysql中in的限制)
- MySQL中IN的用法和意义(mysql中in的意思)
- 深入分析MySQL中IN语句的性能问题(mysql中in性能分析)
- MySQL中的IN查询的性能优化(mysql中in性能)
- 解决MySQL中IN不走索引问题的方法(mysql中in不走索引)
- Oracle的IN走索引技术(in走索引 oracle)
- 使用Oracle中的IN条件实现复杂查询(oracle中in条件)
- 查询Oracle中使用IN关键字查询多个值的方法(oracle中in多个值)