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<'0'||in>'9')) in=getchar();
        if(in=='-'){ IsN=true;num=0;}
        else num=in-'0';
        while(in=getchar(),in>='0'&&in<='9'){
                num*=10,num+=in-'0';
        }
        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 = 0;
        for(int i = 0 ;i < n ; i++){
            int x;
            scanf("%d",&x);
            if(q.size() == 1 || q.empty()){
                q.push(x);
            }//如果为1或者空就直接入队列
            if(q.size() == 2){
                int n1 = q.top();q.pop();
                int n2 = q.top();//暂时不出队,因为如果不相等是不需要出队列的
                if(mp[n1] == 1){
                    continue;
                }//如果标记过了,那就跳过,说明在之前已经通过移动搞定匹配
                if(n1 != n2){
                    mp[n1] = 1;
                    Max = max(Max,n1);
                }//不相等就处理比较小的那个
                if(n1 == n2){
                    q.pop();
                }//相等的话一起出队列
            }
        }
        while(!q.empty()){
            Max=max(Max,q.top());
            q.pop();
        }//检查最后是否有漏网之鱼,需要匹配
        for(int i = 0 ;i < n ; i++){
            int x;
            scanf("%d",&x);
            if(q.size() == 1 || q.empty()){
                q.push(x);
            }
            if(q.size() == 2){
                int n1 = q.top();q.pop();
                int n2 = q.top();
                if(mp[n1] == 1){
                    continue;
                }
                if(n1 != n2){
                    mp[n1] = 1;
                    Max = max(Max,n1);
                }
                if(n1 == n2){
                    q.pop();
                }
            }
        }
        printf("%d
",Max);
    }
}
原文地址:https://www.cnblogs.com/Esquecer/p/9478490.html