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 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 first3 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.
这道题可以用树状数组做,用map<int,int>hash,来储存相同的数第二次出现的位置,这样待会更新的时候回比较方便,然后这里用到了贪心策略,即依次从左到右进行循环,找出相同的两个数,然后求出两个位置的差,然后删除这两个位置。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int a[200006],b[200006],n,vis[200006];
int lowbit(int x){
return x&(-x);
}
void update(int pos,int num)
{
while(pos<=2*n){
b[pos]+=num;pos+=lowbit(pos);
}
}
int getsum(int pos)
{
int num=0;
while(pos>0){
num+=b[pos];pos-=lowbit(pos);
}
return num;
}
int main()
{
int m,i,j,t,sum;
while(scanf("%d",&n)!=EOF)
{
map<int,int>hash;
hash.clear();
for(i=1;i<=2*n;i++){
vis[i]=0;
scanf("%d",&a[i]);
hash[a[i]]=i;
b[i]=lowbit(i);
}
sum=0;
for(i=1;i<=2*n;i++){
if(vis[i]==1)continue;
vis[i]=1;
t=hash[a[i]];
vis[t]=1;
sum+=getsum(t)-getsum(i);
update(i,-1);update(t,-1);
//printf("%d\n",sum);
}
printf("%d\n",sum);
}
return 0;
}
hoj2430 Counting the algorithms的更多相关文章
- 【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 ...
- 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 ...
- hoj Counting the algorithms
贪心加树状数组 给出的数据可能出现两种情况,包括与不包括,但我们从右向左删就能避免这个问题. #include<stdio.h> #include<string.h> #inc ...
- [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 ...
随机推荐
- Flutter 基础组件:按钮
前言 Material组件库中提供了多种按钮组件如RaisedButton.FlatButton.OutlineButton等,它们都是直接或间接对RawMaterialButton组件的包装定制,所 ...
- 误删数据库怎么办?mysql 回滚,撤销操作,恢复数据
刚刚不小心把数据库删掉了,于是想着上网上找找有没有可以恢复数据库的方法,没想到还真有,除了备份以外,还有以下方法. 在mysql有时执行了错误的update或者delete时导致大量数据错误恢复的办法 ...
- PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二手急速响应捡垃圾平台_3(附源码持续更新)
说明 文章首发于HURUWO的博客小站,本平台做同步备份发布. 如有浏览或访问异常图片加载失败或者相关疑问可前往原博客下评论浏览. 原文链接 PYTHON爬虫实战_垃圾佬闲鱼爬虫转转爬虫数据整合自用二 ...
- Junit测试和反射
Junit单元测试 测试分类 黑盒测试:不需要写代码,给输入值,看程序能否得到输出期望值. 白盒测试:需要些代码,关注程序具体的执行流程. Junit的使用 步骤 定义一个测试类(测试用例). 定义测 ...
- centos6-centos7防火墙(iptables-firewalld)设置端口nat转发
背景: 将本机的 8080端口转发至其他主机,主机 IP:192.168.1.162,目标主机 IP和端口192.168.1.163:80,方法如下: centos6系统iptables环境下: ip ...
- jemeter断言和性能分析
一.添加断言 1.原因:检查是否有该结果,一般一个请求过去除了400和500的只要通过的都会代表请求成功,比如登录页面及时填写了错误密码,虽然会返回密码错误,但这个请求还是成功的,所以我们要添加断言, ...
- flume到底会丢数据吗?其可靠性如何?——轻松搞懂Flume事务机制
先给出答案: 需要结合具体使用的source.channel和sink来分析,具体结果可看本文最后一节. Flume事务 一提到事务,我们首先就想到的是MySQL中的事务,事务就是将一批操作做成原 ...
- 淘宝APP消息推送模型
为什么到了2020年,"统一推送联盟"依旧无法起显著作用? - 知乎 https://www.zhihu.com/question/370632447 https://mp.wei ...
- https://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/util/MurmurHash.html
https://hbase.apache.org/devapidocs/org/apache/hadoop/hbase/util/MurmurHash.html https://github.com/ ...
- 我感兴趣的 .NET 开源项目
Gui.cs - 用于.NET 的控制台终端 UI 工具包 https://github.com/migueldeicaza/gui.cs Newtonsoft.Json - 高性能的 JSON 解析 ...