2019summer系列 第八场

本文精选了多个在线编程竞赛题目,包括CodeForces和AtCoder的挑战,详细解析了涉及数组处理、区间维护、贪心算法及特殊规律寻找等问题的解决思路与实现代码。

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

https://vjudge.net/contest/311991#overview

A CodeForces 1176C


看代码的字面意思即可~

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,sum;
int a[500500];
int cnt[50],tmp;
int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	sum = n;
	tmp = 0;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
		if(a[i] == 4){
			cnt[4] ++;
		}
		else if(a[i] == 8 && cnt[4]>0){
			cnt[8] ++;
			cnt[4] --;
		}
		else if(a[i]==15 && cnt[8]>0){
			cnt[15]++;
			cnt[8] --;
		}
		else if(a[i]==16 && cnt[15]>0){
			cnt[16] ++;
			cnt[15] --;
		}
		else if(a[i]==23 && cnt[16]>0){
			cnt[23] ++;
			cnt[16] --;
		}
		else if(a[i]==42 && cnt[23]>0){
			cnt[42] ++;
			cnt[23] --;
			tmp ++;
		}
	}
	cout << n-tmp*6 <<endl;
    return 0;
}

B CodeForces 482B


线段树维护区间&和~
//有点小麻烦呢,等会专门写一篇吧!

C AtCoder 4636


稍微找一下规律啦!
特殊情况要认真考虑哦!

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll k,a,b,bis,yen;
int main(){
	ios::sync_with_stdio(false);
	cin >> k >> a >> b;
	bis = 1;
	if(b-a <= 2 || k+1 < a) cout << k+1;
	else{
		while(bis < a){
			k --;
			bis ++;
		}
		if(k%2 == 0) cout << (k/2)*(b-a) +bis;
		else cout << (k/2)*(b-a) + 1 + bis;
	}
    return 0;
}

D AtCoder 4637


据说是个dp,可是我真的不会dp啊呜呜呜呜。。。。

E CodeForces 1176B


一个非常水的题,按 m o d 3 mod 3 mod3的余数分类,稍微用一丢丢贪心就阔以啦。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int T,n;
int a[5],x,ans;
int main(){
	ios::sync_with_stdio(false);
	cin >> T;
	while(T --){
		cin >> n;
		ans = 0;
		memset(a,0,sizeof a);
		for(int i = 1;i <= n;i ++){
			cin >> x;
			a[x%3] ++;
		}
		ans += a[0];
//		cout << a[0] <<' '<<a[1] <<' '<<a[2] <<endl;
		if(a[2] <= a[1]){
			a[1] -= a[2];
			ans += a[2];
			ans += a[1]/3;
		}
		else{
			a[2] -= a[1];
			ans += a[1];
			ans += a[2]/3;
		}
		cout << ans << endl;
	}
    return 0;
}

F AtCoder 5090


其实也不难,就是要考虑各种奇奇怪怪的情况。。
某澜分了7种情况呢。。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,a[100500],sum;
bool pos,neg,ze;
int main(){
	ios::sync_with_stdio(false);
	cin >> n;
	ze = pos = neg = false;
	for(int i = 1;i <= n;i ++){
		cin >> a[i];
		sum += abs(a[i]);
		if(a[i] > 0) pos = true;
		else if(a[i] < 0) neg = true;
		else ze = true;
	}
	sort(a+1,a+n+1);
	if(ze && !pos && !neg){
		cout << 0 <<endl;
		while(n > 1){
			cout << 0 << ' ' << 0 <<endl;
			n --;
		}
		return 0;
	}
	if(neg && pos || ze){
		cout << sum << endl;
		int st = 2,mm = n;
		while(a[st] <= 0){
			cout << a[n] <<' '<<a[st]<<endl;
			a[n] = a[n]-a[st];
			st ++;
			mm --;
		}
		while(mm > 2){
			cout << a[1] <<' ' <<a[n]<<endl;
			a[1] = a[1] -a[n];
			mm --;
			n --;
		}
		cout << a[st] << ' '<< a[1]<<endl;
	}
	else if(pos){
		cout <<sum-a[1]*2<<endl;
		cout << a[1] <<' '<< a[n] <<endl;
		a[1] = a[1]-a[n];
		n --;
		while(n > 2){
			cout << a[1] <<' ' <<a[n]<<endl;
			a[1] = a[1] -a[n];
			n --;
		}
		cout << a[n] << ' '<< a[1]<<endl;
	}
	else {
		int st = 1;
		cout << sum+2*a[n]<<endl;
		cout << a[n] <<' '<< a[1] <<endl;
		a[n] = a[n]-a[1];
		st ++;
		while(st<n){
			cout << a[n] <<' ' <<a[st]<<endl;
			a[n] = a[n] -a[st];
			st ++;
		}
	}
    return 0;
}

G CodeForces 498C


据说是个二分图匹配网络流啥子的,如果有时间阔以好好学习一下。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值