Advertisement
Derga

Untitled

Jul 27th, 2023
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. https://codeforces.com/contest/1851/problem/D
  2.  
  3.  
  4. #include<bits/stdc++.h>
  5. using namespace std;
  6. #define ll long long
  7. int main()
  8. {
  9.     ll i,j,k,t,n,a[200005];
  10.    
  11.     cin>>t;
  12.     while(t--)
  13.     {
  14.         cin>>n;set<ll> b;
  15.         ll s=(1+n)*n/2;
  16.         ll x,l=0;
  17.         for(i=1;i<n;i++)
  18.         {
  19.             cin>>x;
  20.             if(x-l<=n)
  21.                 b.insert(x-l);
  22.             l=x;
  23.         }
  24.         if(b.size()==n-2+(x!=s))
  25.             cout<<"YES"<<endl;
  26.         else
  27.             cout<<"NO"<<endl;
  28.     }
  29.     return 0;
  30. }
  31.  
  32.  
  33.  
  34.  
  35. #include <bits/stdc++.h>
  36. using namespace std;
  37.  
  38. using ll = long long;
  39.  
  40. int main()
  41. {
  42.     ios::sync_with_stdio(false);
  43.     cin.tie(nullptr);
  44.     int tests;
  45.     cin >> tests;
  46.     while (tests--) {
  47.         ll n;
  48.         cin >> n;
  49.         --n;
  50.         vector<ll> a(n);
  51.         for (ll &i: a) {
  52.             cin >> i;
  53.         }
  54.         vector<ll> numbers(1, a[0]);
  55.         for (int i{1}; i < n; ++i) {
  56.             numbers.push_back(a[i] - a[i - 1]);
  57.         }
  58.         ++n;
  59.         vector<bool> occurred(1 + n, false);
  60.         vector<ll> unexpected_numbers;
  61.         for (auto i: numbers) {
  62.             if (1 <= i && i <= n) {
  63.                 if (occurred[i]) {
  64.                     unexpected_numbers.push_back(i);
  65.                 } else {
  66.                     occurred[i] = true;
  67.                 }
  68.             } else {
  69.                 unexpected_numbers.push_back(i);
  70.             }
  71.         }
  72.         vector<ll> missing_numbers;
  73.         for (int i{1}; i <= n; ++i) {
  74.             if (!occurred[i]) {
  75.                 missing_numbers.push_back(i);
  76.             }
  77.         }
  78.         if (missing_numbers.size() == 1u) {
  79.             ll sum = n * (n + 1) / 2;
  80.             if (a.back() < sum && missing_numbers[0] == sum - a.back()) {
  81.                 cout << "YES\n";
  82.             } else {
  83.                 cout << "NO\n";
  84.             }
  85.             continue;
  86.         }
  87.         if (
  88.             unexpected_numbers.size() == 1u
  89.             && missing_numbers.size() == 2u
  90.             && unexpected_numbers[0] == missing_numbers[0] + missing_numbers[1]
  91.         ) {
  92.             cout << "YES\n";
  93.         } else {
  94.             cout << "NO\n";
  95.         }
  96.     }
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement