比赛链接:

https://codeforces.com/contest/1700

C. Helping the Nature

题意:

给定一个序列 \(a\),每一步有三种操作可以执行:
1、让 1 到 \(i\) 所有数减 1
2、让 i 到 \(n\) 的所有数减 1
3、1 到 \(n\) 所有数都加上 1
问,最少几步可以让序列中所有的元素变成 0。

思路:

\(i < i + 1\) 的时候,所有 \(i + 1\)\(n\) 的数都要减去它们的差值。
\(i > i + 1\) 的时候,所有 1 到 \(i\) 的数都要减去它们的差值。
这样子操作过后,所有的元素都是一样的了,然后全体加或者减到 0 即可。

代码:

#include <bits/stdc++.h>
using namespace std;
#define LL long long
void solve(){
	LL n;
	cin >> n;
	vector <LL> a(n);
	for (int i = 0; i < n; i ++ )
		cin >> a[i];
	LL ans = 0, s = a[0];
	for (int i = 1; i < n; i ++ ){
		LL d = a[i - 1] - a[i];
		if (d > 0){
			s -= d;
		}
		ans += abs(d);
	}
	cout << ans + abs(s) << "\n";
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	LL T;
	cin >> T;
	while (T -- )
		solve();
	return 0;
}

D. River Locks

题意:

\(n\) 个水池,第 \(i\) 个水池的容量为 \(v_i\),每个水池都有一个出水口,满足\((v_i < v_{i + 1})\),当第 \(i\) 个水池满水之后,会自动流向第 \(i + 1\) 个水池。
现在有 \(q\) 次询问,问在 \(t\) 时间内,最少开多少水池的出水口可以让所有水池都蓄满。

思路:

首先根据贪心的思想,水不浪费,所有开的出水口都在左边,这样子才可以实现最优。
那么,对于每个出水口可以贡献的价值就是单调的,所以可以采取二分的策略。
另外,时间是有限制的,每一个水池都要填满,那么对于每一个水池,前面来的水都要足够,由此可以算出时间的一个下限。

#include <bits/stdc++.h>
using namespace std;
#define LL long long
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	LL n;
	cin >> n;
	vector <LL> v(n), sum(n + 1);
	LL mx = 0;
	for (int i = 0; i < n; i ++ ){
		cin >> v[i];
		sum[i + 1] = sum[i] + v[i];
		mx = max(mx, (sum[i + 1] + i) / (i + 1));
	}
	LL q;
	cin >> q;
	while (q -- ){
		LL t;
		cin >> t;
		if (t < mx){
			cout << "-1\n";
		}
		else{
			LL L = 1, R = n;
			while(L < R){
				LL mid = (L + R) >> 1;
				if (mid * t < sum[n]){
					L = mid + 1;
				}
				else{
					R = mid;
				}
			}
			cout << L << "\n";
		}
	}
	return 0;
}

因为总水量固定,所以可以直接通过总量除时间向上取整求出答案。

#include <bits/stdc++.h>
using namespace std;
#define LL long long
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	LL n;
	cin >> n;
	vector <LL> v(n), sum(n + 1);
	LL mx = 0;
	for (int i = 0; i < n; i ++ ){
		cin >> v[i];
		sum[i + 1] = sum[i] + v[i];
		mx = max(mx, (sum[i + 1] + i) / (i + 1));
	}
	LL q;
	cin >> q;
	while (q -- ){
		LL t;
		cin >> t;
		if (t < mx){
			cout << "-1\n";
		}
		else{
			cout << (sum[n] + t - 1) / t << "\n";
		}
	}
	return 0;
}
posted on 2022-07-10 11:13  Hamine  阅读(32)  评论(0编辑  收藏  举报