1400D - Zigzags - CodeForces Solution


brute force combinatorics data structures math two pointers *1900

Please click on ads to support us..

Python Code:

import os,sys
from random import randint, shuffle
from io import BytesIO, IOBase

from collections import defaultdict,deque,Counter
from bisect import bisect_left,bisect_right
from heapq import heappush,heappop, heapify
from functools import lru_cache, reduce
from itertools import accumulate, permutations
import math, copy

sys.setrecursionlimit(2*10**5+5)

BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

MOD = 998244353  



def main():
    def solve():
        n = int(input())
        arr = list(map(int, input().split()))
        cc = Counter()
        ans = 0
        for i in range(n):
            delta = 0
            for j in range(i+1,n):
                if arr[i] == arr[j]:
                    ans += delta 
                delta += cc[arr[j]]
            cc[arr[i]] += 1
        print(ans)
        return  

        


            t = int(input())
        for i in range(t):
                solve() 
                                                

    

if __name__ == "__main__":
    main()


C++ Code:

#include<bits/stdc++.h>

#define ll long long
#define pp push_back
#define endl '\n'
#define all(x) x.begin(),x.end()
#define ld long double
#define PI acos(-1)
#define sin(a) sin((a)*PI/180)
#define cos(a) cos((a)*PI/180)
#define ones(x) __builtin_popcountll(x)
//#define int ll

#define Drakon  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;


unsigned long long inf = 1e10;
const double EPS = 1e-6;
const int MOD = 1000000007, N = 200005, LOG = 25, M = 2;

typedef array<array<int, M>, M> matrix;

ll gcd(ll x, ll y) {
    return y ? gcd(y, x % y) : x;
}

ll lcm(ll a, ll b) {
    return (a * b) / __gcd(a, b);
}

void solve() {
    int n;
    cin >> n;
    int arr[n];
    vector<int>ind[n + 1];
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
        ind[arr[i]].pp(i);
    }
    ll tmp[n][n];
    for (int i = 0; i < n; ++i) {
        for (int j = i + 2; j < n; ++j) {
            tmp[i][j] = (int)ind[arr[i]].size() - (lower_bound(all(ind[arr[i]]),j) - ind[arr[i]].begin());
        }
    }
    for (int j = 2; j < n; ++j) {
        for (int i = 1; i <= j - 2; ++i) {
            tmp[i][j] += tmp[i - 1][j];
        }
    }

    ll ans = 0;
    for (int i = 0; i < n; ++i) {
        for (int k = i + 2; k < n - 1; ++k) {
            if(arr[i] != arr[k])continue;
            ll add = tmp[k - 1][k + 1] - tmp[i][k + 1];
            ans += add;
        }
    }
    cout << ans << endl;
    
}

signed main() {
    Drakon
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
}


Comments

Submit
0 Comments
More Questions

148A - Insomnia cure
1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters