HOJ——T 2430 Counting the algorithms
http://acm.hit.edu.cn/hoj/problem/view?id=2430
Source : mostleg | |||
Time limit : 1 sec | Memory limit : 64 M |
Submitted : 804, Accepted : 318
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 3is 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
3 1 2 3 1 2 3 3 1 2 3 3 2 1
Sample Output
6 9
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.
题意:给你长度为2*n的序列,保证1~n中每个数会出现两次,求出相同数坐标差的和的最大值、、每次得到一个坐标差都会讲两个数从序列中删除从而改变编号
贪心+树状数组
考虑两种情况 ①当两组1~n不包含时,什么顺序删数都是等价的; ②包含时,从右向左删是最优的,可以保证差最大。 用树状数组维护坐标
#include <algorithm> #include <cstring> #include <cstdio> using namespace std; const int N(200000+5); int n,ans,x[N<<1],last ,tr ; #define lowbit(x) (x&((~x)+1)) inline void Update(int i,int x) { for(;i<=N;i+=lowbit(i)) tr[i]+=x; } inline int Query(int x) { int ret=0; for(;x;x-=lowbit(x)) ret+=tr[x]; return ret; } int main() { for(;~scanf("%d",&n);ans=0) { memset(tr,0,sizeof(tr)); memset(last,0,sizeof(last)); for(int i=1;i<=(n<<1);i++) { scanf("%d",x+i),Update(i,1); last[x[i]]=i; } for(int i=1;i<=(n<<1);i++) { ans+=Query(last[x[i]])-Query(i); Update(last[x[i]],-1); } printf("%d\n",ans); } return 0; }
- HOJ 2430——Counting the algorithms(树状数组+贪心)
- hoj 2430 Counting the algorithms
- hoj 2430 Counting the algorithms
- hoj 2430 Counting the algorithms(树状数组,贪心)
- hoj 2430 Counting the algorithms (树状数组)
- HOJ_2430 Counting the algorithms
- hoj2430 Counting the algorithms
- 【HOJ2430】【贪心+树状数组】 Counting the algorithms
- HOJ2430 Counting the Algorithms 树状数组
- 【HOJ2430】Counting the Algorithms-区间维护
- A list of books on general-purpose algorithms for the practical programmer (or 'software engineer')
- Hoj 3136 To help the beauty
- Counting-the-number-of-set-bits-in-an-integer
- 统治我们世界的十大真实算法(The real 10 algorithms that dominate our world)
- The Most Important Algorithms
- Counting the on hand quantities by API inv_quantity_tree_pub.query_quantities
- Hoj 1307 Choose Your Words Carefully、Hoj 1989 The Most Frequent Word
- Basic Data Structures and Algorithms in the Linux Kernel--reference
- POJ 2282 The Counting Problem
- (纪录片)现代生活的秘密规则:算法 The Secret Rules of Modern Living: Algorithms