【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 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

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=1 marks. Therefore, in total you get 5+3+1=9 and that is the best strategy.

【分析】

比较水的一道题,根据题意可知,任何两个区间仅为相交或包含的关系。

相交无论先删除哪一个对结果都没有影响,包含当然要先删除外面的那一个,所以从后往前删不会有包含的关系。

具体用树状数组实现就可以了。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <vector>
 6 #include <utility>
 7 #include <iomanip>
 8 #include <string>
 9 #include <cmath>
10 #include <map>
11
12 const int MAXN = 100000 * 2 + 10;
13 using namespace std;
14 struct DATA{
15        int l, r;
16 }num[MAXN * 2];
17 int data[MAXN * 2];
18 int C[MAXN * 2], cnt[MAXN * 2];
19 int n;
20 bool get[MAXN * 2];
21
22 int lowbit(int x){return x&-x;}
23 void add(int x, int val){
24      while (x <= 2 * n){
25            C[x] += val;
26            x += lowbit(x);
27      }
28      return;
29 }
30 int sum(int x){
31     int cnt = 0;
32     while (x > 0){
33           cnt += C[x];
34           x -= lowbit(x);
35     }
36     return cnt;
37 }
38 void init(){
39      C[0] = 0;
40      for (int i = 1; i <= 2 * n; i++) C[i] = cnt[i] = 0;
41      for (int i = 1; i <= 2 * n; i++){
42          scanf("%d", &data[i]);
43          if (cnt[data[i]] == 0) {num[data[i]].l = i;cnt[data[i]]++;}
44          else num[data[i]].r = i;
45          add(i, 1);
46      }
47      long long Ans = 0;
48      memset(get, 0, sizeof(get));
49      for (int i = 2 * n; i >= 1; i--){
50          if (get[i] == 1) continue;
51          Ans += sum(i) - sum(num[data[i]].l);
52          add(i, -1);
53          add(num[data[i]].l, -1);
54          get[i] = get[num[data[i]].l] = 0;
55      }
56      printf("%lld\n", Ans);
57 }
58
59 int main(){
60      int T = 0;
61      #ifdef LOCAL
62      freopen("data.txt", "r", stdin);
63      freopen("out.txt", "w", stdout);
64      #endif
65      while (scanf("%d", &n) != EOF){
66            init();
67      }
68      return 0;
69 }
70  

时间: 2024-08-24 09:39:46

【HOJ2430】【贪心+树状数组】 Counting the algorithms的相关文章

[luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)

传送门 贪心...蒟蒻证明不会... 每一次找最大的即可,找出一次最大的,数列会分为左右两边,左边用stl优先队列维护,右边用树状数组维护.. (线段树超时了....) 代码 #include <queue> #include <cstdio> #include <iostream> #define N 100001 #define ls now << 1 #define rs now << 1 | 1 #define max(x, y) (p[

D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个问题: 数轴上有个点突然兴奋,如果自己被身上覆盖了超过条线段,这个点就会浑身难受然后把Koishi批判一番. Koishi十分善良,为了不让数轴上的点浑身难受,也为了让自己开心,她想在数轴上放入尽量多的线段. 按照套路,Koishi假装自己并不会做这道题,所以她就来求你帮忙.并承诺如果你解决了问题就

UVALive 6911---Double Swords(贪心+树状数组(或集合))

题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4923 problem  description Last night, Kingdom of Light was attacked by Kingdom of Dark! The queen of Kingdom of Light, Queen Ar, was ca

Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)

George is a cat, so he loves playing very much. Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers f

[CSP-S模拟测试]:d(贪心+树状数组)

题目传送门(内部题65) 输入格式 第一行,一个自然数$T$,代表数据组数.对于每组数据:第一行,一个正整数$n$,一个自然数$m$.接下来$n$行,每行两个正整数,$a_i,b_i$. 输出格式 对于每组数据,输出一行,一个整数,代表答案. 样例 样例输入: 3 2 0 5 10 5 5 2 1 1 1 2 2 3 1 3 5 4 4 5 3 样例输出: 25412 数据范围与提示 保证$0\leqslant m<n,a_i,b_i\leqslant 10^5$. 题解 题目并不难,考虑贪心,

uva 11054 Gerovia的酒交易(贪心+树状数组)

直线上有n个等距的村庄,每个村庄要么买酒,要么卖酒.把k个单位的酒从一个村庄运到相邻村庄需要k个单位的劳动力.问最少需要多少劳动力才能满足所有村庄的需求 思路贪心 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #inclu

csu 1757(贪心或者树状数组)

1757: 火车入站 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 209  Solved: 51[Submit][Status][Web Board] Description 火车站人们总是在站台等待列车进站,一个站台有火车停留的时候就不能有其他火车进入,今天有n辆火车经过,已知它们进站时间Si以及出站时间Ti,进站时间到出站时间之间火车必须有一个站台给它停靠,问让所有火车都能按时停靠,至少要安排多少个站台给这些火车 Input 第一行输入一个正

13年山东省赛 Boring Counting(离线树状数组or主席树+二分or划分树+二分)

转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 2224: Boring Counting Time Limit: 3 Sec  Memory Limit: 128 MB Description In this problem you are given a number sequence P consisting of N integer and Pi is the ith element in the sequence.

Codeforces Round #413, rated, Div. 1 + Div. 2 C. Fountains(贪心 or 树状数组)

http://codeforces.com/contest/799/problem/C 题意: 有n做花园,有人有c个硬币,d个钻石 (2 ≤ n ≤ 100 000, 0 ≤ c, d ≤ 100 000) ,每一个花园用三个维度描述(a,b,c),分别是美丽度,所花钱币个数,钱币种类,当然,钱币之间不能兑换,该人必须要建筑两座花园,如果可以,输出两座花园总的美丽度,否则输出0: 思路: 首先想到分三种情况讨论,两个花园都用硬币,两个花园都用钻石,一个用钻石一个用硬币. 大神的代码真的是很厉害