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不包含时,什么顺序删数都是等价的; ②包含时,从右向左删是最优的,可以保证差最大。 用树状数组维护坐标

 1 #include <algorithm>
 2 #include <cstring>
 3 #include <cstdio>
 4
 5 using namespace std;
 6
 7 const int N(200000+5);
 8 int n,ans,x[N<<1],last[N],tr[N];
 9
10 #define lowbit(x) (x&((~x)+1))
11 inline void Update(int i,int x)
12 {
13     for(;i<=N;i+=lowbit(i)) tr[i]+=x;
14 }
15 inline int Query(int x)
16 {
17     int ret=0;
18     for(;x;x-=lowbit(x)) ret+=tr[x];
19     return ret;
20 }
21
22 int main()
23 {
24     for(;~scanf("%d",&n);ans=0)
25     {
26         memset(tr,0,sizeof(tr));
27         memset(last,0,sizeof(last));
28         for(int i=1;i<=(n<<1);i++)
29         {
30             scanf("%d",x+i),Update(i,1);
31             last[x[i]]=i;
32         }
33         for(int i=1;i<=(n<<1);i++)
34         {
35             ans+=Query(last[x[i]])-Query(i);
36             Update(last[x[i]],-1);
37         }
38         printf("%d\n",ans);
39     }
40     return 0;
41 }
时间: 2024-10-27 13:22:20

HOJ——T 2430 Counting the algorithms的相关文章

hoj Counting the algorithms

贪心加树状数组 给出的数据可能出现两种情况,包括与不包括,但我们从右向左删就能避免这个问题. #include<stdio.h> #include<string.h> #include<iostream> using namespace std; const int maxn=200010; int f[maxn],l[maxn],a[maxn]; long long tree[maxn]; int n; inline int lowbit(int x) { retur

hoj2430 Counting the algorithms

My Tags   (Edit)   Source : mostleg   Time limit : 1 sec   Memory limit : 64 M Submitted : 725, Accepted : 286 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 tim

【HOJ2430】【贪心+树状数组】 Counting the algorithms

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 proble

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

[Algorithms] Counting Sort

For a nice introduction to counting sort, please refer to Introduction to Alogrithms, 3rd edition. The following code is basically a translation of the pseudo code above. 1 #include <iostream> 2 #include <vector> 3 #include <ctime> 4 5 u

[算法]Comparison of the different algorithms for Polygon Boolean operations

Comparison of the different algorithms for  Polygon Boolean operations. Michael Leonov 1998 http://www.angusj.com/delphi/clipper.php#screenshots http://www.complex-a5.ru/polyboolean/comp.html http://www.angusj.com/delphi/clipper.php#screenshots Intro

The Aggregate Magic Algorithms

http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that create and collect algorithms of all types (here are a few WWW sites). Unfortunately, in building systems hardware and software, we in The Aggregate o

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include