题意:...
析:我们可以知道,a1+a2=b1,那么我们可以枚举a1,那么a2就有了,并且a1+a3=b2,所以a3就有了,我们再从把里面的剩下的数两两相加,并从b数组中去掉,
那么剩下的最小的就是a4,然后依次可以求出a5,a6....由于a最大才是5000,并且保证有唯一解,那么找到一个就直接退出。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <unordered_map> #include <unordered_set> #define debug() puts("++++"); #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e4 + 5; const int mod = 2000; const int dr[] = {-1, 1, 0, 0}; const int dc[] = {0, 0, 1, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int b[maxn], c[maxn]; int a[maxn]; bool judge(int x){ a[1] = x; a[2] = b[1] - a[1]; int cnt = 2; for(int i = 2; i <= m; ++i){ if(b[i]) a[++cnt] = b[i] - a[1]; else continue; for(int j = 2; j < cnt; ++j){ bool ok = false; for(int k = i+1; k <= m; ++k) if(a[j] + a[cnt] == b[k]){ b[k] = 0; ok = true; break; } if(!ok) return ok; } } return true; } int main(){ while(scanf("%d", &n) == 1 && n){ m = n * (n-1) / 2; for(int i = 1; i <= m; ++i) scanf("%d", c+i); for(int i = 1; ; ++i){ memcpy(b+1, c+1, 4*m); if(judge(i)) break; } for(int i = 1; i <= n; ++i) if(i == 1) printf("%d", a[i]); else printf(" %d", a[i]); printf("\n"); } return 0; }
时间: 2024-11-04 16:23:13