HIT 2430 Counting the algorithms (树状数组)
来源:互联网 发布:python range to list 编辑:程序博客网 时间:2024/05/24 11:14
Counting the algorithms
Submitted : 788, Accepted : 312
As most of the ACMers, wy's next target is algorithms, too. wy is clever, so he can learn most of the algorithms quickly. After a short time, he has learned a lot. One day, mostleg asked him that how many he had learned. That was really a hard problem, so wy wanted to change to count other things to distract mostleg's attention. The following problem will tell you what wy counted.
Given 2N integers in a line, in which each integer in the range from 1 to N will appear exactly twice. You job is to choose one integer each time and erase the two of its appearances and get a mark calculated by the differece of there position. For example, if the first 3 is in position 86 and the second 3 is in position 88, you can get 2 marks if you choose to erase 3 at this time. You should notice that after one turn of erasing, integers' positions may change, that is, vacant positions (without integer) in front of non-vacant positions is not allowed.
Input
There are multiply test cases. Each test case contains two lines.
The first line: one integer N(1 <= N <= 100000).
The second line: 2N integers. You can assume that each integer in [1,N] will appear just twice.
Output
One line for each test case, the maximum mark you can get.
Sample Input
31 2 3 1 2 331 2 3 3 2 1
Sample Output
69
Hint
We can explain the second sample as this. First, erase 1, you get 6-1=5 marks. Then erase 2, you get 4-1=3 marks. You may notice that in the beginning, the two 2s are at positions 2 and 5, but at this time, they are at positions 1 and 4. At last erase 3, you get 2-1=1marks. Therefore, in total you get 5+3+1=9 and that is the best strategy.
题目大意:
可以直接看样例和HINT, 就是从一个队列中拿走相同的,然后位置差作为得分,使得分尽量要高。
分析:
很有意思的小题目。考虑三种情况 AABB ABAB ABBA 可知第一种情况我随意拿都可以, 第二种情况不管怎么拿都会让之后的操作所得数字-1, 第三种情况我应该先拿外面,里面相对位置不变。
然后树状数组中记录下所有的数字,第一次出现就标记为1 第二次出现标记为-1。 从前往后遍历, 求得第i个的配对元素j之后还有多少个-1即可,就是ABAB情况的对数。
#include<cstdio>#include <cmath>#include <vector>#include <complex>#include<cstdlib>#include<string.h>#include <iostream>#include <algorithm>#define MAX 200100struct node{int f, s;}st[MAX];int arr[MAX];long long int m_arr[MAX];bool oc[MAX];int lowbit(int n){return n & (-n);}void update(int p, int n){while(p < MAX){m_arr[p] += n;p += lowbit(p);}}long long int get(int p){long long int res = 0;while(p > 0){res += m_arr[p];p -= lowbit(p);}return res;}int main(){int num, i, tmp;long long int res;while(~scanf("%d", &num)){res = 0;memset(oc, 0, sizeof(oc));num *= 2;for(i = 1; i <= num; i++){scanf("%d", &arr[i]);if(oc[arr[i]]){res += i - st[arr[i]].f;update(i, -1);st[arr[i]].s = i;}else{update(i, 1);oc[arr[i]] = 1;st[arr[i]].f = i;}}memset(oc, 0 ,sizeof(oc));for(i = 1; i <= num; i++){if(oc[arr[i]]) continue;oc[arr[i]] = 1;tmp = - get(num) + get(st[arr[i]].s);update(st[arr[i]].s, 1);update(st[arr[i]].f, -1);res -= tmp;}printf("%lld\n", res);}return 0;}
- HIT 2430 Counting the algorithms (树状数组)
- hoj 2430 Counting the algorithms +树状数组
- hoj 2430 Counting the algorithms (树状数组)
- hoj 2430 Counting the algorithms(树状数组,贪心)
- HOJ2430 Counting the Algorithms 树状数组
- HOJ 2430——Counting the algorithms(树状数组+贪心)
- hoj 2430 Counting the algorithms
- HIT-1867-树状数组
- HOJ_2430 Counting the algorithms
- hoj Counting the algorithms
- hoj2430 Counting the algorithms
- 树状数组实践-HIT经理的烦恼
- 【HOJ2430】Counting the Algorithms-区间维护
- HDU Boring counting 树状数组
- HDU3450-Counting Sequences【树状数组
- Counting Black(树状数组)
- 区间素数个数 树状数组 HIT 1867 经理的烦恼
- HIT 1867 经理的烦恼(树状数组)
- Bootstrap
- 动态规划:从新手到专家
- 在Ubuntu中安装sublime并编译以及运行C++程序
- HDU 1465:不容易系列之一
- 引用可以是void类型吗?
- HIT 2430 Counting the algorithms (树状数组)
- poj 2955 Brackets(区间dp)
- 如何在元组中的每个元素命名,提高程序可读性
- centos下saltstack的安装与部署
- 关于ActionBar
- Javascript 中 == 和 === 区别
- 23个MySQL常用查询语句
- Spring 框架简介
- HDFS的文件系统