codeforces round 811题解 A~F

A. Everyone Loves to Sleep

A. Everyone Loves to Sleep

题目大意

给定一个人的睡觉的时间(小时+分钟),和这个人定的n个闹钟的时间(小时+分钟),求这个人最多睡多久就被闹钟叫醒。

思路

时间是一个闭环,当到达凌晨十二点是,时间清零,但实际上我们可以不必这样,我们把时间转为一天1440分钟,当之一天这个人睡觉的时间之后没有顶任何闹钟,那么下一个闹钟,就是下一天的第一个闹钟,显然需要对闹钟时间进行排序,这里建议选用multiset进行排序和存储,这样可以直接获得,睡觉时间是不是这一天的最后时间的信息,而且其他操作都更方便。

AC代码

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
const int M = 5000;
int a[M];
multiset<int> mp;
int main() {
	int t; cin >> t;
	while (t--) {
		mp.clear();
		int n, h, m; cin >> n >> h >> m;
		int w = h * 60 + m;
		mp.insert(w);
		for (int i = 1; i <= n; i++) {
			int hh, mm; cin >> hh >> mm;
			int minn = hh * 60 + mm;
		//	int min1 = minn + 1440;
			mp.insert(minn);// mp.insert(min1);
		}
		auto p=mp.lower_bound(w);
	//	for (auto p : mp) {
	//		cout << p << " ";
	//	}
		if (p == prev(mp.end())) {
			int tmp = *mp.begin();
			int cha =1440-w+tmp;
	//		cout << "chain " << cha << endl;
			int h1 = cha / 60; int m1 = cha % 60;
			cout << h1 << " " << m1 << endl;
			continue;
		}
	//	cout << endl;
		int ti = *next(p);
	//	cout << "w: " << w << endl;
	//	cout << "ti: " << ti << endl;
		int cha = ti - w;
		int ho = cha / 60; int mi = cha % 60;
		cout << ho << " " << mi << endl;
	}
	return 0;
}

B. Remove Prefix

B. Remove Prefix

题目大意

给定一个序列a,我们可以删除一部分前缀序列,如果剩余序列每个元素都是唯一的话,就是“令人高兴的序列”,问最少要删除多少个前缀元素,才可以变成“高兴序列”。

思路

既然只能从最前面删除元素,那么我们可以从后往前遍历,使用一个map来记录每个元素出现的次数,当有一个元素出现了超过一次后,就说明只有这个元素往后(除了它以外)的序列是高兴的,就ok了。

AC代码

#include<iostream>
#include<map>
using namespace std;
const int M = 2e5 + 5;
int a[M];
int main() {
	int t; cin >> t;
	while (t--) {
		map<int, int> mp;
		int n; cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
		}
		int pos = 0;
		for (int i = n; i >= 1; i--) {
			if (!mp.count(a[i])) {
				mp[a[i]]++;
				pos = i;
			}
			else
				break;
		}
	//	cout << pos << endl;
		cout << pos-1<< endl;
	}
}

C. Minimum Varied Number

C. Minimum Varied Number

题目大意

给定一个数字s,求出一个数字,这个数字有两条限制:

  • 数字在所有情况中是最小数
  • 数字的每一位的加和是s
  • 数字的每一位都不相同

思路

首先,如果想让数是最小数,那么就要数字长度最短,并且较大值在低位上,所以就可以从大到小枚举1~9的数字,当sum+i>s的时候,就选取大小为s-sum的数,也就是较小的数,将每次选出的数存在数组里,然后倒着输出就ok了

AC代码

#include<iostream>
#include<algorithm>
#include<map>
#include<queue>

using namespace std;
const int M = 100;
int num[M];
int main() {
	int t; cin >> t;
	while (t--) {
		int s; cin >> s;
		int tot = 0;
		int sum = 0;
		for (int i = 9; i >= 1; i--) {
			
			if (sum+i>s) {
				
				num[++tot] = s - sum;
				sum += s - sum;
				break;
			}
			else {
				num[++tot] = i;
				sum += i;
			}
		//	sum += i;
		}
		//cout << sum << endl;
		//for (int i = 1; i <= tot; i++) cout << num[i] << " ";
		int ans = 0;
		for (int i = tot; i >= 1; i--) {
			ans = ans * 10 + num[i];
		}
		cout << ans << endl;
	}
}

D. Color with Occurrences

D. Color with Occurrences

题目大意

给定一个字符串s,和一些小字符串,每个小字符串可以多次使用,求出可以将s完全覆盖使用的最少小字符串数量,如果不能覆盖,输出-1;

思路

这道题可以用动态规划来求解,上一次决策并不会影响当前决策,也就是满足了无后效性,当前状态由l-1处的状态**(放置该字符串的情况)或者r-1处的状态(不放置该字符串的情况)**转移过来,每次记录下来是由哪个状态转移而来的,子字符串的选取情况,更新dp为更优解……
最后从结尾向前遍历,根据状态转移的记录进行跳跃就好了。

思路比较复杂,建议根据代码多理解。

AC代码

#include<iostream>
using namespace std;
const int M = 150;
int dp[M];
const int INF = 0x3f3f3f3f;
string s[15];
int pre[M];
int ansl[M];
int ansid[M];
int main() {
	int q; cin >> q;
	while (q--) {
		string t; cin >> t;
		int len = t.length();
		t = " " + t;
		for (int i = 1; i <= len; i++) {
			dp[i] = INF;
		}
		int n; cin >> n;
		for (int i = 1; i <= n; i++) {
			cin >> s[i];
			s[i] = " " + s[i];
		}
		for (int i = 1; i <= len; i++) {
			for (int j = 1; j <= n; j++) {
				int tmplen = s[j].length() - 1;
		//		cout <<"tmplen "<< tmplen << endl;
				int k = 0;
				while (i - k >= 1 && tmplen - k >= 1 && t[i - k] == s[j][tmplen - k]) {
					k++;
				}
				if (k != tmplen) continue;
				for (int p = i - 1; p >= i - tmplen; p--) {
					if (dp[i] > dp[p] + 1) {
						dp[i] = dp[p] + 1;
						pre[i] = p;
						ansl[i] = i - tmplen + 1;
						ansid[i] = j;
					}
				}
			}
		}
		if (dp[len] == INF) {
			cout << "-1" << endl;
			continue;
		}
		cout << dp[len] << endl;
		for (int i = len; i >= 1; i = pre[i]) {
			cout << ansid[i] << " "<<ansl[i] << endl;
		}
	}
}

E. Add Modulo 10

E. Add Modulo 10

题目大意

对一个数组中的元素进行以下操作

  • a[i]+=a[i]%10
    判断数组元素最后能不能相等。

思路

  • 个位是奇数加一次以后变成了偶数,所以最终的情况一定个位都是偶数的,所以只用分析2、4、6、8四种情况就可以了。
  • 我们依据题目条件写两个数组看一下
  • (1)2 4 8 16 22 24 28 36 42 44 48 56 62
  • (2)18 26 32 34 38 46 52 54 58 66
  • 观察一下发现下面的数组无论如何都不能由上面的数组转换而来,而下面的数组,个位是2、4、8的位上的高位都是奇数,而上面的数组,个位是2、4、8的高位都是偶数,所以,我们可以将数组元素的个位都转换为2以后,判断高位奇偶性是否相同,如果不同,那么无论如何也不能变成相同的数。
  • 另一种情况就是个位是0或5,如果个位有0或5,那么将个位变成0以后,数组中的所有数必须相同。

AC代码


#include<iostream>
using namespace std;
typedef long long ll;
const int M = 2e5 + 5;
ll a[M];
int main() {
	int t; cin >> t;
	while (t--) {
		int n; cin >> n;
		int num0 = 0; int num5 = 0;
		int cnt = 0;
		for (int i = 1; i <= n; i++) {
			cin >> a[i];
			while (a[i] % 10 != 2 && a[i] % 10) {
				a[i] += a[i] % 10;
			}
			if (a[i] % 10 == 0) {
				++cnt;
			}
		}
		int f = 1;
		if (cnt != 0) {
			for (int i = 1; i < n; i++) {
				if (a[i] != a[i + 1]) {
					f = 0;
					break;
				}
			}
		}
		else {
			for (int i = 1; i < n; i++) {
				if ((a[i] / 10) % 2 != (a[i+1] / 10) % 2) {
					f = 0;
					break;
				}
			}
		}
		if (f == 0) cout << "No" << endl;
		else cout << "Yes" << endl;
	}
}

F. Build a Tree and That Is It

F. Build a Tree and That Is It

题目大意

给定节点数量,结点1~2的最短路,2 ~3的最短路,3 ~1的最短路,求出任意一个满足要求的树,输出每条边的两个节点。

思路

分成1、2、3在一条链上和1、2、3不在一条链上的情况分开讨论。

  • 1、在一条链上 :

  • 类似于 在这里插入图片描述
    这两种情况都叫在一条链上。我们设sum为三条最短路的总长度,那么判断方式就是,当sum==di,j*2时,就说明是右图这种情况,很容易得出,可以自行推一下。

  • 2、不在一条链上

  • 类似于
    在这里插入图片描述
    这样就不在一条链上。
    我们可以设4是“根结点”,然后求出4~1的距离,4 ~2的距离,4 ~3的距离,然后将距离大但是中间结点数量不够的部分补上就好了。

  • 提示一点,三个最短路之和一定是偶数,因为 设根到1的距离为x,根到2的距离为y,根到3的距离为z,可以得出

在这里插入图片描述
所以一定是偶数

  • 根到节点的距离怎么求就要自己推导一下了,不是很复杂。

  • 3、最后两种情况共同的,当当前使用的结点数量比n小时,任意补充就好了。

AC代码

#include<iostream>
using namespace std;
const int M = 5;
int d[M];
int dis[M];
int q = 4;
int n;
int root;
int line = 0;
void print(int u, int p) {
	int di = dis[p];
	if (di == 1) {
		cout << u << " " << p << endl;
		return;
	}
	else {
		di -= 2;
		cout << u << " " << q << endl;
		q++;
		for (; di > 0; di--) {
			cout << q - 1 << " " << q << endl;
			q++;
		}
		
		cout << q-1 << " " << p << endl;
	}
}

int main() {
	int t; cin >> t;
	while (t--) {
		q = 4;
	//	root = 0; line = 0;
		 cin >> n >> d[1] >> d[2] >> d[3];
		int sum = d[1] + d[2] + d[3];
		for (int i = 1; i <= 3; i++) dis[i] = 0;
		if (sum % 2 != 0) {
			cout << "NO" << endl;
			continue;
		}
		if (n < sum / 2 + 1) { //sum一定是偶数
			cout << "NO" << endl;
			continue;
		}
		if (d[1] > n || d[2] > n || d[3] > n) {
			cout << "NO" << endl;
			continue;
		}
		line = 0; root = 0; int f = 0;
		for (int i = 1; i <= 3; i++) {
			if (sum == d[i] * 2) {
				line = i; root = i-1;
				if (root == 0) root = 3;
			}
			if (sum < d[i] * 2) f = 1;
		}
		if (f == 1) {
			cout << "NO" << endl;
			continue;
		}
		if (line) {
	//		cout << "in" << endl;
			int linea = (line + 2) % 3;
			int lineb = (line + 1) % 3;
			int a = (root + 1) % 3;
			int b = (root + 2) % 3;
			if (a == 0) a = 3;
		    if (b == 0) b = 3;
			if (linea == 0) linea = 3;
			if (lineb == 0) lineb = 3;
			cout << "YES" << endl;
			dis[a] = d[linea];
			dis[b] = d[lineb];
			print(root, a);
			print(root, b);
		}
		else {
		//	cout << d31 << " " << d12 << " " << d23 << endl;
			dis[1] = d[3] + d[1] - d[2];
			dis[2] = d[2] + d[1] - d[3];
			dis[3] = d[3] + d[2] - d[1];
			if (dis[1] % 2 || dis[2] % 2 || dis[3] % 2) {
				cout << "NO" << endl;
				continue;
			}
			dis[1] /= 2; dis[2] /= 2; dis[3] /= 2;
	//		cout << d[1] << " " << d[2] << " " << d[3] << endl;
			cout << "YES" << endl;
			root = q;
			q++;
			print(root, 1);
			print(root, 2);
			print(root, 3);

		}
		if (q <= n) {
			cout << root << " " << q << endl;
			q++;
			for (; q <= n; q++) {
				cout << q << " " << q - 1 << endl;
			}
		}
	}
}

持续更新ing~

  • 17
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值