洛谷题目链接:[POI2007]TET-Tetris Attack

题目描述

A puzzle called "Tetris Attack" has lately become a very popular game in Byteotia. The game itself is highlysophisticated, so we shall only introduce its simplified rules: the player is given a stack of \(2n\) elements, placedone on another and marked with \(n\) different symbols. Exactly two elements of the stack are marked with eachsymbol. A player's move consists in swapping two neighbouring elements, ie. interchanging them. If, as aresult of the swap, there are some two neighbouring elements marked with the same symbol, they are bothremoved from the stack. Then all the elements that have been above them fall down in consequence and mayvery well cause another removals. The player's goal is emptying the stack in the least possible number ofmoves.

TaskWrite a programme that:

reads the description of the initial stack content from the standard input, finds a solution with the minimum number of moves possible, writes out the outcome to the standard output.

给定一个长度为2n的序列,1~n各出现两次,可以交换相邻两项,两个同样的数放在一起会对消,求把所有数对消的最小交换次数。

温馨提示,输出方案。

输入输出格式

输入格式:

In the first line of the standard input there is one integer \(n\), \(1\le n\le 50\ 000\).

The following \(2n\) lines describe theinitial content of the stack. The \((i+1)\)'th line contains one integer \(a_i\) -the symbol which the \(i\)'th (\(1\le a_i\le n\))element from the bottom is marked with. Each symbol appears in the stack exactly twice. Moreover, notwo identical symbols neighbour initially. The test data is well chosen so that a solution with no more than \(1\ 000\ 000\) moves exists.

输出格式:

A solution with the minimum number of moves possible should be written out to the standard output asfollows. The first line should contain one integer \(m\) -the minimum number of moves. The following \(m\) should describe the optimal solution itself, i.e. a sequence of \(m\) integers \(p_1,\cdots,p_m\)​), one in each line. \(p_i\) denotes that in \(i\)'th move the player has chosen to swap the \(p_i\)'th and \((p_i+1)\)'th elements from the bottom.

If more than one optimal solution exists, your programme should write out any one of them.

输入输出样例

输入样例#1:

5

5

2

3

1

4

1

4

3

5

2

输出样例#1:

2

5

2

说明

SPJ返回WA:Something Left为方案错误

温馨提示,输出方案。


一句话题意: 给定一个长度为\(2n\)的序列,\(1\)~\(n\)各出现两次,可以交换相邻两项,两个同样的数放在一起会对消,求把所有数对消的最小交换次数.


题解: 先直接讲做法吧:

直接从\(1\)到\(2n\)读入每个值,当这个值第一次出现就向树状数组这个位置+1,并记录下这个值第一次出现的位置. 当这个值第二次出现的时候计算这个值与它上一次出现的位置之间加入进的值,并把上一次修改的位置-1.

那么这个方法为什么是正确的呢? (其实我自己做这道题的时候也是大力猜的结论233)

我们可以简单证明一下(可能有点伪证的成分,感性理解一下):

如果我们要使两个相同的数字合并,那么这两个数字中间的数字一定要合并.

所以这样在我们第二遍扫到一个数字的时候,要合并这个数字,就至少要将这个数字之间的数字都合并掉.

然而在树状数组中这个区间内只会有只出现过一次的数字,因为如果有还没计算的数字,就会在前面的循环中扫到.

这样的话为了让这个数字能合并,至少需要让它与之前每一个数字交换一次,不然一定会有数字夹在这两个数字中间导致不能消掉这个数字.

所以这样算出来的答案是可以保证最优的

然后在输出方案的时候就每次算出距离就可以了,可以自己想一下怎么模拟算位置.

#include<bits/stdc++.h>
using namespace std;
const int N=50000+5; int n, c[N*2], ans = 0, pos[N], stk[N*100], top = 0, res, size = 0, p; int lowbit(int x){ return -x & x; }
void update(int x,int val){ for(;x<=n*2;x+=lowbit(x)) c[x] += val; } int query(int x){
int res;
for(res = 0;x;x-=lowbit(x)) res += c[x];
return res;
} int main(){
int x; scanf("%d", &n);
for(int i=1;i<=n*2;i++){
scanf("%d", &x);
if(pos[x]){
update(pos[x], -1), res = query(i)-query(pos[x]-1);
ans += res, p = i;
while(res > 0) stk[++top] = p-size-1, p--, res--;
size += 2;
}
else pos[x] = i, update(i, 1);
}
printf("%d\n", ans);
for(int i=1;i<=top;i++) printf("%d\n", stk[i]);
return 0;
}

[洛谷P3460] [POI2007]TET-Tetris Attack的更多相关文章

  1. [洛谷3457][POI2007]POW-The Flood

    洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...

  2. 洛谷P3459 [POI2007]MEG-Megalopolis(树链剖分,Splay)

    洛谷题目传送门 正解是树状数组维护dfn序上的前缀和,这样的思路真是又玄学又令我惊叹( 我太弱啦,根本想不到)Orz各路Dalao 今天考了这道题,数据范围还比洛谷的小,只有\(10^5\)(害我复制 ...

  3. 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]

    题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...

  4. 洛谷 P3462 [POI2007]ODW-Weights

    题面: https://www.luogu.org/problemnew/show/P3462 https://www.lydsy.com/JudgeOnline/problem.php?id=111 ...

  5. 洛谷 P3456 [POI2007]GRZ-Ridges and Valleys

    P3456 [POI2007]GRZ-Ridges and Valleys 题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两 ...

  6. 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)

    题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...

  7. 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]

    [POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...

  8. 【刷题】洛谷 P3455 [POI2007]ZAP-Queries

    题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...

  9. 洛谷P3457 [POI2007]POW-The Flood [并查集,模拟]

    题目传送门 pow 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是 ...

随机推荐

  1. 推荐形参使用常量引用:void func(const T &);

    一.声明为const的原因: 把函数不会改变的形参定义成普通的引用会带给函数的调用者一种误导,即函数可以修改它的实参的值: 限制函数所能接受的实参类型,如不能把const对象.字面值或者需要类型转换的 ...

  2. lintcode-18-带重复元素的子集

    带重复元素的子集 给定一个可能具有重复数字的列表,返回其所有可能的子集 注意事项 子集中的每个元素都是非降序的 两个子集间的顺序是无关紧要的 解集中不能包含重复子集 样例 如果 S = [1,2,2] ...

  3. asp.net中Repeater结合js实现checkbox的全选/全不选

    前台界面代码: <input name="CheckAll" type="checkbox" id="CheckAll" value= ...

  4. eclipse 创建并运行maven web项目

    这两天想在eclipse上运行maven web项目,折腾了许久,总算success啦. 1,利用eclipse创建dynamic web project(eclipse需要安装m2eclipse). ...

  5. JDK版本Java SE、Java EE、Java ME的区别

    想在win7 X64上搭建JAVA开发环境来着(只是尝试下),打开JAVA 官网下载JDK,发现好多版本懵了,百度了下找到这些版本的区别,故有了下文 1.JAVA SE Java2平台标准版(Java ...

  6. stm32f407启动文件分析

    ; Amount of memory (in bytes) allocated for Stack; Tailor this value to your application needs; < ...

  7. BZOJ 2326 数学作业(分段矩阵快速幂)

    实际上,对于位数相同的连续段,可以用矩阵快速幂求出最后的ans,那么题目中一共只有18个连续段. 分段矩阵快速幂即可. #include<cstdio> #include<iostr ...

  8. BZOJ 1050 旅行(并查集)

    很好的一道题.. 首先把边权排序.然后枚举最小的边,再依次添加不小于该边的边,直到s和t联通.用并查集维护即可. # include <cstdio> # include <cstr ...

  9. 【题解】CF#833 B-The Bakery

    一个非常明显的 \(nk\) dp 状态 \(f[i][k]\) 表示以 \(i\) 为第 \(k\) 段的最后一个元素时所能获得的最大代价.转移的时候枚举上一段的最后一个元素 \(j\)更新状态即可 ...

  10. 【题解】Atcoder ARC#76 F-Exhausted?

    第一次用霍尔定理做题..简单的来说,就是判断一张二分图上是否存在完美匹配,只需要证明对于 \(a\) 集合中的任意 \(k\) 个点来说,都与 \(b\) 集合中的 \(k\) 个点有边相连.如果不满 ...