uva 10125 - Sumsets

简介:

  

    本来这几天不打算写题了,但是发现太无聊。

 

    这题一开始直接dfs,果断超时,加个搜到就跳出,直接WA了,因为例如1 4 5 6 7这样数列,7 6 1<4 5 6所以直接dfs循环是不行的 

 

    然后就a+b=d-c n^2的复杂度,不想用hash,于是就二分,结果忘记排序,WA了好多次……

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <queue>
#define INF 100000000000ll
using namespace std;
long long org[1005];
long long add[1000010];
int ke[1000010][2];
bool bsearch(int i,int j,int l,int r)
{
    int mid;
    long long aim=org[i]-org[j];
    while(l<r)
    {
        mid=(l+r)>>1;
        if(add[mid]<aim)l=mid+1;
        else if(add[mid]==aim)
        {
            while(add[mid]==aim&&mid>=0)mid--;//找到最左边的相等下标
            mid++;
            while(add[mid]==aim&&(ke[mid][0]==i||ke[mid][0]==j||ke[mid][1]==i||ke[mid][1]==j)) mid++;
            if(add[mid]==aim) return 1;
            else return 0;
        }
        else r=mid;
    }
    return 0;
}
int main()
{
    int n,i,j,now;
    while(~scanf("%d",&n)&&n)
    {
        for(i=0;i<n;i++)
         scanf("%lld",&org[i]);
        now=0;
        sort(org,org+n);
        for(i=0;i<n;++i)
          for(j=i+1;j<n;++j)
          {
              if(org[i]==org[j])continue;
              ke[now][0]=i;ke[now][1]=j;
              add[now++]=org[i]+org[j];
          }
        sort(add,add+now);
        bool flag=1;
        for(i=n-1;i>=0&&flag;i--)
         for(j=0;j<n&&flag;j++)
         {
            if(org[i]==org[j])continue;
            if(bsearch(i,j,0,now))flag=0;
         }
        if(flag)printf("no solution\n");
        else printf("%lld\n",org[i+1]);
    }
}


 

目录
相关文章
uva10038 Jolly Jumpers
uva10038 Jolly Jumpers
37 0
UVa11968 - In The Airport
UVa11968 - In The Airport
55 0
UVa 10082 WERTYU
Problem Description A common typing error is to place the hands on the keyboard one row to the right of the correct position.
890 0
|
机器学习/深度学习
|
C++
UVA 之10010 - Where's Waldorf?
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SunnyYoona/article/details/24863879 ...
710 0
uva 1160 X-Plosives
点击打开链接uva 1160 思路: 并查集 分析: 1 看懂题目之和就是切菜了 代码: #include #include #include #include using namespace std; const int MAXN...
770 0