Educational Codeforces Round 158 (Rated for Div. 2)

A

#include<bits/stdc++.h>
 
using namespace std;
using ll = long long;
const int maxn = 20;
const int MOD = 998244353;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}	
int d1[maxn][maxn], d2[maxn][maxn];
 
inline void solve() {
	int n, x; cin >> n >> x;
	vector<int>a(n);
	
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int ans = a[0];
	for (int i = 1; i < n; i++) {
		ans = max(ans, a[i] - a[i - 1]);
	}
	ans = max(ans, 2 * (x - a[n - 1]));
	cout << ans << '\n';
}
 
signed main(){
 
	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);
 
	int t = 1;
	cin >> t;
	while (t--)
		solve();
 
	return 0;
}

B

记得开long long,只有a[i+1]>a[i]才需要增加传送次数

#include<bits/stdc++.h>
 
using namespace std;
using ll = long long;
const int maxn = 20;
const int MOD = 998244353;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}	
int d1[maxn][maxn], d2[maxn][maxn];
 
inline void solve() {
	int n; cin >> n;
	vector<int>a(n);
	vector<int>dp(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	dp[0] = a[0] - 1;
	if (n == 1) { cout << a[0] - 1 << '\n'; return; }
	for (int i = 1; i < n; i++) {
		dp[i] = dp[i - 1] + (a[i] > a[i - 1] ? a[i] - a[i - 1] : 0);
	}
	cout << dp[n - 1] << '\n';
}
 
signed main(){
 
	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);
 
	int t = 1;
	cin >> t;
	while (t--)
		solve();
 
	return 0;
}

C

每次操作可使两数差距变为原来差距的一半向下取整,只最大值和最小值,这两个相等别的也相等了

#include<bits/stdc++.h>
 
using namespace std;
using ll = long long;
const int maxn = 20;
const int MOD = 998244353;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}	
 
inline void solve() {
	int n; cin >> n;
	vector<ll>a(n);
 
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
 
	if (n == 1) { cout << 0 << '\n'; return; }
	sort(a.begin(), a.end());
	if (a[0]==a[n-1]) { cout << 0 << '\n'; return; }
 
	ll sum = a[n - 1] - a[0];
	int ans = 0;
	while (sum) {
		sum /= 2;
		ans++;
	}
	cout << ans << '\n';
	if (ans <= n) {
		for (int i = 0; i < ans; i++)
			cout << a[0] << ' ';
		cout << '\n';
	}
}
 
signed main(){
 
	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);
 
	int t = 1;
	cin >> t;
	while (t--)
		solve();
 
	return 0;
}

D

我们考虑令i为起点,那么在i左边的位置j最坏情况需要以a[j]+n-j为开头,在i右边的位置k最坏情况需要以a[k]+k-1为开头,那么如果以i为起点,需要的x为a[i]、前缀的最坏情况的最大值、后缀最坏情况的最大值中的最大值。又由于第一步要最优,那么我们只需以1~n为起点需要的x中的最小值即可,从最大值开始并不一定是最优

#include<bits/stdc++.h>

using namespace std;
using ll = long long;
const int maxn = 20;
const int MOD = 998244353;
int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}	

inline void solve() {
	int n; cin >> n;
	vector<ll>a(n + 2);
	vector<ll>ml(n + 2), mr(n + 2);
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		ml[i] = a[i] + n - i;
		mr[i] = a[i] + i - 1;
	}
	
	ll t = 0;

	for (int i = 1; i <= n; i++) {
		t = max(ml[i], t);
		ml[i] = t;
	}

	ans = max(ans,t);
	t = 0;

	for (int i = n; i >= 1; i--) {
		t = max(mr[i], t);
		mr[i] = t;
	}
	ans = max(ans, t);

	for (int i = 1; i <= n; i++) {
		ans = min(ans, max(max(mr[i + 1], ml[i - 1]), a[i]));
	}
	std::cout << ans << '\n';
}

signed main(){

	ios::sync_with_stdio(false);
	cin.tie(0);
	std::cout.tie(0);

	int t = 1;
	//cin >> t;
	while (t--)
		solve();

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值