hdu 6168 Numbers
阿新 • • 發佈:2017-08-23
line rate input fin one urn scanf inpu p s .
LsF wants to make some trouble. While zk is sleeping, Lsf mixed up sequence a and b with random order so that zk can‘t figure out which numbers were in a or b. "I‘m angry!", says zk.
Can you help zk find out which n numbers were originally in a?
For each test case:
?The first line is an integer m(0≤m≤125250), indicating the total length of a and b. It‘s guaranteed m can be formed as n(n+1)/2.
?The second line contains m numbers, indicating the mixed sequence of a and b.
Each ai is in [1,10^9]
The first line is an integer n, indicating the length of sequence a;
The second line should contain n space-seprated integers a1,a2,...,an(a1≤a2≤...≤an). These are numbers in sequence a.
It‘s guaranteed that there is only one solution for each case. 題意 已經有an個數 對於 1≤i<j≤n,an中的數 兩兩相加,得到b數組 然後把a,b數組亂序 讓你挑選出a數組的內容
Numbers
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 514 Accepted Submission(s): 270
LsF wants to make some trouble. While zk is sleeping, Lsf mixed up sequence a and b with random order so that zk can‘t figure out which numbers were in a or b. "I‘m angry!", says zk.
Can you help zk find out which n numbers were originally in a?
Input Multiple test cases(not exceed 10).
For each test case:
?The second line contains m numbers, indicating the mixed sequence of a and b.
Each ai is in [1,10^9]
Output For each test case, output two lines.
The first line is an integer n, indicating the length of sequence a;
It‘s guaranteed that there is only one solution for each case. 題意 已經有an個數 對於 1≤i<j≤n,an中的數 兩兩相加,得到b數組 然後把a,b數組亂序 讓你挑選出a數組的內容
#include<bits/stdc++.h> using namespace std; const int maxn = 125250+100; map<int,int>mp; int s[maxn];//記錄a和b int t[maxn];//存儲a數組的結果 void init() { mp.clear(); memset(t,0,sizeof(t)); } int main() { int n; while(~scanf("%d",&n) ) { init(); for(int i=1;i<=n;i++) scanf("%d",&s[i]); sort(s+1,s+n+1); int tot = 0; for(int i=1;i<=n;i++) { if(tot + (tot-1)*tot/2 >= n)//總數夠了 就不需要再選下去了 break; int x= s[i]; if(mp[x]==0 || mp.count(x)==0)//前面是這個數在mp中 且數值為1 另外一個是這個不在mp中 { for(int j=0;j<tot;j++) { mp[x+t[j]]++;//把新挑出來的數和之前的都相加 } t[tot++] = x;//存儲這個數 } else { mp[x]--;//這個數之前出現過 所以不需要加了 } } cout<<tot<<endl; for(int i=0;i<tot;i++) { if(i) cout<<" "; cout<<t[i]; } cout<<endl; } return 0; }
hdu 6168 Numbers