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 round2 defeats 13 defeats 14 defeats 15 defeats 13 defeats 24 defeats 25 defeats 25 defeats 33 defeats 44 defeats 52nd round3 defeats 14 defeats 15 defeats 11 defeats 24 defeats 25 defeats 22 defeats 34 defeats 35 defeats 35 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

解析

题意

存在一台不精确的计算机只能正确判断差值为 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本项目是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。该系统主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的Java学习者,包含项目源码、数据库脚本、项目说明等,有论文参考,可以直接作为毕设使用。 后台框架采用SpringBoot,数据库使用MySQL,开发环境为JDK、IDEA、Tomcat。项目经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。 该系统的功能主要包括商品管理、订单管理、用户管理等模块。在商品管理模块中,可以添加、修改、删除商品信息;在订单管理模块中,可以查看订单详情、处理订单状态;在用户管理模块中,可以注册、登录、修改个人信息等。此外,系统还提供了数据统计功能,可以对销售数据进行统计和分析。 技术实现方面,前端采用Vue框架进行开发,后端使用SpringBoot框架搭建服务端应用。数据库采用MySQL进行数据存储和管理。整个系统通过前后端分离的方式实现,提高了系统的可维护性和可扩展性。同时,系统还采用了一些流行的技术和工具,如MyBatis、JPA等进行数据访问和操作,以及Maven进行项目管理和构建。 总之,本系统是一个基于SpringBoot开发的华府便利店信息管理系统,使用了Vue和MySQL作为前端框架和数据库。系统经过严格调试,确保可以运行。如果基础还行,可以在代码基础之上进行改动以实现更多功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值