Codeforces Round #674 (Div. 3) (A - E题题解)

A. Floor Number

https://codeforces.com/contest/1426/problem/A

Codeforces Round #674 (Div. 3) (A - E题题解)

题意:

一个楼房房间号由 \(1\) 递增,一楼仅2个房间。给定一位用户的房间号和 \(2\)楼以上每层的房间数\(x\)

求出用户所在楼层

思路:

很简单,理解题意即可。

如果 \(n≤2\) ,则答案为1。否则,您可以“删除”第一层,然后答案为 \(⌊\frac{n-3}{x}⌋+ 2。\)

#python
for i in range(int(input())):
    n, x = map(int, input().split())
    print(1 if n <= 2 else (n - 3) // x + 2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int _; cin >> _; while (_--) {
		ll n, x;
		cin >> n >> x;
		if (n <= 2)cout << 1 << endl;
		else cout << (n - 3) / x + 2 << endl;
	}
}

B. Symmetric Matrix

https://codeforces.com/contest/1426/problem/B

题目有点长,大家点击链接看原题 或者 题意↓

题意:

给定 n 种 2 x 2大小的方块,问是否能通过这些方块组成 m x m的矩形(需要 \(s[i][k] = s[j][i]\))

思路:

首先,如果m为奇数,则出于显而易见的原因,答案为“否”。 否则,我们会注意到图块的左上角和右下角值无关紧要(因为我们可以对称放置图块)。 因此,我们只需要检查是否有一些图块的右上值等于其左下值(因为这是我们获得主对角线对称性的方式)。

#python
for i in range(int(input())):
	n, m = map(int, input().split())
	a = []
	for i in range(n):
		a.append([[int(x) for x in input().split()] for i in range(2)])
	ok = False
	for i in range(n):
		ok |= a[i][0][1] == a[i][1][0]
	ok &= m % 2 == 0
	print("YES" if ok else "NO") 
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
	ll n, m;
	cin >> n >> m;
	ll a, b, c, d;
	bool f1 = 0, f2 = 0;
	for (int i = 1; i <= n; ++i) {
		cin >> a >> b >> c >> d;
		if (!f2 && b == c)f2 = 1;
	}
	if (m % 2 == 0)f1 = 1;
	if (f1 && f2)cout << "YES" << endl;
	else cout << "NO" << endl;
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int _; cin >> _; while (_--)solve();
}

C. Increase and Copy

https://codeforces.com/contest/1426/problem/CCodeforces Round #674 (Div. 3) (A - E题题解)

题意:

思路:

直观地说,我们首先需要进行所有增量操作,然后才需要复制数字(因为否则我们可以交换移动顺序,并且总和不会减少)。 您可能会注意到答案不超过 \(O(\sqrt{n})\),所以我们可以从1迭代到⌊\(O(\sqrt{n})\)⌋,然后确定要复制的数字。 设为x。 那么我们需要x-1个移动来获得它,还需要⌈\(\frac{n-x}x\)⌉个移动来获得足够数量的副本。 因此,我们可以用此举数来更新答案。
时间复杂度:每个测试用例为\(O(\sqrt{n})\)。
实际上,所需的数字总是非常接近⌊\(O(\sqrt{n})\)⌋,因此只要尝试在[⌊\(O(\sqrt{n})\)⌋-5; ⌊\(O(\sqrt{n})\)⌋ + 5]范围内进行一些选择就足够了。 回答。 这是 \(O(1)\)解决方案。

#include<bits/stdc++.h>
using namespace std;
int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int t;
	cin >> t;
	while (t--) {
		int n;
		cin >> n;
		int ans = 1e9;
		for (int x = 1; x * x <= n; ++x) {
			ans = min(ans, x - 1 + ((n - x) + x - 1) / x);
		}
		cout << ans << endl;
	}
}

D. Non-zero Segments

https://codeforces.com/contest/1426/problem/D

Codeforces Round #674 (Div. 3) (A - E题题解)

从开始遍历,利用sum去统计前面一段的值。

如果已经出现过,说明会导致有区间和为0的情况出现,ans++并且map clear 重新计数 sum从当前开始.

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	map<ll, ll>m;
	ll ans = 0, sum = 0, x;
	m[0]++;
	for (int i = 0; i < n; ++i) {
		cin >> x;
		sum += x;
		if (m[sum] > 0) {
			ans++;
			sum = x;
			m.clear();
			m[0]++;
		}
		m[sum]++;
	}
	cout << ans;
}

E. Rock, Paper, Scissors

https://codeforces.com/contest/1426/problem/E

Alice 和 Bob这次开始玩猜拳了,给定他们玩的次数n,和石头剪刀布出现的次数,求Alice能赢的最多次数和最少次数。

数学解法:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

ll q_max(ll x, ll y, ll z, ll h) {
	x = x > y ? x : y;
	x = x > z ? x : z;
	x = x > h ? x : h;
	return x;
}

int main() {
	//freopen("in.txt", "r", stdin);
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	ll n, a, b, c, x, y, z;
	cin >> n;
	cin >> a >> b >> c >> x >> y >> z;
	cout << q_max(0, a - x - z, b - x - y, c - y - z ) << " " << min(a, y) + min(b, z) + min(c, x);
}

F. Number of Subsequences

没做出来

先贴一下dalao的代码留做学习

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
typedef ll ll;
int main() {
    int n; cin >> n;
    string ss; cin >> ss;
    ll x = 0;
    ll ans = 0;
    ll temp = 0;
    ll num = 1;
    for (int i = 0; i < n; i++) {
        if (ss[i] == 'a') x += num;
        else if (ss[i] == 'b') temp += x;
        else if (ss[i] == 'c') ans += temp;
        else {
            ans = ans * 3 + temp;
            temp = temp * 3 + x;
            x = x * 3 + num;
            num *= 3;
        }
        num %= mod;
        x %= mod; temp %= mod; ans %= mod;
    }
    cout << ans << endl;
}
上一篇:学习进度条(第五周)


下一篇:C10K问题