Codeforces Round #644 (Div. 3)—CodeForces1360

Codeforces Round #644 (Div. 3)—CodeForces1360

1360D-Buying Shovels

https://codeforces.com/problemset/problem/1360/D

Problem:

​ 商店里有k个包裹,第i个包裹中含i个物品,现在想要买n物品,你可以选择某一个包裹购买任意次,使得物品数刚好等于n,求最少的购买次数

Solution:

首先,假如k≥n,那么只用买一次。

k<n,枚举1~√n

1.n/i≤k,那么i一定是我们所需要的最小购买次数,

2.n/i>k,只要i≤k,我们就维护ans和n/i的最小值.

Code:

#include<bits/stdc++.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
inline ll read() {
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int maxn=1e6+5;
const ll mod=1e9+7;
int t;
ll n,k,ans;
int main() {
	t=read();
	while(t--){
		n=read(),k=read();
		ans=n;
		if(k>=n) ans=1;
		else{
			for(ll i=min((ll)sqrt(n),k);i>=1;i--){
				if(n%i==0){
					if(n/i<=k) ans=min(ans,min(i,n/i));
					else ans=min(ans,n/i);
				}
			}
		}
		cout<<ans<<endl;
	}	
}

1360E-Polygon

https://codeforces.com/problemset/problem/1360/E

Problem:

​ 一个n*n的方阵,每行每列都有一个大炮在上方和左方。方阵中全是0,炮弹发射数字1的炮弹。但是现在如果这个炮弹会一直向前发现,直到越到前面是方阵的边界后停下,或者他的前面一个数是1,则也停下,问给定方阵是否合法

Solution:

​ 假设一个炮弹停在了a[i][j]位置上,那么a[i+1][j]或a[i][j+1]这两个位置上必定有一个位置上有炮弹,枚举判断即可。

Code:

#include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define PI acos(-1)
using namespace std;
typedef long long ll;
inline ll read() {
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int maxn=1e6+5;
const ll mod=1e9+7;
int t;
int n;
char a[55][55];
int main() {
	t=read();
	while(t--) {
		n=read();
		bool flag=true;
		for(int i=0;i<n;i++) scanf("%s",a[i]);
		for(int i=0;i<n-1;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]=='1'){
					if(a[i+1][j]=='0'&&a[i][j+1]=='0'){
						flag=false;
						break;
					}
				}
			}
			if(!flag)break;
		}
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}

1360F-Spy-string

https://codeforces.com/problemset/problem/1360/F

Problem:

​ 给你n个长度为m的字符串,要求你找出一个字符串,这个字符串需要满足与这n个字符串的差别(字符不同)数最大为1,如果找不到则输出-1

Solution:

拿第一个字符串,对于每一位,从a到z枚举,再与其他字符串比较差异

Code:

#include<bits/stdc++.h>
#define PI acos(-1)
using namespace std;
typedef long long ll;
inline ll read() {
	ll x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9') {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9') {
		x=(x<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
const int maxn=1e6+5;
const ll mod=1e9+7;
int t;
int n,m;
string a[15];
bool check(string s){
	for(int i=2;i<=n;i++){
		int cnt=0;
		for(int j=0;j<=m;j++){
			if(a[i][j]!=s[j]) cnt++;
			if(cnt>1) return false;
		}
	}
	return true;
}
int main() {
	t=read();
	while(t--) {
		n=read(),m=read();
		for(int i=1; i<=n; i++) cin>>a[i];
		string s=a[1];
		bool flag=false;
		for(int i=0;i<m;i++){
			for(char j='a';j<='z';j++){
				s=a[1];
				s[i]=j;
				if(check(s)){
					flag=true;
					break;
				}
			}
			if(flag) break;
		}
		if(flag) cout<<s<<endl;
		else cout<<-1<<endl;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值