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(+);
int n,ans,x[N<<],last[N],tr[N]; #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=;
for(;x;x-=lowbit(x)) ret+=tr[x];
return ret;
} int main()
{
for(;~scanf("%d",&n);ans=)
{
memset(tr,,sizeof(tr));
memset(last,,sizeof(last));
for(int i=;i<=(n<<);i++)
{
scanf("%d",x+i),Update(i,);
last[x[i]]=i;
}
for(int i=;i<=(n<<);i++)
{
ans+=Query(last[x[i]])-Query(i);
Update(last[x[i]],-);
}
printf("%d\n",ans);
}
return ;
}
HOJ——T 2430 Counting the algorithms的更多相关文章
- hoj Counting the algorithms
贪心加树状数组 给出的数据可能出现两种情况,包括与不包括,但我们从右向左删就能避免这个问题. #include<stdio.h> #include<string.h> #inc ...
- 【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 th ...
- hoj2430 Counting the algorithms
My Tags (Edit) Source : mostleg Time limit : 1 sec Memory limit : 64 M Submitted : 725, Acce ...
- [Algorithms] Counting Sort
Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...
- Coursera Algorithms week3 归并排序 练习测验: Counting inversions
题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...
- [算法]Comparison of the different algorithms for Polygon Boolean operations
Comparison of the different algorithms for Polygon Boolean operations. Michael Leonov 1998 http://w ...
- [zt]Which are the 10 algorithms every computer science student must implement at least once in life?
More important than algorithms(just problems #$!%), the techniques/concepts residing at the base of ...
- The Aggregate Magic Algorithms
http://aggregate.org/MAGIC/ The Aggregate Magic Algorithms There are lots of people and places that ...
- Top 10 Algorithms for Coding Interview--reference
By X Wang Update History:Web Version latest update: 4/6/2014PDF Version latest update: 1/16/2014 The ...
随机推荐
- Linux多线程实践(六)使用Posix条件变量解决生产者消费者问题
前面的一片文章我们已经讲过使用信号量解决生产者消费者问题.那么什么情况下我们须要引入条件变量呢? 这里借用 http://www.cnblogs.com/ngnetboy/p/3521547.htm ...
- bzoj3438: 小M的作物(那年花开最小割)
3438: 小M的作物 题目:传送门 题解: 最小割标准水题(做了几天的最小割之后表示是真的水) 为什么水:博主已经做过两道基本一样的题目了... 详情参考:bzoj3894 代码: #include ...
- rest_framework-权限-总结完结篇
#权限#创建一个权限类 在view添加列表 class MyPermission(object): #message 表示权限决绝时返回的数据 message = "必须是SVIP" ...
- 33.unique_ptr独享内存智能指针
#include <iostream> #include <memory> #include <string> #include <vector> us ...
- java根据模板导出PDF(利用itext)
一.制作模板 1.下载Adobe Acrobat 9 Pro软件(pdf编辑器),制作模板必须使用该工具. 2.下载itextpdf-5.5.5.jar.itext-asian-5.2.0.j ...
- 前端验证银行卡(Luhn校验算法)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- POJ 1320 Street Numbers(佩尔方程)
Street Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3078 Accepted: 1725 De ...
- ubuntu 18.04网卡命名规则改回传统的ethx
自15版本开始网卡命名规则就不叫eth0了.而是用可预期网络接口设备名称的命名规则,比如网卡名为enp3s0 . 如果想要变回ethx也是可以的,参考以下步骤: 1.编辑/etc/default/gr ...
- DAG-背包九解-01背包
饭卡: 电子科大本部食堂的饭卡有一种很诡异的设计,即在购买之前判断余额.如果购买一个商品之前,卡上的剩余金额大于或等于5元,就一定可以购买成功(即使购买后卡上余额为负),否则无法购买(即使金额足够 ...
- 关于vue自定义事件中,传递参数的一点理解
例如有如下场景 先熟悉一下Vue事件处理 <!-- 父组件 --> <template> <div> <!--我们想在这个dealName的方法中传递额外参数 ...