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 ...
随机推荐
- QtWebkit里RenderLayer树的绘制具体流程分析
更新:RenderLayer树的绘制对RenderObject的绘制.同一时候补足绘制阶段的描写叙述. QtWebkit里,QWebView,QWebPage和QWebFr ...
- emmet教程
https://www.zfanw.com/blog/zencoding-vim-tutorial-chinese.html https://www.zfanw.com/blog/zencoding- ...
- grpc编译错误解决
berli@berli-VirtualBox:~/grpc$ make [MAKE] Generating cache.mk [C] Compiling src/core/lib/s ...
- Persistence
Most of the programs we have seen so far are transient in the sense that they run for a short time a ...
- Fiddler 故障
如果只要打开 Fiddler 就没法进网页,重启 Fiddler 问题依旧.卸载并重装 Fiddler 后,提示 8888 端口被占,错误弹窗中包含“ipoverusbsvc:2492”,说明有设备在 ...
- ListView和GridView的setOnScrollListener的简介
---恢复内容开始--- 设置ListView和GridView的滑动监听 circle_lv.setOnScrollListener(new AbsListView.OnScrollListener ...
- <Sicily>Inversion Number(线段树求逆序数)
一.题目描述 There is a permutation P with n integers from 1 to n. You have to calculate its inversion num ...
- ReactiveCocoa使用记录-网络登录事件
对于一个应用来说,绝大部分的时间都是在等待某些事件的发生或响应某些状态的变化,比如用户的触摸事件.应用进入后台.网络请求成功刷新界面等等,而维护这些状态的变化,常常会使代码变得非常复杂,难以扩展.而 ...
- echarts如何修改数据视图dataView中的样式
原文链接:点我 做了一个现实折线图的图表,通过右上角icon可以自由切换成柱状图,表格.在表格中遇到的一点小问题,解决方案如下: 1.场景重现 这是一个显示两个折线图的图表,一切看起来都很顺利.但是点 ...
- 浴谷 P1768 天路
P1768 天路 题目描述 “那是一条神奇的天路诶~,把第一个神犇送上天堂~”,XDM先生唱着这首“亲切”的歌曲,一道猥琐题目的灵感在脑中出现了. 和C_SUNSHINE大神商量后,这道猥琐的题目终于 ...