Codeforces gym 102920E. Imprecise Computer 题解
题解
我们首先来看如果 \(x,x+1\) 被错误地判断为 \(x>x+1\) 时会发生什么。
假设是第 \(1\) 次实验,此时 \(r_1(x)\to r_1(x)+1, r_1(x+1)\to r_1(x+1)-1\),会产生一正一负的影响,考虑第 \(2\) 次实验时同理。我们每次考虑数对 \((x,x+1)\),根据给出的 \(D_i\),我们知道了当前的差分 \(|r_1(x)-r_2(x)|\),根据刚才的叙述,我们要把当前位变为 \(0\)(假如计算机正常时的输出的差分应当全部为 \(0\)),如果当前给出的差分不是 \(0\),如果是 \(1\),则对应改变对数 \(i-1,i+1\) 的影响,由于是从前往后依次处理的,如果处理后当前位仍不能为 \(0\),即处理之前就已经有 \(d_i\geq 2\),则为非法序列。
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <cstdio> #include <cctype> #include <algorithm> #include <cstring> #include <iostream> #include <cassert> #include <cmath> using namespace std; typedef long long ll; template <typename _Tp> void read(_Tp &a, char c = 0, int f = 1) { for(c = getchar(); !isdigit(c); c = getchar()) if(c == '-') f = -1; for(a = 0; isdigit(c); a = a * 10 + c - '0', c = getchar()); a *= f; } template <typename _Tp> void write(_Tp a) { if(a < 0) putchar('-'), a = -a; if(a > 9) write(a / 10); putchar(a % 10 + '0'); }
const int N = 1e6 + 5; int n, d[N];
int main() { read(n); for(int i = 1; i <= n; i++) { read(d[i]); } bool f = 1; for(int i = 1; i < n; i++) { if(d[i] >= 2) { f = 0; break; } if(d[i] == 1) { if(d[i + 1]) d[i + 1]--; else d[i + 1]++; if(d[i - 1]) d[i - 1]--; else d[i - 1]++; } } if(d[n] != 0) f = 0; puts(f ? "YES" : "NO"); return 0; }
|