Codeforces Round #420 (Div. 2)

本文解析了三道经典的编程题目,包括矩阵元素匹配验证、直线上的整数点选择及栈内元素排序问题,通过示例代码详细展示了每道题目的解题思路与实现方法。

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

A

写的比较傻

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int qq = 2e5 + 10;
int num[105][105];
int a[105], b[105];
int n;	
bool Check(int x, int y){
	int cnt1 = 0;
	for(int i = 1; i <= n; ++i)
		if(i != y)	a[cnt1++] = num[x][i];
	int cnt2 = 0;
	for(int i = 1; i <= n; ++i)
		if(i != x)	b[cnt2++] = num[i][y];
	for(int i = 0; i < cnt1; ++i)
		for(int j = 0; j < cnt2; ++j)
			if(a[i] + b[j] == num[x][y])	return true;
	return false;
}

int main(){
	scanf("%d", &n);
	REP(i, 1, n)
		REP(j, 1, n)
			scanf("%d", &num[i][j]);
	bool f = true;
	REP(i, 1, n){
		REP(j, 1, n){
			if(num[i][j] != 1){
				if(!Check(i, j))	f = false;
			}
		}
	}
	if(f)	puts("Yes");
	else	puts("No");
	return 0;
}


B

题意:给出一条直线,选出一个矩形,要求矩形内的整数点x,y的和最大

思路:枚举y轴,二分找最大的x

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int qq = 2e5 + 10;
LL m, b;
bool Check(LL x, LL y){
	double f = x * 1.0 / m;
	f = y * 1.0 + f - b * 1.0;
	if(f > 0)	return false;
	return true;
}
LL Cal(LL y){
	LL l = 0, r = m * b, mid;
	LL x = 0;
	while(l <= r){
		mid = (l + r) / 2;
		if(Check(mid, y)){
			x = mid;
			l = mid + 1;
		}else{
			r = mid - 1;
		}
	}
//	printf("%lld %lld\n", x, y);
	LL xx = y + 1, yy = x + 1;
	LL ans = ((1LL + x) * x / 2LL) * xx + ((1LL + y) * y / 2LL) * yy;
	return ans;
}

int main(){
	scanf("%lld%lld", &m, &b);
	LL maxn = 0;
	for(int i = 0; i <= b; ++i){
		maxn = max(maxn, Cal(i));
	}
	printf("%lld\n", maxn);
	return 0;
}


C

题意:有一个栈,可以放元素进去,也可以消除栈顶元素,但是要按元素1到元素n的顺序消除,每次可以对栈内的元素排序,问最少排多少次。

思路:分两个堆,一堆是已经排好的,一堆是没排好的进行讨论即可

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>

using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define mst(a, b)	memset(a, b, sizeof a)
#define REP(i, x, n)	for(int i = x; i <= n; ++i)
const int qq = 2e5 + 10;
int n;
map<int, int> mp1, mp2;

int main(){
	scanf("%d", &n);
	n = 2 * n - 1;
	int x = 1, top = 0;
	bool f = true;
	char op[10];
	int ans = 0, num;
	map<int, int>::iterator it;
	while(n--){
		scanf("%s%d", op, &num);
		if(op[0] == 'a'){
			mp2[num] = ++top;
			continue;
		}
		if(mp2.size() == 0){
			mp1.erase(mp1.begin());
			x++;
		}else{
			if(mp2[x] == top){
				mp2.erase(mp2.find(x));
				top--;
				x++;
			}else{
				ans++;
				for(it = mp2.begin(); it != mp2.end(); ++it){
					mp1[it->first] = it->second;
				}
				mp2.clear();
				mp1.erase(mp1.begin());
				x++;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值