Gym - 102920E

E. Imprecise Computer


本题思路和代码借鉴于大佬
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.

1st round 2nd round 2 defeats 1 3 defeats 1 3 defeats 1 4 defeats 1 4 defeats 1 5 defeats 1 5 defeats 1 1 defeats 2 3 defeats 2 4 defeats 2 4 defeats 2 5 defeats 2 5 defeats 2 2 defeats 3 5 defeats 3 4 defeats 3 3 defeats 4 5 defeats 3 4 defeats 5 5 defeats 4
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
题意
不精确计算机(IC)是一种具有某些结构问题的计算机,它只能在两个整数的差值至少为两个时,才能正确地比较两个整数。例如,IC总能正确回答“4大于2”,但它可以回答“2大于3”或“3大于2”(在这种情况下,IC任意选择其中一个)。对于两个整数x和y,当IC回答“x大于y”时,我们说“x打败y”。
给定一个正整数n,设Pn={1,2,…,n}是从1到n的正整数集。然后我们使用IC在Pn上运行一个双循环锦标赛。双循环赛的定义如下:
比赛分两轮(第一轮和第二轮)。对于每一轮,Pn中的每个元素与Pn中的其他元素进行比较。现在对于Pn中的每个元素k,让ri(k)是k在锦标赛第i轮中获胜的次数。我们还定义了“差序列”D=d1d2…dn,其中对于每个1≤k≤n,dk=| r1(k)−r2(k)|。
给定一个由n个非负整数组成的序列,编写程序判断输入序列是否为Pn的差分序列。
题目思路
由于不精确计算机(IC)可能出现类似1 > 2 的情况 并且这种错误会影响到下一个数的d(k)判断,所以我们的程序就是要判断出出错的地方并消除IC所判断的误差(将误差向后移动),最后判断当前位置的值是否符合题意即可。
若两轮相邻的两个数都判错(类似1>2这种错误)则误差会消除,则d(k)=0;不会影响差分序列的判断。
由题意得a[1]不可能为2,如果为2则输入序列一定不是为Pn的差分序列。
若a[ i ] = 2 ,( i ! = 1)则出现了类似
1>2<3
1<2>3 的情况
由于

if(a[i]==1){
			if(a[i+1]) a[i+1]--; 
			else a[i+1]++;
		}

在程序进行到a [ i ]的时候a [ i ]已经被减成了1,以此类推,一直到a [ n ],最后进行判断。

#include <iostream>
using namespace std;
int a[1000001];
int main(){
	int n,i;
	cin>>n;
	for(i=1;i<=n;i++)	cin>>a[i];
	for(i=1;i<n;i++){
		if(a[i]>1){ cout<<"NO"<<endl;	return 0;}
		if(a[i]==1){
			if(a[i+1]) a[i+1]--; //本来a[i]也要-1,但i会往后递增,对最终结果的判断没有影响,所以没有这个必要
			else a[i+1]++;
		}
	}
	if(a[n]==0) cout<<"YES"<<endl;
	else cout<<"NO"<<endl;
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值