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

A. Floor Number

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

題意:ios

一個樓房房間號由 \(1\) 遞增,一樓僅2個房間。給定一位用戶的房間號和 \(2\)樓以上每層的房間數\(x\)c++

求出用戶所在樓層app

思路:學習

很簡單,理解題意便可。測試

若是 \(n≤2\) ,則答案爲1。不然,您能夠「刪除」第一層,而後答案爲 \(⌊\frac{n-3}{x}⌋+ 2。\)spa

#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/Bcode

題目有點長,你們點擊連接看原題 或者 題意↓ci

題意:get

給定 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/C

題意:

思路:

直觀地說,咱們首先須要進行全部增量操做,而後才須要複製數字(由於不然咱們能夠交換移動順序,而且總和不會減小)。 您可能會注意到答案不超過 \(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

從開始遍歷,利用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;
}
相關文章
相關標籤/搜索