Northwestern European Regional Contest 2016 NWERC ,F题Free Weights(优先队列+Map标记+模拟)
传送门:
Vjudge:https://vjudge.net/problem/Gym-101170F
CF: http://codeforces.com/gym/101170
The city of Bath is a noted olympic training ground—bringing local, national, and even international teams to practice. However, even the finest gymnasium falls victim to the cardinal sin. . .Weights put back in the wrong spots. All of the pairs of dumbbells sit in no particular order on the two racks, possibly even with some of them split between rows. Initially each row has an equal number of dumbbells, however, this being a well-funded professional gym, there is infinite space at either end of each to hold any additional weights. To move a dumbbell, you may either roll it to a free neighbouring space on the same row with almost no effort, or you may pick up and lift it to another free spot; this takes strength proportional to its weight. For each pair of dumbbells, both have the same unique weight. What is the heaviest of the weights that you need to be able to lift in order to put identical weights next to each other? Note that you may end up with different numbers of weights on each row after rearranging; this is fine.
Input
The input consists of:
• one line containing the integer n (1 ≤ n ≤ 10^6 ), the number of pairs;
• two lines, each containing n integers w1 . . . wn (1 ≤ wi ≤ 10^9 for each i), where wi is the mass of the weight i-th from the left along this row. Every weight in the input appears exactly twice.
Output
Output the weight of the heaviest dumbbell that must be moved, in order that all items can be paired up while lifting the smallest possible maximum weight.
Sample Input 1
5
2 1 8 2 8
9 9 4 1 4
Sample Output 1
2
Sample Input 2
8
7 7 15 15 2 2 4 4
5 5 3 3 9 9 1 1
Sample Output 2
0
题目大意:
输入N,下面有两行,每行N个数字,代表杠铃的重量,保证给定数字,如果出现,肯定会出现2次,即构成一对。
询问的是要必须移动的杠铃中重量最大的杠铃的重量做到:
要求每两个相同重量的杠铃要在同一行相邻,很像消消乐,当然8 2 2 8不行,而8 8 2 2 可以。每个杠铃可以插入两个杠铃中间,或者放在一行的最右端最左端都行。
竖着两个不算相邻,比如说:
5
1 2 3 4 5
1 2 3 4 5
这里的1和1不相邻。
比如说第一组样例:
5
2 1 8 2 8
9 9 4 1 4
必须得把1,2移动。但是可以不移动8这个。所以最大重量是2。
第二组样例则不需要移动,已经是OK的了。所以输出0。
思路:
维护个优先队列,如果元素不满2个就入队列,如果元素满2个就比较两个元素一样不一样。
如果不一样:map标记小的那个,然后把小的出队列,顺便记录到MAX里面(表示要移动)
如果一样:两个一起出队列,不标记。
当然处理完了一行可能有剩下的,还得对最后的优先队列判断一下是不是空的,不是的话还得跟MAX比。
第二行也是一样。
这样就保证了必须要移动的是比较小的。因为每次都是优先队列里面小的被标记。
具体看代码(代码直接在输入的时候就模拟了入队列这个过程):
感觉代码跑了1K5MS应该不是正解。虽然时限是给了1W MS,算是一种方法吧。
2018/9/8更新:正解应该是二分判断
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<assert.h>
using namespace std;
inline bool scan_d(int &num)
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<''||in>'')) in=getchar();
if(in=='-'){ IsN=true;num=;}
else num=in-'';
while(in=getchar(),in>=''&&in<=''){
num*=,num+=in-'';
}
if(IsN) num=-num;
return true;
}
int main()
{
int n;
while(~scanf("%d",&n)){
priority_queue<int,vector<int>,greater<int> >q;
map<int,int>mp;
int Max = ;
for(int i = ;i < n ; i++){
int x;
scanf("%d",&x);
if(q.size() == || q.empty()){
q.push(x);
}//如果为1或者空就直接入队列
if(q.size() == ){
int n1 = q.top();q.pop();
int n2 = q.top();//暂时不出队,因为如果不相等是不需要出队列的
if(mp[n1] == ){
continue;
}//如果标记过了,那就跳过,说明在之前已经通过移动搞定匹配
if(n1 != n2){
mp[n1] = ;
Max = max(Max,n1);
}//不相等就处理比较小的那个
if(n1 == n2){
q.pop();
}//相等的话一起出队列
}
}
while(!q.empty()){
Max=max(Max,q.top());
q.pop();
}//检查最后是否有漏网之鱼,需要匹配
for(int i = ;i < n ; i++){
int x;
scanf("%d",&x);
if(q.size() == || q.empty()){
q.push(x);
}
if(q.size() == ){
int n1 = q.top();q.pop();
int n2 = q.top();
if(mp[n1] == ){
continue;
}
if(n1 != n2){
mp[n1] = ;
Max = max(Max,n1);
}
if(n1 == n2){
q.pop();
}
}
}
printf("%d\n",Max);
}
}
Northwestern European Regional Contest 2016 NWERC ,F题Free Weights(优先队列+Map标记+模拟)的更多相关文章
- codeforces Gym - 101485 D Debugging (2015-2016 Northwestern European Regional Contest (NWERC 2015))
题目描述: 点击打开链接 这题题意其实很不好理解,你有一个n行的程序,现在程序运行了r时间之后停止了运行,证明此处有一个bug,现在你需要在程序中加printf来调试找到bug所在的位置,你每次加一个 ...
- 2017-2018 Northwestern European Regional Contest (NWERC 2017)
A. Ascending Photo 贪心增广. #include<bits/stdc++.h> using namespace std; const int MAXN = 1000000 ...
- Northwestern European Regional Contest 2017-I题- Installing Apps题解
一.题意 有一个手机,容量为$C$,网上有$N$个app,每个app有个安装包大小$d_i$,有个安装后的占用空间大小$s_i$,安装app是瞬间完成的,即app的占用空间可以瞬间由$d_i$变成$s ...
- 2015-2016 Northwestern European Regional Contest (NWERC 2015)
训练时间:2019-04-05 一场读错三个题,队友恨不得手刃了我这个坑B. A I J 简单,不写了. C - Cleaning Pipes (Gym - 101485C) 对于有公共点的管道建边, ...
- 2012-2013 Northwestern European Regional Contest (NWERC 2012)
B - Beer Pressure \(dp(t, p_1, p_2, p_3, p_4)\)表示总人数为\(t\),\(p_i\)对应酒吧投票人数的概率. 使用滚动数组优化掉一维空间. 总的时间复杂 ...
- Northwestern European Regional Contest 2014 Gym - 101482
Gym 101482C Cent Savings 简单的dp #include<bits/stdc++.h> #define inf 0x3f3f3f3f #define inf64 0x ...
- 2006 ACM Northwestern European Programming Contest C题(二分求最大)
My birthday is coming up and traditionally I'm serving pie. Not just one pie, no, I have a numberN o ...
- ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbilisi, November 24, 2010
ACM ICPC 2010–2011, Northeastern European Regional Contest St Petersburg – Barnaul – Tashkent – Tbil ...
- 2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17)
2017-2018 ACM-ICPC Northern Eurasia (Northeastern European Regional) Contest (NEERC 17) A 题意:有 n 个时刻 ...
随机推荐
- react-native获取设备信息app版本信息,react-native-device-info
安装 yarn add react-native-device-info react-native link react-native-device-info link 之后就可以直接使用了,ios ...
- 机器学习进阶-svm支持向量机
支持向量机需要解决的问题:找出一条最好的决策边界将两种类型的点进行分开 这个时候我们需要考虑一个问题,在找到一条直线将两种点分开时,是否具有其他的约束条件,这里我们在满足找到一条决策边界时,同时使得距 ...
- 【JEECG技术文档】表单配置-树形表单
表单配置支持树型表单了,具体效果如下图: 配置说明: 1.是否树:选择是. 2.树形表单父Id:表的自关联外键. 3.树形表单列表:显示树形图标的列,如上图中为[组织机构名称]. 4.默认值:最外层数 ...
- jquery实现增删改(伪)-老男孩作业day13
使用jquery进行,文件的编写,实现自增id,删除,添加,编辑模式. jquery放在本地,src="jquery_js.js" 可以改成其他,或者在线的路径 readme &l ...
- Windows下如何查看某个端口被谁占用
开发时经常遇到端口被占用的情况,这个时候总是很令人抓狂,知道被哪个进程占用还好,结束就是了,要是不知道我们该怎么办呢? 我告诉大家一个方法,^_^. 1. 开始—->运行—->cmd,或者 ...
- 08_组件三大属性(2)_props
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- Java——如何创建文件夹及文件,删除文件,文件夹
package com.zz; import java.io.File; import java.io.IOException; /** * Java创建文件夹 */ public class Cre ...
- 谈USB重定向的方式
在桌面虚拟化的项目中,常常会遇到用户提出的各自外设需求,这时产品对外设的兼容性就成为了项目成败的拦路虎 本文试图用通俗易懂的语言讲讲USB外设重定向的工作流程,先看看普通PC上USB设备的工作流程 u ...
- php中的错误和异常
总结: php错误不会抛出异常,因此不能被catch,但会根据配置写入日志文件或者输出到浏览器,所以可以通过日志文件查看错误 php异常都必须自己抛出,并通过catch捕捉.SQL语句执行的错误好像可 ...
- C++中类的多继承
在写这一主题的文章之前,在网上找到一篇很非常好的文章C++之继承与多态.就没有必要做重复造轮子的事件了,那就从这篇文章开始吧! 在c++中一个类可以从多个基类中派生(即可以有多个父类),这就是多继承. ...