1487C - Minimum Ties - CodeForces Solution


brute force constructive algorithms dfs and similar graphs greedy implementation math *1500

Please click on ads to support us..

Python Code:

def main():
    alpha = 'abcdefghijklmnopqrstuvwxyz'
    ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    inf = 1e17
        mod = 10 ** 9 + 7

                                                                                    




    def factorial(n):
        f = 1

        for i in range(1, n + 1):
            f = (f * i) % mod          
        return f

    def factorial_by(n, by):
        f = 1

        for i in range(1, n + 1):
            if i == by:
                continue
            f = (f * i) % mod          
        return f

    def ncr(n, r):
                        num = den = 1
        for i in range(r):
            num = (num * (n - i)) % mod
            den = (den * (i + 1)) % mod
        return (num * pow(den,
                          mod - 2, mod)) % mod

    def primeFactors(num):

        pf = []
        while num % 2 == 0:
            pf.append(2)
            num = num // 2

                        for i in range(3, int(math.sqrt(num)) + 1, 2):

                        while num % i == 0:
                pf.append(i)
                num = num // i

                        if num > 2:
            pf.append(num)

        return pf

    class Node(object):
        def __init__(self, anc, s):
            self.anc = anc
            self.s = s

        def __repr__(self):
            return str(self.s)
            pass

    class DSU:
        def __init__(self, n):
            self.parent = list(range(n))
            self.size = [1] * n
            self.num_sets = n

        def find(self, a):
            acopy = a
            while a != self.parent[a]:
                a = self.parent[a]
            while acopy != a:
                self.parent[acopy], acopy = a, self.parent[acopy]
            return a

        def union(self, a, b):
            a, b = self.find(a), self.find(b)
            if a != b:
                if self.size[a] < self.size[b]:
                    a, b = b, a

                self.num_sets -= 1
                self.parent[b] = a
                self.size[a] += self.size[b]


    def floor(a,b):
        val = a//b
        while val*b > a:
            val -= 1
        return val

    def isprime(num):
        for _ in range(2, int(math.sqrt(num)) + 1):
            if num % _ == 0:
                return False
        return True




    def solve(n):

        results = []
        if n % 2:

            for i in range(n):
                cnt = 0
                for j in range(n-1-i):
                    if cnt < (n-1)//2:
                        results.append(1)
                    else:
                        results.append(-1)
                    cnt += 1

            pass
        else:

            for i in range(n):
                cnt = 0
                for j in range(n-1-i):
                    if cnt < (n-2)//2:
                        results.append(1)
                    elif cnt == (n-2)//2:
                        results.append(0)
                    else:
                        results.append(-1)
                    cnt += 1

            pass

        return " ".join(list(map(str,results)))



































    
    t = int(input())
    ans = []
    for _ in range(t):
        
        n = int(input())

                
        
                
                                        
                                                                        

        ans.append(solve(n))

    p = 1
    for answer in ans:
        #        print(answer)
        p += 1


if __name__ == "__main__":
    import sys, threading
    import bisect
    import math
    import itertools
    from sys import stdout
    from collections import defaultdict,deque
    from heapq import heappush, heappop


        import heapq
    from queue import PriorityQueue


            
    input = sys.stdin.readline
    thread = threading.Thread(target=main)
    thread.start()
    thread.join()

C++ Code:

// Problem: C. Minimum Ties
// Contest: Codeforces - Educational Codeforces Round 104 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1487/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define bit(x) (1LL << (x))
#define lowbit(x) (x & (-x))
#define SQU(x) ((x) * (x))
#define ls id << 1
#define rs id << 1 | 1
//#define int long long
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;
const int inv2 = (mod + 1) / 2;
ll qpow(ll x,int y) {
    ll res = 1;
    while(y) {
        if(y & 1) res = res * x % mod;
        x = x * x % mod;
        y >>= 1;
    }
    return res;
}
void add(int &x,int y) {
    x += y;
    if(x >= mod) x -= mod;
}
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
void solve() {
	int n;cin >> n;
	if(n % 2) {
		vector <int> a(n,n / 2);
		for(int i = 0;i < n;i++) {
			for(int j = i + 1;j < n;j++) {
				if(a[i]) {
					cout << "1 ";
					a[i]--;
				}else {
					cout << "-1 ";
					a[j]--;
				}
			}
		}
		cout << '\n';
	}else {
		vector <vector<int>> ans(n,vector <int> (n));
		for(int i = 0;i < n;i++) {
			int t = (n - 1) / 2;
			for(int j = 1;j <= t;j++) ans[i][(i + j) % n] = -1;
			for(int j = t + 2;j <= 2 * t + 1;j++) ans[i][(i + j) % n] = 1;
		}
		//for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) cout << ans[i][j] << " \n"[j == n - 1];
		for(int i = 0;i < n;i++) for(int j = i + 1;j < n;j++) cout << ans[i][j] << ' ';
		cout << '\n';
	}
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    int T = 1;
    cin >> T;
    while(T--) {
        solve();
    }
    return 0;
}
/*
*/


Comments

Submit
0 Comments
More Questions

1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset