Gym - 101170F - Free Weights (二分)

思路:

二分重量,判断删除后大于重量的哑铃是否相等

#include 
using namespace std;
int n;
const int maxn = 1e6 +10;
set se;
int a[maxn];
int b[maxn];
vectorv;
bool check(int id)
{
    int x = v[id];
    for(int i =  0 ;  i < n; i++)
    {
        if(a[i] > x)
        {
            int j =i + 1;
            while(j < n && a[j]!= a[i])
            {
                if(a[j]>x)
                    return 0;
                j++;
            }
            if(a[j] != a[i]) return 0;
            i = j;
        }
        else continue;
    }
    for(int i =  0 ;  i < n; i++)
    {
        if(b[i] > x)
        {

            int j = i + 1;
            while(j < n && b[j]!= b[i])
            {
                if(b[j]>x)
                    return 0;
                j++;
            }
            if(b[j] != b[i]) return 0;
            i = j;
        }
        else continue;
    }
    return 1;
}
bool check0()
{
    if(n & 1) return 0;
    for(int i =  0 ;  i < n; i+=2)
    {
        if(a[i]!=a[i+1])
        {
            return 0;
        }
    }
    for(int i =  0 ;  i < n; i+=2)
    {
        if(b[i]!=b[i+1])
            return 0;
    }
    return 1;
}
int main()
{
    cin>>n;
    for(int i =  0; i < n; i ++)
    {
        cin>>a[i];
        if(se.count(a[i])==0)
        {
            v.push_back(a[i]);
            se.insert(a[i]);
        }
    }
    for(int i =  0; i < n; i ++)
    {
        cin>>b[i];
        if(se.count(b[i])==0)
        {
            v.push_back(b[i]);
            se.insert(b[i]);
        }
    }
    sort(v.begin(),v.end());
    if(check0())
    {
        cout<<0<

 

你可能感兴趣的:(ACM解题记录,二分)