题面
The Imprecise Computer (IC) is a computer with some structural issue that it can compare two integers correctly only when their difference is at least two. For example, IC can always correctly answer ‘4 is larger than 2’, but it can answer either ‘2 is larger than 3’ or ‘3 is larger than 2’ (in this case, IC arbitrarily chooses one of them). For two integers x and y, we say ‘x defeats y’ when IC answers ‘x is larger than y’.
Given a positive integer n, let Pn={1,2,…,n} be the set of positive integers from 1 to n. Then we run a double round-robin tournament on Pn using IC. The double-round-robin tournament is defined as follows:
The tournament is composed of two rounds (the 1st round and the 2nd round). For each round, each element in Pn is compared to every other element in Pn. Now for each element k in Pn, let ri(k) be the number of wins of k in the i-th round of the tournament. We also define the ‘difference sequence’ D=d1d2…dn where for each 1≤k≤n, dk=|r1(k)−r2(k)|.
The following shows an example when n=5.
In the example above, r1(1)=0, r1(2)=1, r1(3)=3, r1(4)=3, r1(5)=3, and r2(1)=1, r2(2)=1, r2(3)=1, r2(4)=3, r2(5)=4. Therefore, the difference sequence is D=1 0 2 0 1 in this example.
Given a sequence of n nonnegative integers, write a program to decide whether the input sequence can be a difference sequence of Pn.
Input
Your program is to read from standard input. The input starts with a line containing an integer n, (3≤n≤1,000,000), where n is the size of Pn. In the following line, a sequence of n integers between 0 and n is given, where each element in the sequence is separated by a single space.
Output
Your program is to write to standard output. Print exactly one line. Print YES if the sequence can be the difference sequence of Pn, and print NO otherwise.
Examples
Input
5
1 0 2 0 1
Output
YES
Input
5
1 1 2 1 0
Output
NO
解析
题意
存在一台不精确的计算机只能正确判断差值为 2 的两个整数之间的大小(例如, 可以判断出 4 > 2), 但对于相邻两数则可能会判断出错(例如, 对于 2 和 3, 它判断的结果可能为"3 > 2" 或 “2 > 3”);
现在对于一个整数集 Pn = {1, 2 , …, n}, 其中任意一个元素 k 用该计算机对 Pn 中其他元素进行比较, 得出小于 k 的元素的个数 r(k), 该过程进行两轮, 把 |r1(k) - r2(k)| 的结果 dk 作为两轮的差值,就可以得出 Pn 中 1 - n 的差值序列.
现在给出一组非负整数数列, 判断它是否是一组差值序列.
思路
由于题意中说只有当两数相邻时才会出现判断错误的情况, 并且 1 只与 2 相邻, 那么对于 1, 我们只需要判断它与 2 的判断是否正确;
现在假设存在 Pn = {1, 2} :
.1 当两次判断都正确时 (即都为 2 > 1), d1 = 0 - 0 = 0, d2 = 1 - 1 = 0;
.2 当两次判断都错误时 (即都为 1 > 2), d1 = 1 - 1= 0, d2 = 0 - 0 = 0;
.3 但判断一对一错时, d1 = 1 - 0 = 1, d2 = 1 - 0 = 1;
由以上可以看出, 当两次判断结果相同时 1 和 2 的差值 r(1) 和r(2) 都为 0; 不同时, 都为 1; 因此可以假设: 当减去所有两次判断不同的情况, 其最终序列结果都为 0 时, 则给出的数列是差值序列; 反之则不是.
同时, 还可以看出错误会向后传递, 且两个错误可以相互抵消, 即两次判断不同是错误向后传递 1.
基于此, 我用一个循环依次判断数列 an 的每个项 ak 是否等于 1, 等于 1 就代表 ak 与后一项 a(k+1) 的两次判断结果不同, 即 ak 向 a(k+1) 传递了 1, 于是我就让 a(k+1) 减去 1, 由于 a(k+1) 和 a(k+2) 也可能存在两次判断不同的情况(可能使 a(k+1) = 0 或 a(k+1) = 2), 因此让 a(k+1) = |a(k+1) - 1|, 该过程中每次判断的项 ak 都只等于 0 或 1, 即当判断到 ak > 1时可以直接输出"NO";
使 a(k+1) = 0 或 a(k+1) = 2 的两种情况:
以下时我写的代码:
#include <iostream>
#include <algorithm>
using namespace std;
const int MAXN = 1e6 + 5;
int a[MAXN];
int main ()
{
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i < n; i++)
{
if (a[i] > 1) // 判断是否大于 1 , 大于 1 可直接输出 "NO";
{
a[n] = 1;
break;
}
if (a[i] == 1)
a[i + 1] = abs(1 - a[i + 1]);// 使a[i + 1] = |1 - a[i + 1]|;
}
if (a[n] == 0)//判断最后一项是否变为零, 即删除了全部的两次判断不同的情况;
cout << "YES" <<endl;
else
cout << "NO" << endl;
return 0;
}