zoukankan      html  css  js  c++  java
  • NWERC 2016 F. Free Weights

    题面链接:

    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 }
  • 相关阅读:
    PAT (Advanced Level) 1086. Tree Traversals Again (25)
    PAT (Advanced Level) 1085. Perfect Sequence (25)
    PAT (Advanced Level) 1084. Broken Keyboard (20)
    PAT (Advanced Level) 1083. List Grades (25)
    PAT (Advanced Level) 1082. Read Number in Chinese (25)
    HDU 4513 吉哥系列故事――完美队形II
    POJ Oulipo KMP 模板题
    POJ 3376 Finding Palindromes
    扩展KMP
    HDU 2289 Cup
  • 原文地址:https://www.cnblogs.com/SCaryon/p/7689270.html
Copyright © 2011-2022 走看看