1598B - Groups - CodeForces Solution


implementation *1000

Please click on ads to support us..

Python Code:

class Solution:
    def groups(self, arr):
        
        for i in range(5):
            for j in range(i+1, 5, 1):
 
                count1 = 0
                count2 = 0
                count0 = 0
                for k in range(len(arr)):
                    if arr[k][i] ==1:
                        count1+=1
                    if arr[k][j] == 1:
                        count2+=1
                    if arr[k][i] == 0 and arr[k][j] == 0:
                        count0+=1
                        
                if count1>= len(arr)//2 and count2>= len(arr)//2 and count0 == 0:
                    return True
        return False
                        
                        
t = int(input())
obj = Solution()
 
for _ in range(t):
    n = int(input())
    arr = []
    for i in range(n):
        k = list(map(int, input().split()))
        arr.append(k)
 
    if obj.groups(arr):
        print("YES")
    else:
        print("NO")
            

C++ Code:

// JAI MATA DI
 
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <string>
# define LL22 long long
# define VI22 vector<int>
# define M22 map<int, int>
# define PII22 pair<int, int>
# define VII22 vector<PII22>
# define NV22(i, a, b) for(int i=a; i<b; i++)
# define F22 first 
# define S22 second 
# define setBits(x) builtin_popcount(x)



 
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);
    LL22 t22;
    cin>>t22;
    while(t22--){
        int n;
        cin>>n;
        int a[n][5];
        for(int i=0; i<n; i++){
            for(int j=0; j<5; j++){
                cin>>a[i][j];
            }
        }
        string ans="No";
        for(int i=0; i<5; i++){
            for(int j=0; j<5; j++){
                if(i!=j){
                    int d1=i, d2=j, c1=0, c2=0, c=0;
                    for(int k=0; k<n; k++){
                        if(a[k][d1]==1) c1++;
                         if(a[k][d2]==1) c2++;
                         if(a[k][d1]==0&&a[k][d2]==0) c++;
                    }
                    if(c1>=n/2 and c2>=n/2 and c==0){
                        ans="Yes";
                        break;
                    }
                }
            }
            if(ans=="Yes") break;
        }
		
   cout<<ans<<endl;

    }
	return 0;
}

//NEERAJ VERMA


Comments

Submit
0 Comments
More Questions

1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem
52A - 123-sequence
1543A - Exciting Bets
1714D - Color with Occurrences
215B - Olympic Medal
1445A - Array Rearrangment
1351A - A+B (Trial Problem)
935B - Fafa and the Gates
1291A - Even But Not Even