danielvitor23

Prefix Permutation Sums

Jul 25th, 2023
1,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. using namespace std;
  5.  
  6. using i64 = long long;
  7.  
  8. void solve() {
  9.   int n; cin >> n;
  10.  
  11.   vector<i64> a(n-1);
  12.   map<int, int> freq;
  13.  
  14.   for (int i = 0; i < n-1; ++i) {
  15.     cin >> a[i];
  16.     ++freq[i == 0 ? a[i] : a[i] - a[i-1]];
  17.   }
  18.  
  19.   // verificar se removeu do fim
  20.   vector<int> faltando;
  21.  
  22.   int cntFreq = 0, hasRepeated = false;
  23.   for (int i = 1; i <= n; ++i) {
  24.     if (freq[i]) {
  25.       if (freq[i] > 1) hasRepeated = true;
  26.       ++cntFreq;
  27.     }
  28.     else {
  29.       faltando.push_back(i);
  30.     }
  31.   }
  32.  
  33.   if (cntFreq == n-1 and !hasRepeated) {
  34.     cout << "YES\n";
  35.     return;
  36.   }
  37.  
  38.   // verificar se removeu do começo
  39.   if (cntFreq == n-2 and faltando.size() == 2 and faltando[0] + faltando[1] == a[0] and !hasRepeated) {
  40.     cout << "YES\n";
  41.     return;
  42.   }
  43.  
  44.   // verificar se removeu do meio
  45.   if (cntFreq == n-2 and faltando.size() == 2 and (faltando[0] + faltando[1] <= n ? freq[faltando[0] + faltando[1]] == 2 : freq[faltando[0] + faltando[1]] == 1)) {
  46.     cout << "YES\n";
  47.     return;
  48.   }
  49.  
  50.   cout << "NO\n";
  51. }
  52.  
  53. int main() {
  54.   cin.tie(0)->sync_with_stdio(0);
  55.  
  56.   int tc; cin >> tc;
  57.  
  58.   while (tc--) {
  59.     solve();
  60.   }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment