题解
1.缺少一个前缀和,缺少在哪了?
如果缺少在 \(i<n\) 的地方,则会出现一个两个数之和,即缺少两个数
否则会只缺少一个数
2.两个数之和可能大于 \(n\),也可能不
3.虽然 \(a_i\) 达到了 \(1e18\) 但是 \(n \leq 2e5\) ,所以可以用数组记录出现的数
code
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll pre[200005] = {0};
ll vis[400005] = {0};
inline void read(ll &x) {
x = 0;
ll flag = 1;
char c = getchar();
while(c < '0' || c > '9'){
if(c == '-') flag = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + (c ^ 48);
c = getchar();
}
x *= flag;
}
inline void write(ll x) {
if(x < 0){
putchar('-');
x = -x;
}
if(x > 9)
write(x / 10);
putchar(x % 10 + '0');
}
void solve()
{
ll n;
read(n);
for(int i=1;i<=2*n;i++) vis[i]=0;
ll sum = 0;
int flag=2;
for(ll i = 1; i < n; i++) {
read(pre[i]);
if(pre[i]-pre[i-1]>n+n-1LL)
{
flag=0;
}
if(!flag) continue;
if(++vis[pre[i] - pre[i - 1]] == 2 || pre[i] - pre[i - 1] > n) {
flag--;
sum = pre[i] - pre[i - 1];
}
}
if(!flag)
{
puts("no");
return;
}
if(!sum)
{
int cnt=0;
for(int i=1;i<=n;i++) cnt+=(vis[i]!=0);
if(cnt==n-1LL) puts("yes");
else puts("no");
return ;
}
ll tem = sum;
for(ll i = 1; i <= n; i++) {
if(!vis[i]) sum -= i;
}
if(!sum) puts("YES");
else puts("NO");
}
int main()
{
ll t;
read(t);
while(t--)
{
solve();
}
return 0;
}
标签:pre,int,ll,Sums,Prefix,while,flag,Permutation,缺少
From: https://www.cnblogs.com/pure4knowledge/p/18253456