E. Imprecise Computer time limit per test 1 second memory limit per
test 512 megabytes input standard input output standard outputThe 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 4In 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 1in 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 nis given, where each element in the sequence is separated by a single
space. OutputYour 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 Copy5 1 0 2 0 1
Output Copy
YES
Input Copy
5 1 1 2 1 0
Output Copy
NO
大佬写的
#include <bits/stdc++.h>
using namespace std;
#define PI cos(-1.)
#define ll long long
const int maxn = 1e7 + 7;
const int mod = 1e9 + 7;
int num[1000007];
int main()
{
int a = 0, b = 0, n;
bool flag = 1;
scanf("%d", &n);
for(int i = 1; i <= n; i ++)scanf("%d", &num[i]);
for(int i = 1; i <= n; i ++)
{
if(num[i] == 1)
if(a == 1) a ^= 1;
else if(b == 1) b ^= 1;
else b ^= 1;
else if(num[i] == 2)
if(!(a ^ b))
{
flag = 0;
printf("NO\n");
break;
}
}
if(flag)
if(a == 0 && b == 0) printf("YES\n");
else printf("NO\n");
return 0;
}
我写的
本来二三十行解决的代码我却用了100来行,惭愧。
#include <stdio.h>
#include<iostream>
#include <stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<map>
using namespace std;
const int maxn=1e6+10;
int d[maxn],r1[maxn][2],r2[maxn][2];
bool judge(int x,int y)
{
if(abs(r1[y][1]+r1[y][0]-r2[y][1]-r2[y][0])==x)
return 1;
return 0;
}
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>d[i];
if(d[1]==0)
{
r1[1][1]=0;
r2[1][1]=0;
}
if(d[1]==1)
{
r1[1][1]=1;
r2[1][1]=0;
}
int flag=0;
for(int i=2;i<=n;i++)
{
r1[i][0]=r1[i-1][1]==0?1:0;
r2[i][0]=r2[i-1][1]==0?1:0;
if(i==n)
{
if(judge(d[i],i))
flag=1;
break;
}
if(d[i+1]==1&&i+1!=n)
{
if(judge(d[i],i))
continue;
else
r1[i][1]=1,r2[i][1]=0;
if(judge(d[i],i))
continue;
else {
r1[i][1]=0,r2[i][1]=1;
if(judge(d[i],i)) continue;
}
}
if(d[i+1]==1&&i+1==n)
{
r1[i][1]=0;
r2[i][1]=1;
if(judge(d[i],i))
continue ;
else {
r1[i][1]=1;
r2[i][1]=0;
if(judge(d[i],i))
continue;
}
}
if(d[i+1]==2)
{
r1[i][1]=0;
r2[i][1]=1;
if(judge(d[i],i))
continue ;
else {
r1[i][1]=1;
r2[i][1]=0;
if(judge(d[i],i))
continue;
}
}
if(d[i+1]==0)
{
if(judge(d[i],i))
continue;
else
r1[i][1]=1,r2[i][1]=0;
if(judge(d[i],i))
continue;
else {
r1[i][1]=0,r2[i][1]=1;
if(judge(d[i],i)) continue;
}
}
break;
}
if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}