Language: HTML document.onkeydown=function (e){ var currKey=0,evt=e||window.event; currKey=evt.keyCode||evt.which||evt.charCode; if (currKey == 123) { window.event.cancelBubble = true; window.event.returnValue = false; } }

Codeforces Round #644 (Div. 3) 解题报告

Codeforces Round #644 (Div. 3)

原文链接 https://www.cnblogs.com/-Dominate-/p/13539705.html

A. Minimal Square

思路

#include<bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
// inline int read()
// {
//     int x=0,f=1;char ch=getchar();
//     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
//     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
//     return x*f;
// }
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	IO;
    int t;cin>>t;
    while(t--)
    {
        int a,b;cin>>a>>b;
        if(a>b)swap(a,b);
        int tmp=max(a*2,b);
        cout<<tmp*tmp<<"\n";
    }
    return 0;
}

B. Honest Coach

思路

排序后取最小间隔

#include<bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
int a[100];
// inline int read()
// {
//     int x=0,f=1;char ch=getchar();
//     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
//     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
//     return x*f;
// }
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	IO;
    int t;cin>>t;
    while(t--)
    {
       int n;cin>>n;
       for(int i=1;i<=n;i++)cin>>a[i];
       sort(a+1,a+1+n);
       int minn=1e8;
       for(int i=2;i<=n;i++)minn=min(minn,a[i]-a[i-1]);
       cout<<minn<<"\n"; 
    }
    return 0;
}

C. Similar Pairs

思路

分类讨论,分别记录奇数和偶数的个数,若两者个数的奇偶性不相等则直接输出NO,两者个数都为偶数直接输出YES,两者个数都为奇数则排序后跑一遍数组寻找有无相邻的数(差为1),只要能找到一对则可用题中另一种配对操作将其转化为两者个数都为偶数的情况,输出YES;一对都找不到则输出NO。

#include<bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
int a[60];
// inline int read()
// {
//     int x=0,f=1;char ch=getchar();
//     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
//     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
//     return x*f;
// }
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	IO;
    int t;cin>>t;
    while(t--)
    {
        int n;cin>>n;
        int cnt1=0,cnt2=0;
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            a[i]%2==0?cnt1++:cnt2++;
        }
        sort(a+1,a+1+n);
        if(cnt1%2==0&&cnt2%2==0){cout<<"YES\n";continue;}
        else if(cnt1%2!=cnt2%2){cout<<"NO\n";continue;}
        int f=0;
        for(int i=2;i<=n;i++)
            if(a[i]-a[i-1]==1){f=1;break;}
        if(f)cout<<"YES\n";
        else cout<<"NO\n";
    }
    return 0;
}

D. Buying Shovels

思路

题意就是给出一个n,让你找出n在1k中最大的因数。一开始我看数据范围好像不太能暴力写了个素数筛来分解,WA了一发之后发现素数筛只能处理素数因子,于是敲了暴力就过了。。。(其实就是没好好分析复杂度)~

#include<bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
// inline int read()
// {
//     int x=0,f=1;char ch=getchar();
//     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
//     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
//     return x*f;
// }
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	//IO;
    //cout<<"gg";
    int t;cin>>t;
    while(t--)
    {
        int n,k;cin>>n>>k;
        if(n<=k){cout<<"1\n";continue;}
        int ans=n;
        for(int i=2;i<=k&&i*i<=n;i++)
        {
            if(n%i==0)
            {
                if(n/i<=k){ans=i;break;}
                ans=n/i;
            }
        }
        cout<<ans<<"\n";
    }
    return 0;
}

E. Polygon

思路

读完题之后观察一会,发现所有的1要么靠着最右边或者最下边,要么右边或者下边有另一个1,然后跑一遍图对所有1判这个条件就行了。

#include<bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
// inline int read()
// {
//     int x=0,f=1;char ch=getchar();
//     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
//     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
//     return x*f;
// }
char s[60][60];
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	IO;
	int t;cin>>t;
	while(t--)
	{
		int n,f=0;cin>>n;
		for(int i=0;i<n;i++)cin>>s[i];
		for(int i=0;i<n;i++)
			for(int j=0;j<n;j++)
				if(s[i][j]=='1')
				{
					if(i==n-1||j==n-1)continue;
					if(s[i+1][j]=='1'||s[i][j+1]=='1')continue;
					f=1;
				}
		if(f)cout<<"NO\n";
		else cout<<"YES\n";
	}
    return 0;
}

F. Spy-string

思路

看了一眼数据范围果断怎么暴力怎么来,于是就取了第一串当板子,然后枚举,每一位改成a~z之后与所有串比对能否满足条件,稳过。

#include<bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
// inline int read()
// {
//     int x=0,f=1;char ch=getchar();
//     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
//     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
//     return x*f;
// }
char s[60][60];
char ss[60],ans[60];
int n,m;
bool finddiffer()
{
    int ff=0;
    for(int i=1;i<=n;i++)
    {
        int cnt=0;
        for(int j=0;j<m;j++)
            if(s[i][j]!=ss[j])cnt++;
        if(cnt>1){ff=1;break;}
    }
    if(ff)return false;
    return true;
}
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	IO;
    int t;cin>>t;
    while(t--)
    {
        memset(ss,0,sizeof(ss));
        int f=0;cin>>n>>m;
        for(int i=1;i<=n;i++)cin>>s[i];
        for(int i=0;i<m;i++)ss[i]=s[1][i];
        //cout<<ss<<'\n';
        if(finddiffer()){cout<<ss<<"\n";continue;}
        for(int i=0;i<m;i++)
        {
            char tmp=ss[i];
            for(int j=0;j<26;j++)
            {
                ss[i]=char(j+'a');
                if(finddiffer()){f=1;break;}
            }    
            if(f)break;
            ss[i]=tmp;
        }
        if(f)cout<<ss<<"\n";
        else cout<<"-1\n";
    }
    return 0;
}

G. A/B Matrix

思路

一开始想简单了直接无脑放,不出意外WA2,仔细想了想发现是一个类似于轮换串的东西。首先1的总数算两次要满足条件,所以an=bm必须要满足,否则直接输出NO,然后就是需要进行挪动操作,可以对着下面这个例子看:

1  
17 17 13 13  
YES  
11111111111110000  
01111111111111000  
00111111111111100  
00011111111111110  
00001111111111111  
10000111111111111  
11000011111111111  
11100001111111111  
11110000111111111  
11111000011111111  
11111100001111111  
11111110000111111  
11111111000011111  
11111111100001111  
11111111110000111  
11111111111000011  
11111111111100001  

这样看下来就有点轮换那味了,n个位移,刚好到第m列的时候是一个轮回。假设位移为dis,(dis*n)%m==0。从小到大枚举,求dis。
(此处有参考知乎ZqIceberg大神的题解,传送门)

#include<bits/stdc++.h>
#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
// inline int read()
// {
//     int x=0,f=1;char ch=getchar();
//     while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
//     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
//     return x*f;
// }
int g[60][60];
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	IO;
    int t;cin>>t;
    while(t--)
    {
        memset(g,0,sizeof(g));
        int n,m,a,b;cin>>n>>m>>a>>b;
        if(a*n!=b*m){cout<<"NO\n";continue;}
        int dis=1;
        while(dis*n%m!=0)dis++;
        //cout<<"dis="<<dis<<"\n";
        for(int i=0,del=0;i<n;i++,del+=dis)
            for(int j=0;j<a;j++)
                g[i][(j+del)%m]=1;
        cout<<"YES\n";
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                cout<<g[i][j];
            cout<<"\n";
        }
    }
    return 0;
}

H. Binary Median

待补。_

posted @ 2020-08-21 10:32  DDDOMINATE  阅读(166)  评论(0)    收藏  举报