Codeforces Round #632 (Div. 2)(A+B)

Posted by Possum on Fri, 10 Apr 2020 16:30:25 +0200

A. Little Artem

Question meaning: this question is relatively simple. To put it simply, I want to give you a matrix of n*m, in which "B" stands for black and "W" stands for white. It is required that both of them meet the equation: B=W+1.

Problem solving: it's a simple sign in problem, but I used a complex method in the competition, so it took a long time. After the competition, I saw someone else's solution. It's really the best.

Code:

Law 1:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int nk_max = 8 + 5;
const int INF = 0x3fffffff;
int main() {
    int t;
    int n,m;
    cin>>t;
    while(t--) {
        int B,W;
        cin>>n/*Row number*/>>m/*Column number*/;
        int tot=n*m;
        char ptr[150][150];
        if(tot%2==0) {
            B=tot/2;
            W=B-1;
        } else {
            B=(tot/2)+1;
            W=tot-B;
        }
        //Coloring
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=m; j++) {
                if(i%2!=0&&j%2==0) {
                    ptr[i][j]='W';
                }
                if(i%2!=0&&j%2!=0) {
                    ptr[i][j]='B';
                }
                if(i%2==0&&j%2!=0) {
                    ptr[i][j]='W';
                }
                if(i%2==0&&j%2==0) {
                    ptr[i][j]='B';
                }
            }
        }
//        cout<<B<<" "<<W<<"==="<<endl;
        if(tot%2==0) {//Judge parity, if it's even 
            if(ptr[n][m]=='B') {
                ptr[n][m-1]='B';
            } else {
                ptr[n][m]='B';
            }
        }
        for(int i=1; i<=n; i++) {
            for(int j=1; j<=m; j++) {
                cout<<ptr[i][j];
            }
            cout<<endl;
        }
    }
    return 0;
}

Law two:

The idea is simple, and the code is simple;

#include <bits/stdc++.h>
using namespace std;

signed main() {
    ios::sync_with_stdio(false);
    int t,n,m;
    cin>>t;
    while(t--) {
        cin>>n>>m;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                if(i==1 && j==1) cout<<"W";
                else cout<<"B";
            }
            cout<<endl;
        }
    }
}

Reprinted from: https://www.cnblogs.com/mollnn/p/12664323.htmlB. Kind AntonB. Kind AntonB. Kind Anton

B. Kind Anton

There are two strings for you. A only includes - 1, 0, 1. There is no limit to the composition of b array. First select i, j, where i < j, operate a array, aj=ai+aj. Note that at this time (i < j), now you need to judge whether a array can be changed to array a by changing.

Solution: if we do this problem by simple simulation, it will time out. Here we use map to record the times of each element in a, and then we traverse from back to front to judge each element, and see the code for the specific implementation.

#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
int a[100010]={0};
int b[100010]={0}; 
using namespace std;;
int main(){
    int t,n;
    cin>>t;
    map<int,int> mp;
    while(t--){
        mp.clear();
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            cin>>a[i];
            mp[a[i]]++;
        }
        for(int i=1;i<=n;i++){
            cin>>b[i];
        }//After data input, start to process data
        int f=1;
        for(int i=n;i>=1;i--){
            mp[a[i]]--;
            if(a[i]==b[i]){
                continue;
            }
            if(a[i]<b[i]){
                if(!mp[1]){
                    f=0;
                    break;
                }
            }
            if(a[i]>b[i]){
                if(!mp[-1]){
                    f=0;
                    break;
                }
            }
        } 
        if(f){
            cout<<"YES"<<endl;
        }else{
            cout<<"NO"<<endl; 
        }
    }
    return 0;
}

Topics: C++ iOS