Daily Practice 13th:Codeforces Round #743 (Div. 2)

VP*13;

rating:800--1400--1800(这次的rating跨度好离谱?而且好像赛后这次unrated了?)

A. Countdown

给出一个字符串,可以把它视为一个数字,可以对它进行两种操作:1、把这个字符串代表的数字-1;2、选择两位数字进行交换。想让这个数字变为0,需要的最少的步骤是多少。

思路: 若非0数在非个位上,就把它换到个位上,换完之后进行若干次1操作使这一位变成0。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int t, n;
std::string s;

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> n >> s;
		ll ans = 0;
		if (s[n - 1] == '0') {
			for (int i = 0; i < n - 1; i++) {
				if (s[i] != '0')
					ans += (s[i] - '0'), ans++;
			}
		} else {
			ans += (s[n - 1] - '0');
			for (int i = 0; i < n - 1; i++) {
				if (s[i] != '0')
					ans += (s[i] - '0'), ans++;
			}
		}
		std::cout << ans << '\n';
	}
	return 0;
}

B. Swaps

有两个数组,a数组中是1~2n的所有奇数,b数组是1~2n的所有偶数,可以进行如下操作:选择一个数组,交换相邻两个数字,求最少进行多少次操作,使得a数组的字典序小于b数组。

思路: 一开始想简单了,以为是仅考虑a数组和b数组的第一个元素。应该是对于每一个b数组中的元素,找到对应小于它的a数组中的操作次数最小的数,遍历b数组找到最小操作次数。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int t, n;
int a[N], b[N], c[N << 1];

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	std::cin >> t;
	while (t--) {
		std::cin >> n;
		for (int i = 1; i <= n; i++) {
			std::cin >> a[i], c[a[i]] = i;
		}
		for (int i = 1; i <= n; i++) {
			std::cin >> b[i];
		}
		for (int i = 3; i <= 2 * n; i += 2) {
			c[i] = std::min(c[i], c[i - 2]);
		}
		int ans = 2 * n + 1;
		for (int i = 1; i <= n; i++) {
			ans = std::min(ans, i - 1 + c[b[i] - 1] - 1);
		}
		std::cout << ans << '\n';
	}
	return 0;
}

C. Book

给出一本出,有n个章节,每一个章节有若干个前置章节,每一次阅读都是从书的第一个章节一直读到最后一个章节,只有一个章节的全部前置章节被读完才能理解这一章,问至少要阅读几遍这本书才能理解所有章节。

思路: 拓扑排序+set,用vector存每一个章节的入度,每次将入度为0的章节加入set,若可以满足全部理解的条件,那循环次数就是阅读书的遍数;若不满足,即如入set的章节数不等于n,则输出-1。

AC Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)

template <typename T>
inline void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-')
			f = -1;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + ch - '0', ch = getchar();
	}
	x *= f;
}

template <typename T>
void write(T x) {
	if (x < 0)
		putchar('-'), x = -x;
	if (x > 9)
		write(x / 10);
	putchar(x % 10 + '0');
}

#define INF 0x3f3f3f3f
typedef long long ll;
const double PI = acos(-1);
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int t, n, in[N], u, o;
int a[N], b[N];
std::vector<int>vec[N];
std::set<int>ss;

int main() {
//	freopen("test.in","r",stdin);
//  freopen("output.in", "w", stdout);
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		for(int i=1;i<=n;i++) vec[i].clear();
		for (int i = 1; i <= n; ++i) {
			scanf("%d", &in[i]);
			if (!in[i])
				ss.insert(i);
			for (int j = 1; j <= in[i]; ++j) {
				scanf("%d", &u);
				vec[u].push_back(i);
			}
		}
		int ans = 0, cnt = 0;
		while (!ss.empty()) {
			++ans;
			for (auto it = ss.begin(); it != ss.end(); it = ss.lower_bound(o)) {
				o = *it;//从小到大处理set中的数据
				ss.erase(o);
				++cnt;
				for (auto &v : vec[o])
					if (!(--in[v]))//入度为0,入set
						ss.insert(v);
			}
		}
		printf("%d\n", cnt == n ? ans : -1);
	}
	return 0;
}

os:这个题好像不能在过程中开vector和set?在过程中开vector和set会TLE,但是开在主函数外就没事了???

若有错误请指教,谢谢!

orzorz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值