1494B - Berland Crossword - CodeForces Solution


bitmasks brute force greedy implementation *1400

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n, U, R, D, L = map(int, input().split())
    for mask in range(16):
        rU, rR, rD, rL = U, R, D, L
        if mask & 1:
            rU -= 1
            rL -= 1
        if mask & 2:
            rL -= 1
            rD -= 1
        if mask & 4:
            rD -= 1
            rR -= 1
        if mask & 8:
            rR -= 1
            rU -= 1
        if min(rU, rR, rD, rL) >= 0 and max(rU, rR, rD, rL) <= n - 2:
            print("YES")
            break
    else:
        print("NO")

C++ Code:

#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define N 200005
using namespace std;
int arr[N];

bool fun(int num, int n, int m, int k){
      int pos = 0;
      bool flag = false;
      int ct = 0;
      for(int i=1; i<=n; i++){
            if(num%i == 0 and num/i<=m)
                  ct++;
            pos += (num/i);}
      if(ct == 0)return (pos+1)>=k;
      return (pos)>=k;
}
int find(int n){
      int ct = 0;
      while(n){
            ct++;
            n = n&(n-1);
      }
      return ct;
}
int32_t main(){
           int t;
           cin>>t;
           
           while(t--){
                 int n,u,d,r,l;
                 cin>>n>>u>>r>>d>>l;
                 bool flag = false;
                 
                 for(int mask=0; mask<16; mask++){
                       int p,q,x,y;
                       p=u;q=d;x=r;y=l;
                       if((mask & (1<<0))){
                             if(p == 0)continue;
                             p--;
                             if(y == 0)continue;
                             y--;
                       }
                        if((mask & (1<<1))){
                             if(p == 0)continue;
                             p--;
                             if(x == 0)continue;
                             x--;
                       }
                        if((mask & (1<<2))){
                             if(q == 0)continue;
                             q--;
                             if(x == 0)continue;
                             x--;
                       }
                        if((mask & (1<<3))){
                             if(q == 0)continue;
                             q--;
                             if(y == 0)continue;
                             y--;
                       }
                       
                       if(p<=n-2 and q<=n-2 and x<=n-2 and y<=n-2)
                        flag = true;
                       
                 }
                 
                 if(flag)          cout<<"YES"<<endl;
                 else cout<<"NO"<<endl;
           }
        }
            // (i,j)
    
   // 1 + 1 + 2 + 4 + 8
//   1 4 -> 


Comments

Submit
0 Comments
More Questions

145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians