Codeforces Round #419 (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 = 1e5 + 10;

int main(){
	int a, b;	scanf("%d:%d", &a, &b);
	int cnt = 0;
	while(true){
		int t2 = b % 10;
		int t1 = b / 10;
		int ans = t1 + t2 * 10;
		if(a == ans){
			printf("%d\n", cnt);
			break;
		}
		cnt++;
		b++;
		if(b >= 60){
			b = 0;
			a++;
		}
		if(a >= 24){
			a = 0;
		}
	}
	return 0;
}


B

题意:给出n个区间和q次查询和一个数k,给出n个区间ai,bi表示区间ai-bi之间的数都加一,q次查询区间ai-bi有多少个数超过k。

思路:参考传送门

#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, k, q;
int num[qq];
int sum[qq];

int main(){
	scanf("%d%d%d", &n, &k, &q);
	int a, b;
	for(int i = 1; i <= n; ++i){
		scanf("%d%d", &a, &b);
		num[a] += 1, num[b + 1] += -1;
	}
	for(int i = 1; i <= 200000; ++i)
		sum[i] = sum[i - 1] + num[i];
	for(int i = 1; i <= 200000; ++i){
		if(sum[i] < k)	sum[i] = 0;
		else	sum[i] = 1;
	}
	for(int i = 1; i <= 200000; ++i)
		sum[i] += sum[i - 1];
	while(q--){
		scanf("%d%d", &a, &b);
		printf("%d\n", max(sum[b] - sum[a - 1], 0));
	}
	return 0;
}


C

题意:给出n,m的矩阵,每次可以选择一行或者一列进行加一,要求你在最操作数最少的情况下得到所给矩阵,没办法得出矩阵则输出-1

思路:其实挺简单的,最开始对这个操作数最少不理解,但想了想这个样例以后

2 3

1 1 1

1 1 1

会发现此时满足行就是两次,满足列就是三次,所以首先判断一下行列的大小决定先满足谁即可

#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 = 100 + 10;
int n, m;
int num[qq][qq];
vector<int> ans[2];
void Solve(int x, int y, int z){
	REP(i, 1, x){
		int minx = 1e9;
		REP(j, 1, y)	minx = min(minx, z ? num[i][j] : num[j][i]);
		REP(j, 1, y)	(z ? num[i][j] : num[j][i]) -= minx;
		while(minx > 0)	ans[z].pb(i), minx--;
	}
}

int main(){
	scanf("%d%d", &n, &m);
	REP(i, 1, n)
		REP(j, 1, m)
			scanf("%d", &num[i][j]);
	if(n <= m)	Solve(n, m, 1), Solve(m, n, 0);
	else	Solve(m, n, 0), Solve(n, m, 1);
	REP(i, 1, n)
		REP(j, 1, m)
			if(num[i][j]){
				printf("-1\n");
				return 0;
			}
	printf("%d\n", ans[0].size() + ans[1].size());
	REP(i, 1, ans[0].size())	printf("col %d\n", ans[0][i - 1]);
	REP(i, 1, ans[1].size())	printf("row %d\n", ans[1][i - 1]);
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值