Codeforces Round 811 (Div. 3)(VP-15,寒假加训)

本文介绍了在CodeforcesRound811(Div.3)中五个不同问题的解法,涉及贪心算法(A、C)、区间覆盖(D)、动态规划(D)以及简单的模运算(E)。这些题目展示了在实际编程竞赛中常见的技巧和数据结构的运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A.

模拟

// Problem: A. Everyone Loves to Sleep
// Contest: Codeforces - Codeforces Round 811 (Div. 3)
// URL: https://codeforces.com/group/RAx5fiilkP/contest/1714/problem/A
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
struct node{
	int h,m;
}a[N];
bool cmp(node a,node b){
	if(a.h!=b.h){
		return a.h<b.h;
	}
	return a.m<b.m;
}
void Lan(){
	int n,h,m;
	cin>>n>>h>>m;
	for(int i=1;i<=n;i++){
		cin>>a[i].h>>a[i].m;
		if(a[i].h<h || (a[i].h==h && a[i].m<m)){
			a[i].h+=24;
		}
	}
	sort(a+1,a+1+n,cmp);
	int ans=h*60+m;
	int res=a[1].h*60+a[1].m;
	if(ans==res){
		cout<<0<<" "<<0<<'\n';
		return;
	}
	res-=ans;
	if(res%60==0){
		cout<<res/60<<" "<<0<<'\n';
	}else{
		cout<<res/60<<" "<<res%60<<'\n';
	}
	
		
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}

B.

从后往前遍历一遍即可

// Problem: B. Remove Prefix
// Contest: Codeforces - Codeforces Round 811 (Div. 3)
// URL: https://codeforces.com/group/RAx5fiilkP/contest/1714/problem/B
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int a[N],vis[N];
void Lan(){
	int n;
	cin>>n;
	int  mx=0;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		mx=max(mx,a[i]);
	}
	for(int i=1;i<=(mx+1);i++){
		vis[i]=0;
	}
	for(int i=n;i>=1;i--){
		if(!vis[a[i]]){
			vis[a[i]]=1;
		}else{
			cout<<i<<'\n';
			return;
		}
	}
	cout<<0<<'\n';
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}

C.

简单贪心

// Problem: C. Minimum Varied Number
// Contest: Codeforces - Codeforces Round 811 (Div. 3)
// URL: https://codeforces.com/group/RAx5fiilkP/contest/1714/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int a[N];
void Lan(){
	int s;
	cin>>s;
	vector<int> v;
	int num=9;
	while(s){
		if(s>num){
			s-=num;
			v.push_back(num);
			num--;
		}else{
			v.push_back(s);
			break;
		}
	}
	reverse(v.begin(),v.end());
	for(auto &i:v){
		cout<<i;
	}
	cout<<'\n';
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}

D.

1.

重点

学到了区间覆盖

先暴力枚举有几个可以覆盖

然后贪心

找最长的线段去覆盖

认定左边都是被上一个覆盖过了,所以只要枚举右边线段即可

因为写的是while所以我玄学了加了一个计时器如果超过1000就说明不存在方案即可

debug...

2.

dp

// Problem: D. Color with Occurrences
// Contest: Codeforces - Codeforces Round 811 (Div. 3)
// URL: https://codeforces.com/group/RAx5fiilkP/contest/1714/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 1e5 + 9;
int dp[N],pre[N];
pair<int,int>a[N];
string ss[N];
void Lan(){
	string t;
	cin>>t;
	int m=t.length();
	t=" "+t;
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>ss[i];
	}
	memset(dp,0x3f,sizeof(dp));
	dp[0]=0;
	int mx=dp[1];
	for(int i=1;i<=m;i++){
		for(int j=1;j<=n;j++){
			if(i>=ss[j].length()){
				if(t.substr(i-ss[j].length()+1,ss[j].length())==ss[j]){
					for(int l=(int)(i-ss[j].length());l<i;l++){
						if(dp[l]+1<dp[i]){
							dp[i]=dp[l]+1;
							pre[i]=l;
							a[i]={i-ss[j].length()+1,j};
						}
					}
				}
			}
		}
	}
	if(dp[m]==mx){
		cout<<-1<<'\n';
	}else{
		cout<<dp[m]<<'\n';
		int now=m;
		while(now){
			cout<<a[now].second<<" "<<a[now].first<<'\n';
			now=pre[now];
		}
	}
	
	
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}
 

E.

找规律举几个数字

// Problem: E. Add Modulo 10
// Contest: Codeforces - Codeforces Round 811 (Div. 3)
// URL: https://codeforces.com/group/RAx5fiilkP/contest/1714/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>
#define eps 1e-5
#define INF 1e9
using namespace std;
typedef long long ll;
const int N = 2e6 + 9;
int a[N];
//发现有5和没5倍数在一起就不可能
//其他就抽成只有2,然后这样变化就+20,mod20看最后数字是不是一样即可
int work(int x){
	while(x%10!=2 && x%10!=0){
		x=x+x%10;
	}
	return x;
}
void Lan(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int cnt=0;
	for(int i=1;i<=n;i++){
		if(a[i]%5==0){
			cnt++;
		}
	}
	if(cnt>0 && cnt<n){
		cout<<"No"<<'\n';
		return;
	}
	for(int i=1;i<=n;i++){
		a[i]=work(a[i]);
	}
	if(!cnt){
		for(int i=1;i<=n;i++){
			a[i]%=20;
		}
	}
	for(int i=1;i<=n-1;i++){
		if(a[i]!=a[i+1]){
			cout<<"No"<<'\n';
			return;
		}
	}
	cout<<"Yes"<<'\n';
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int q;
	cin>>q;
	while (q--) {
		Lan();
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值