icpc:problems:usaco22dec_range_reconstruction_s
problems | |
---|---|
名称 | Range Reconstruction S |
题目编号 | USACO22DEC_S3 |
题目链接 | luogu.com.cn/… |
来源 | USACO |
算法分类 | 构造 |
难易程度 | 容易 |
Range Reconstruction S
想法
r数组很有特点,r[i][i+1]可以得出第i个数与第i+1个数的差,而对于差来说,只有+与-的关系。所以枚举加减,使用之前的r进行判断。从而得出第i个数。
代码实现
#include<iostream> #include<cstring> using namespace std; const int N = 305; int r[N][N], ans[N], mx[N], mi[N]; int main() { int n; cin >> n; for (int i = 0; i < n; ++i) for (int j = i; j < n; ++j) cin >> r[i][j]; for (int i = 0; i < n; ++i) { mx[i] = -1e9; mi[i] = 1e9; } ans[0] = 0; mx[0] = 0; mi[0] = 0; for (int i = 1; i < n; ++i) { int temp = ans[i-1] + r[i-1][i]; bool flag = true; for (int j = 0; j < i; ++j) { if(max(mx[j], temp) - min(mi[j], temp) != r[j][i]) { flag = false; break; } } if(!flag) temp = ans[i-1] - r[i-1][i]; for (int j = 0; j <= i; ++j) { mx[j] = max(mx[j], temp); mi[j] = min(mi[j], temp); } ans[i] = temp; } for (int i = 0; i < n; ++i) cout << ans[i] << ' '; return 0; }
/app/www/public/data/pages/icpc/problems/usaco22dec_range_reconstruction_s.txt · 最后更改: 2023/02/13 13:33 由 温婕莺