Codeforces Round 1031 (Div.2)

比赛链接:Codeforces Round 1031
Github 链接:CF1031

A. Shashliks

贪心。

不妨假设 a > b a > b a>b ( a ≤ b a \le b ab 时分别交换 ( a , b ) (a, b) (a,b) ( x , y ) (x, y) (x,y) 即可)

  • x > y x > y x>y 时,一直进行第二种操作是最优的操作。
  • x < y x < y x<y 时,先进行第一种操作直到温度 k < a k < a k<a 再进行第二种操作是最优的操作。

时间复杂度: O ( 1 ) O(1) O(1)

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

#define int long long

void solve() {
    int k, a, b, x, y;
    cin >> k >> a >> b >> x >> y;
    if (a < b) swap(a, b), swap(x, y);
    int res = 0;
    if (x < y) {
        int res1 = max(0LL, (k - a + x) / x), res2 = max(0LL, (k - res1 * x - b + y) / y);
        res = max(res1 + res2, res);
    } else {
        res = max(res, (k - b + y) / y);
    }
    cout << res << "\n";
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}

B. Good Start

( x 1 − x 2 ) % a = 0 (x_1 - x_2) \% a = 0 (x1x2)%a=0 ( y 1 − y 2 ) % b = 0 (y_1 - y_2) \% b = 0 (y1y2)%b=0 时输出 Yes

特判:当 x 1 = x 2 x_1 = x_2 x1=x2 y 1 = y 2 y_1 = y_2 y1=y2 时需要判断另一个不相等的坐标的差是否能整除 a a a 或者 b b b

时间复杂度: O ( 1 ) O(1) O(1)

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

#define int long long

int w, h, a, b, x1, Y1, x2, y2;

void solve() {
    cin >> w >> h >> a >> b >> x1 >> Y1 >> x2 >> y2;
    if (x1 == x2) {
        if (abs(Y1 - y2) % b == 0) cout << "Yes\n";
        else cout << "No\n";
    } else if (Y1 == y2) {
        if (abs(x1 - x2) % a == 0) cout << "Yes\n";
        else cout << "No\n";
    } else {
        if ((x1 - x2) % a == 0 || (Y1 - y2) % b == 0) cout << "Yes\n";
        else cout << "No\n";
    }
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}

C. Smilo and Minecraft

可以发现无论 k k k 等于多少,第二次操作开始一定不会损失金矿,所以第一次操作只需要选择损失金矿最少的空地进行操作即可。为了快速查询区间内的金矿数量,可以利用二维前缀和对金矿数量进行维护。

时间复杂度: O ( n m ) O(nm) O(nm)

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

#define int long long

int n, m, k, pre[505][505];

void solve() {
	cin >> n >> m >> k;
	string s[505];
	for (int i = 1; i <= n; i++) cin >> s[i], s[i] = " " + s[i];
	for (int i = 0; i <= n; i++)
		for (int j = 0; j <= m; j++) pre[i][j] = 0;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			pre[i][j] = pre[i - 1][j] + pre[i][j - 1] - pre[i - 1][j - 1] + (s[i][j] == 'g');
	int res = INT_MAX;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			if (s[i][j] == '.') {
				int maxx = min(n, i + k - 1), maxy = min(m, j + k - 1);
				int minx = max(1LL, i - k + 1), miny = max(1LL, j - k + 1);
				int tmp = pre[maxx][maxy] - pre[minx - 1][maxy] - pre[maxx][miny - 1] + pre[minx - 1][miny - 1];
				res = min(res, tmp);
			}
	cout << pre[n][m] - res << "\n";
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    int T;
    cin >> T;
    while (T--) solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值