Codeforces Round #726 (Div. 2)

又是天崩开局…

A. Arithmetic Array

An array b of length k is called good if its arithmetic mean is equal to 1. More formally, if

Note that the value is not rounded up or down. For example, the array [1,1,1,2] has an arithmetic mean of 1.25, which is not equal to 1.

You are given an integer array a of length n. In an operation, you can append a non-negative integer to the end of the array. What’s the minimum number of operations required to make the array good?

We have a proof that it is always possible with finitely many operations.

Input
The first line contains a single integer t (1≤t≤1000) — the number of test cases. Then t test cases follow.

The first line of each test case contains a single integer n (1≤n≤50) — the length of the initial array a.

The second line of each test case contains n integers a1,…,an (−104≤ai≤104), the elements of the array.

Output
For each test case, output a single integer — the minimum number of non-negative integers you have to append to the array so that the arithmetic mean of the array will be exactly 1.

  • 题意:给定一个数字序列,求至少往里面加入多少个非负整数就能使整个序列的算术平均值为1
  • 思路:将数字序列求和得到sum,以sum与n的关系分类讨论
    • 首先,当sum < n时,只需要添加一个n - sum + 1即可
    • 其次,当sum >= n时,只需要添加sum - n个0即可
  • 代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t, n, m;
inline void solve() {
	cin >> n;
	ll sum = 0;
	for(int i = 1; i <= n; i++) {
		ll x; cin >> x;
		sum += x;
	}
	if(sum < n) cout << 1 << endl;
	else cout << sum - n << endl;
	return ;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
} 

B. Bad Boy

Riley is a very bad boy, but at the same time, he is a yo-yo master. So, he decided to use his yo-yo skills to annoy his friend Anton.

Anton’s room can be represented as a grid with n rows and m columns. Let (i,j) denote the cell in row i and column j. Anton is currently standing at position (i,j) in his room. To annoy Anton, Riley decided to throw exactly two yo-yos in cells of the room (they can be in the same cell).

Because Anton doesn’t like yo-yos thrown on the floor, he has to pick up both of them and return back to the initial position. The distance travelled by Anton is the shortest path that goes through the positions of both yo-yos and returns back to (i,j) by travelling only to adjacent by side cells. That is, if he is in cell (x,y) then he can travel to the cells (x+1,y), (x−1,y), (x,y+1) and (x,y−1) in one step (if a cell with those coordinates exists).

Riley is wondering where he should throw these two yo-yos so that the distance travelled by Anton is maximized. But because he is very busy, he asked you to tell him.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of each test case contains four integers n, m, i, j (1≤n,m≤109, 1≤i≤n, 1≤j≤m) — the dimensions of the room, and the cell at which Anton is currently standing.

Output
For each test case, print four integers x1, y1, x2, y2 (1≤x1,x2≤n, 1≤y1,y2≤m) — the coordinates of where the two yo-yos should be thrown. They will be thrown at coordinates (x1,y1) and (x2,y2).

If there are multiple answers, you may print any.

  • 题意:将两个球放两个位置上,问怎么走(捡到球再回去)才能使路径最远。
  • 思路:对顶角即可,最短距离都是这个矩形的周长
  • 代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll N = 1e6 + 10;
ll t, n, m, x, y;
inline void solve() {
	cin >> n >> m >> x >> y;
	cout << 1 << " " << 1 << " " << n << " " << m << endl;
	return ;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
} 

C. Challenging Cliffs

You are a game designer and want to make an obstacle course. The player will walk from left to right. You have n heights of mountains already selected and want to arrange them so that the absolute difference of the heights of the first and last mountains is as small as possible.

In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains i (1≤i<n) such that hi≤hi+1 where hi is the height of the i-th mountain. You don’t want to waste any of the mountains you modelled, so you have to use all of them.

From all the arrangements that minimize |h1−hn|, find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.

Input
The first line will contain a single integer t (1≤t≤100) — the number of test cases. Then t test cases follow.

The first line of each test case contains a single integer n (2≤n≤2⋅105) — the number of mountains.

The second line of each test case contains n integers h1,…,hn (1≤hi≤109), where hi is the height of the i-th mountain.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output
For each test case, output n integers — the given heights in an order that maximizes the difficulty score among all orders that minimize |h1−hn|.

If there are multiple orders that satisfy these requirements, you may output any.

  • 题意:
    给定一个数字序列,求满足以下两点要求的新组合的数字序列,要求如下:1.首尾的绝对值差最小,2.数字序列难度最大(x[i] >= x[i - 1],那么难度贡献值+1)。

  • 思路:
    首先将数字序列排序,找到相差最小的两项作为头部尾部,其次从除头尾的剩余数字中找到第一个比头元素大的,依次输出,最后再把未输出的元素挨着输出一遍。

  • 代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll N = 1e6 + 10;
ll t, n, m, x[N];
inline void solve() {
	cin >> n;
	int flag = 0; 
	for(int i = 1; i <= n; i++) cin >> x[i];
	sort(x + 1, x + n + 1);
	int st, ed, now = 0x3f3f3f3f3f3f3f;
	for(int i = 2; i <= n; i++) {
		if(x[i] - x[i - 1] < now) {
			st = i - 1;
			ed = i;
			now = x[i] - x[i - 1];
		}
	}
	vector<int> ans;
	int ok1 = 0, ok2 = 0;
	cout << x[st] << " ";
	for(int i = 1; i <= n; i++) {
		if(ok1 == 0 && x[i] == x[st]) {
			ok1 = 1; continue;	
		}
		if(ok2 == 0 && x[i] == x[ed]) {
			ok2 = 1; continue;
		}
		ans.push_back(x[i]);
	}
	if(n > 2) {
		int pos = 0;
		for(int i = 0; i < ans.size(); i++) 
			if(ans[i] > x[st]) {
				pos = i; break;
		}
		for(int i = pos; i < ans.size(); i++) cout << ans[i] << " ";
		for(int i = 0; i < pos; i++) cout << ans[i] << " ";
	}
	cout << x[ed] << endl;
	return ;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}

D. Deleting Divisors

Alice and Bob are playing a game.

They start with a positive integer n and take alternating turns doing operations on it. Each turn a player can subtract from n one of its divisors that isn’t 1 or n. The player who cannot make a move on his/her turn loses. Alice always moves first.

Note that they subtract a divisor of the current number in each turn.

You are asked to find out who will win the game if both players play optimally.

Input
The first line contains a single integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

Each test case contains a single integer n (1≤n≤109) — the initial number.

Output
For each test case output “Alice” if Alice will win the game or “Bob” if Bob will win, if both players play optimally.

  • 思路:打表找规律

  • 代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t, n, m;
inline void solve() {
	cin >> n;
	if(n % 2 == 1) {
		cout << "Bob" << endl; 
		return ;
	} 
	int cnt = 1;
	for(; pow(2, cnt) <= n; cnt+=2) {
		if(pow(2, cnt) == n) {
			cout << "Bob" << endl; 
			return ;
		}
	}
	cout << "Alice" << endl;
	return ;
}
int main() {
	ios::sync_with_stdio(false); cin.tie(0);
	cin >> t;
	while(t--) {
		solve();
	}
	return 0;
}

E1. Erase and Extend (Easy Version)

This is the easy version of the problem. The only difference is the constraints on n and k. You can make hacks only if all versions of the problem are solved.

You have a string s, and you can do two types of operations on it:

Delete the last character of the string.
Duplicate the string: s:=s+s, where + denotes concatenation.
You can use each operation any number of times (possibly none).

Your task is to find the lexicographically smallest string of length exactly k that can be obtained by doing these operations on string s.

A string a is lexicographically smaller than a string b if and only if one of the following holds:

a is a prefix of b, but a≠b;
In the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b.
Input
The first line contains two integers n, k (1≤n,k≤5000) — the length of the original string s and the length of the desired string.

The second line contains the string s, consisting of n lowercase English letters.

Output
Print the lexicographically smallest string of length k that can be obtained by doing the operations on string s.

  • 题意:给定一个字符串,通过删除字符串的最后一个字符或者复制该字符串,找到长度恰好为k的按字典顺序最小的字符串

  • 思路:首先找到字典序最小的循环节,由于只能从后开始删除,所以开头必须保留,这样就得到思路,找到该循环节即可。

  • 代码:

#include <bits/stdc++.h>
using namespace std;
int main() {
	string s; int k, n; cin >> n >> k >> s;
	int now = 0, ans = 0; 
	for(int i = 1; i < s.size(); i++) {
		if(s[i] > s[now]) break;
		else if(s[i] == s[now]) now++;
		else {
			ans = i;
			now = 0;
		}
	}
	now = 0;
	while(k--) {
		cout << s[now];
		if(now == ans) now = 0;
		else now++;
	}
	return 0;
}

E2. Erase and Extend (Hard Version)

  • 思路代码:同上
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

顶白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值