【AtCoder ABC 127】补题AK E待补

比赛链接

A 语法 模拟

思路 几个else if 就行
代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "Yes" << endl
#define cn cout << "No" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;

int a, b;
int ans;
int main() {
	cin >> a >> b;

	if (a >= 13) ans = b;
	else if (a >= 6 && a <= 12)
		ans = b / 2;
	else
		ans = 0;

	cout << ans;
	return 0;
}

B 语法 模拟

思路 按题意写就行
代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "Yes" << endl
#define cn cout << "No" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;

ll r, d, x;
int main() {
	cin >> r >> d >> x;

	forn(i, 1, 10) x = x * r - d, cout << x << endl;
	return 0;
}

C 思维

思路 第i次,暴力将l 到r 加上1(这些ID卡可通过这扇门)。最后再看每一张ID卡,是否m条门都打得开。但这很明显会TLE飞。 很明显区间修改,直接上差分优化就行了
代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "Yes" << endl
#define cn cout << "No" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 1e5 + 5;

int n, m;
int l, r;
int d[N];
int main() {
	cin >> n >> m;
	forn(i, 1, m) {
		cin >> l >> r;
		d[l]++, d[r + 1]--;
	}

	int ans = 0;
	forn(i, 1, n) d[i] += d[i - 1];
	forn(i, 1, n) if (d[i] == m) ans++;
	cout << ans;
	return 0;
}

D 贪心

思路 排个序,每次把尽可能小的改成尽可能大的,如果改不了就break
代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "Yes" << endl
#define cn cout << "No" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;

ll n, m;
struct node {
	ll x, y;
} s[N];
bool cmp(node a, node b) { return a.y > b.y; }
ll a[N];
ll ans;
int main() {
	cin >> n >> m;
	forn(i, 1, n) cin >> a[i];
	forn(i, 1, m) cin >> s[i].x >> s[i].y;
	sort(a + 1, a + n + 1);
	sort(s + 1, s + m + 1, cmp);

	int cnt = 1;
	forn(i, 1, m) {
		if (cnt > n) break;//没加这个就RE了,我猜是爆int成负数了
		forn(j, cnt, min(n, cnt + s[i].x - 1)) {
			if (a[j] > s[i].y) break;
			a[j] = s[i].y;
		}
		cnt += s[i].x;
	}

	forn(i, 1, n) ans += a[i];
	cout << ans;
	return 0;
}

E 排列组合

思路 不会,抄题解结论的
代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "Yes" << endl
#define cn cout << "No" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;

ll p = 1e9 + 7;
ll qpow(ll a, ll b) {
	ll s = 1;
	while (b) {
		if (b & 1) s = (s * a) % p;
		a = (a * a) % p;
		b >>= 1;
	}
	return s;
}
ll C(ll a, ll b) {
	ll res = 1;
	forn_(i, a, a - b + 1) res = (res * i) % p;
	ll fac = 1;
	forn(i, 1, b) fac = (fac * i) % p;
	res = (res * qpow(fac, p - 2)) % p;
	return res;
}
ll n, m, k;
ll ans;
int main() {
	cin >> n >> m >> k;

	forn(i, 1, n) ans = (ans + (n - i) * i * m * m) % p;
	forn(i, 1, m) ans = (ans + (m - i) * i * n * n) % p;
	ans = (ans * C(n * m - 2, k - 2)) % p;

	cout << ans;
	return 0;
}

F 数学 数据结构

思路 观察下原式,就是几个绝对值加一起,根据绝对值的几何关系,选中位数能让最小。

那么问题就转化为怎么求中位数了,直接上对顶堆

还有个问题就是怎么求和,如果遍历一个一个加的话,肯定TLE了,考虑优化。

因为对顶堆的性质,一个堆全大于中位数,一个堆全小于中位数,那么就很好去绝对值了,用个值s来维护堆里所有元素的和,最后直接算中位数乘以堆大小,再减/加上s即可

代码
#include <bits/stdc++.h>
using namespace std;
#define umap unordered_map
#define cy cout << "Yes" << endl
#define cn cout << "No" << endl
#define ll long long
#define forn(i, l, r) for (int i = l; i <= r; i++)
#define forn_(i, l, r) for (int i = l; i >= r; i--)
#define debug(a) cout << #a << "=" << a << endl;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 5;

struct cmp1 {
	bool operator()(const int &a, const int &b) const {
		return a < b;
	}
};
struct cmp2 {
	bool operator()(const int &a, const int &b) const {
		return a > b;
	}
};
priority_queue<int, vector<int>, cmp1> q1;
priority_queue<int, vector<int>, cmp2> q2;
ll s1, s2;
ll mid, cnt;
ll q, n;
ll op, x, y;
ll b;
int main() {
	cin >> q;

	while (q--) {
		cin >> op;
		if (op == 1) {
			cin >> x >> y;
			q1.push(x), s1 += x, b += y;
			cnt++;
			while (q1.size() - q2.size() > 1) {
				x = q1.top(), q1.pop();
				q2.push(x);
				s1 -= x, s2 += x;
			}
			while (!q2.empty() && q1.top() > q2.top()) {
				x = q1.top(), y = q2.top();
				q1.pop(), q2.pop();
				q2.push(x), q1.push(y);
				s1 = s1 - x + y, s2 = s2 - y + x;
			}
		}
		if (op == 2) {
			ll res = b;
			mid = q1.top();
			res += q1.size() * mid - s1;
			res += s2 - q2.size() * mid;
			cout << mid << ' ' << res << endl;
		}
	}
	return 0;
}
posted @ 2023-12-02 23:55  史上最速败犬  阅读(13)  评论(0)    收藏  举报