kingofworls's blog

By kingofworls, history, 4 months ago, In English

link to the problem https://codeforces.net/contest/1915/problem/E

my code

include <bits/stdc++.h>

using namespace std;

define int long long

signed main() { int t; cin >> t; while (t--) { int n; cin >> n; std::vector a(n+1);

unordered_map<int, int> sum_map;
    sum_map[0] = 1;  
    bool flag=false;
    int sum=0;

    for(int i = 1; i <= n; i++){
      cin >> a[i];
      if(i%2==0){
          a[i]=-a[i];
      }
      sum=sum+a[i];
      sum_map[sum]++;
      if(sum_map[sum]>1){
        flag =true;
        break;
      }
    }

    if(flag){
        cout << "YES\n";
    } else {
        cout << "NO\n";
    }
}

} why is tle coming in this and one more question when i am using the break in ok=true there as its been done why is still false coming ???? please help me

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
4 months ago, # |
  Vote: I like it 0 Vote: I do not like it

swap unordered_map to map

  • »
    »
    4 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    but tle is less in unordered_map right..

    • »
      »
      »
      4 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      no, unordered_map is O(N) worst case and oftentimes on cf there are cases designed to trigger its worst case

      also even if that doesn't happen using unordered_map is a bad idea in cp because it's not that much faster than map + map offers a lot of other benefits like being sorted

      • »
        »
        »
        »
        4 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        okay thank you .. but when i am using break after flag =true why is answer coming false still??