码迷,mamicode.com
首页 > 其他好文 > 详细

NWERC 2016 F. Free Weights

时间:2017-10-18 22:12:12      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:题目   匹配   att   inf   logs   ace   continue   tac   name   

题面链接:

http://codeforces.com/gym/101170/attachments

题目大意:

现在有两行一共2N个哑铃,重量相同的哑铃为一队。现在让尽量移动质量小的一些哑铃,使得每一对哑铃相邻(上下相邻不算)。

问移动的哑铃中,重量最大的值是多少?

大致思路:

哑铃的质量为1~1e9,可以采用二分答案的方法。即每次的值就是移动哑铃中重量最大的那一个。而比他重量小的则可以随便移动。

所以对于每次枚举,判断是否合法的方法就是把质量比他大的哑铃进行匹配(类似于括号匹配)

如果合法,则可以尝试一个更小的数。如果不合法,则答案为一个更大的数

代码:

 1 #include<iostream>
 2 #include<cstdlib>
 3 using namespace std;
 4 const int maxn=1e6+7;
 5 const int INF=1e9+7;
 6 int a[2][maxn],stack[maxn];
 7 int n;
 8 bool slove(int mid)//判断是否合法
 9 {
10     int top=0;
11     for(int i=0;i<2;++i){
12         for(int j=1;j<=n;++j){
13             if(a[i][j]<=mid)
14                 continue;
15             if(top&&stack[top]==a[i][j]){
16                 top--;
17                 continue;
18             }
19             if(top&&stack[top]!=a[i][j])//匹配不成功
20                 return false;
21             stack[++top]=a[i][j];
22         }
23         if(top)
24             return false;
25     }
26     return true;
27 }
28 int main()
29 {
30     ios::sync_with_stdio(false);
31     cin>>n;
32     for(int i=0;i<2;++i)
33         for(int j=1;j<=n;++j)
34             cin>>a[i][j];
35     int l=-1,r=INF;
36     while(l+1<r)
37     {
38         int mid=(l+r)>>1;//二分答案
39         if(slove(mid))
40             r=mid;
41         else
42             l=mid;
43     }
44     cout<<r<<endl;
45     return 0;
46 }

 

NWERC 2016 F. Free Weights

标签:题目   匹配   att   inf   logs   ace   continue   tac   name   

原文地址:http://www.cnblogs.com/SCaryon/p/7689270.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!