HDU6168-Numbers(離散|哈希)

Numbers

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 603 Accepted Submission(s): 318

Problem Description
zk has n numbers a1,a2,…,an. For each (i,j) satisfying 1≤i

題意:

給定初始的n個數字和n個數字的二元和,求原始的n個數字

將b數組排序,取出最小的兩項作爲
a1,a2
a1,a2
​​
,刪除
a1,a2,a1+a2
​​
,再取出最小項作爲
a3
​​
,再刪除
a3,a1+a3,a2+a3

,再取出最小項作爲
a4
​​
,依次列推。
用set或者map/離散後的桶排來快速維護這個過程

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <string>
#include <map> 
using namespace std;
typedef long long LL;

const int maxn=125260;

int a[maxn],b[maxn];
int n,cnt,l;
map<int,int>Q;

inline int IN()
{
  int x=0,y=1;char c;
  for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')y=-1;
  for(;c>='0'&&c<='9';c=getchar())x=x*10+c-'0';
  return x*y;
}

int main()
{
    while(~scanf("%d",&n))
    {
        Q.clear();
        for(int i=0;i<n;++i)
        {
          a[i]=IN();
          Q[a[i]]++;
        }

        sort(a,a+n);
        n=unique(a,a+n)-a;
        b[0]=a[0];
        Q[a[0]]--;
        if(Q[a[0]])
        {
          b[1]=a[0];
          Q[a[0]]--;
          l=0;
        }
        else
        {
          b[1]=a[1];
          Q[a[1]]--;
          l=1;
        }

        int x=lower_bound(a,a+n,b[0]+b[1])-a;
        Q[a[x]]--;
        cnt=1;
        for(int i=l;i<n;++i)
        {
          while(Q[a[i]])
          {
            b[++cnt]=a[i];
            Q[a[i]]--;
            for(int j=0;j<cnt;++j)
            {
              x=lower_bound(a,a+n,b[j]+a[i])-a;
              Q[a[x]]--;
            }
          } 
        }
        printf("%d\n%d",cnt+1,b[0]);

        for(int i=1;i<=cnt;++i)printf(" %d",b[i]);
        printf("\n");
    }

  return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章