Codeforces Round 96 (Rated for Div. 2)

这篇博客探讨了如何运用编程技巧解决实际的数学和逻辑挑战。文章通过三个具体的例子——A题的房间窗户数量计算,B题的桶中水量分配以及C题的白板上数字操作——展示了如何利用C++语言来找到最优解。在A题中,通过分析窗户数的尾数确定可能的公寓类型数量;B题中,通过排序和倒水操作找出最大水量差;C题中,通过每次选取两数相加取整来最小化最后的数值。这些案例揭示了编程在解决复杂问题时的有效性和灵活性。

A. Number of Apartments
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Recently a new building with a new layout was constructed in Monocarp’s hometown. According to this new layout, the building consists of three types of apartments: three-room, five-room, and seven-room apartments. It’s also known that each room of each apartment has exactly one window. In other words, a three-room apartment has three windows, a five-room — five windows, and a seven-room — seven windows.

Monocarp went around the building and counted n windows. Now he is wondering, how many apartments of each type the building may have.

Unfortunately, Monocarp only recently has learned to count, so he is asking you to help him to calculate the possible quantities of three-room, five-room, and seven-room apartments in the building that has n windows. If there are multiple answers, you can print any of them.

Here are some examples:

if Monocarp has counted 30 windows, there could have been 2 three-room apartments, 2 five-room apartments and 2 seven-room apartments, since 2⋅3+2⋅5+2⋅7=30;
if Monocarp has counted 67 windows, there could have been 7 three-room apartments, 5 five-room apartments and 3 seven-room apartments, since 7⋅3+5⋅5+3⋅7=67;
if Monocarp has counted 4 windows, he should have mistaken since no building with the aforementioned layout can have 4 windows.
Input
Th first line contains one integer t (1≤t≤1000) — the number of test cases.

The only line of each test case contains one integer n (1≤n≤1000) — the number of windows in the building.

Output
For each test case, if a building with the new layout and the given number of windows just can’t exist, print −1.

Otherwise, print three non-negative integers — the possible number of three-room, five-room, and seven-room apartments. If there are multiple answers, print any of them.

Example
inputCopy
4
30
67
4
14
outputCopy
2 2 2
7 5 3
-1
0 0 2
A题题意:有三种户型,分别是3,5,7个窗户,现在已知窗户总数,求这三种户型的可能数量。
可以根据总窗户数 n 的尾数进行判断,减去某数后正好可以被5整除。

#include<stdio.h>
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		if(n%10==0)
		{
			printf("0 %d 0\n",n/5);
		}
		else if(n%10==1)
		{
			if(n<10)
			printf("-1\n");
			else
			printf("2 %d 0\n",(n-6)/5);
		}
		else if(n%10==2)
		{
			if(n<10)
			printf("-1\n");
			else
			printf("0 %d 1\n",(n-7)/5);
		}
		else if(n%10==3)
		{
			printf("1 %d 0\n",(n-3)/5); 
		} 
		else if(n%10==4)
		{
			if(n<10)
			printf("-1\n");
			else
			printf("3 %d 0\n",(n-9)/5); 
		} 
		else if(n%10==5)
		{
			printf("0 %d 0\n",n/5);
		}
		else if(n%10==6)
		{
			printf("2 %d 0\n",(n-6)/5);
		}
		else if(n%10==7)
		{
			printf("0 %d 1\n",(n-7)/5);
		}
		else if(n%10==8)
		{
			printf("1 %d 0\n",(n-3)/5);
		}
		else if(n%10==9)
		{
			printf("3 %d 0\n",(n-9)/5);
		}
	}
	return 0;
} 

B. Barrels
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have n barrels lined up in a row, numbered from left to right from one. Initially, the i-th barrel contains ai liters of water.

You can pour water from one barrel to another. In one act of pouring, you can choose two different barrels x and y (the x-th barrel shouldn’t be empty) and pour any possible amount of water from barrel x to barrel y (possibly, all water). You may assume that barrels have infinite capacity, so you can pour any amount of water in each of them.

Calculate the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most k times.

Some examples:

if you have four barrels, each containing 5 liters of water, and k=1, you may pour 5 liters from the second barrel into the fourth, so the amounts of water in the barrels are [5,0,5,10], and the difference between the maximum and the minimum is 10;
if all barrels are empty, you can’t make any operation, so the difference between the maximum and the minimum amount is still 0.
Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.

The first line of each test case contains two integers n and k (1≤k<n≤2⋅105) — the number of barrels and the number of pourings you can make.

The second line contains n integers a1,a2,…,an (0≤ai≤109), where ai is the initial amount of water the i-th barrel has.

It’s guaranteed that the total sum of n over test cases doesn’t exceed 2⋅105.

Output
For each test case, print the maximum possible difference between the maximum and the minimum amount of water in the barrels, if you can pour water at most k times.

Example
inputCopy
2
4 1
5 5 5 5
3 2
0 0 0
outputCopy
10
0
B题题意:有n个桶,可随意倒水,使得桶中水的(最大值减最小值)最大。
可以通过排序确定最大值,然后从最大值依据倒水的次数k,逐个相加,即可算出最大值。

#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int main(){
	int t;
	cin >> t;
	while(t--){
		int n, k;
		cin >> n >> k;
		int x, min = 0x3f3f3f3f;
		priority_queue< int > s;//插入队列中自动排序,队尾的数最大。 
		for(int i = 0; i < n; i++){
			cin >> x;
			s.push(x);
			min = min < x ? min : x;
		}
		long long ans = 0;//注意要用long long, 因为每个数字的范围都是(0≤ai≤109),相加后会大于int的最大值 
		if(k == 0){ 
			cout << s.top()-min << endl;
		}
		else{ 
		    ans = ans + s.top();
		    s.pop();
			for(int i = 0; !s.empty() && i < k ; i++){
			    ans = ans + s.top();
			    s.pop();
		    }
		    cout << ans << endl;
		}	
	}
	return 0;
}

C. Numbers on Whiteboard
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Numbers 1,2,3,…n (each integer from 1 to n once) are written on a board. In one operation you can erase any two numbers a and b from the board and write one integer a+b2 rounded up instead.

You should perform the given operation n−1 times and make the resulting number that will be left on the board as small as possible.

For example, if n=4, the following course of action is optimal:

choose a=4 and b=2, so the new number is 3, and the whiteboard contains [1,3,3];
choose a=3 and b=3, so the new number is 3, and the whiteboard contains [1,3];
choose a=1 and b=3, so the new number is 2, and the whiteboard contains [2].
It’s easy to see that after n−1 operations, there will be left only one number. Your goal is to minimize it.

Input
The first line contains one integer t (1≤t≤1000) — the number of test cases.

The only line of each test case contains one integer n (2≤n≤2⋅105) — the number of integers written on the board initially.

It’s guaranteed that the total sum of n over test cases doesn’t exceed 2⋅105.

Output
For each test case, in the first line, print the minimum possible number left on the board after n−1 operations. Each of the next n−1 lines should contain two integers — numbers a and b chosen and erased in each operation.

Example
inputCopy
1
4
outputCopy
2
2 4
3 3
3 1
取任意两个数a,b,将(a+b)/2放回数组,使最后留下的数最小。
应该从大数开始计算,需要两个数加起来是偶数,才不会丢失精度,所以,要先算相加为偶数的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=200010;
int a[N][2];//用来储存每一步取得的两个数
int main()
{
	int t;
	scanf("%d",&t);
    while(t--){
    	int n;
    	scanf("%d",&n);
    	priority_queue<int> s;
    	for(int i=1;i<=n;i++){
    		s.push(i);
		}
		int cnt=0;
		int m=0;
	    if(n==2)
	    {
	    	printf("2\n");
	    	printf("2 1\n");
		}
		else{
			while(s.size()!=1){
			a[cnt][0]=s.top();
			s.pop();
			a[cnt][1]=s.top();
			s.pop();
			if((a[cnt][0]+a[cnt][1])%2!=0)
			{
				int p=s.top();
				s.pop();
				s.push(a[cnt][1]);
				a[cnt][1]=p;
			}
			s.push((a[cnt][0]+a[cnt][1])/2);
			cnt++;
		    }
		    printf("%d\n",s.top());//最后队列中留下的数就是那个最小数
	    	for(int i=0;i<cnt;i++){
		    	printf("%d %d\n",a[i][0],a[i][1]);
		    }
		}	
	}
	return 0;
 } 
"educational codeforces round 103 (rated for div. 2)"是一个Codeforces平台上的教育性比赛,专为2级选手设计评级。以下是有关该比赛的回答。 "educational codeforces round 103 (rated for div. 2)"是一场Codeforces平台上的教育性比赛。Codeforces是一个为程序员提供竞赛和评级的在线平台。这场比赛是专为2级选手设计的,这意味着它适合那些在算法和数据结构方面已经积累了一定经验的选手参与。 与其他Codeforces比赛一样,这场比赛将由多个问题组成,选手需要根据给定的问题描述和测试用例,编写程序来解决这些问题。比赛的时限通常有两到三个小时,选手需要在规定的时间内提交他们的解答。他们的程序将在Codeforces的在线评测系统上运行,并根据程序的正确性和效率进行评分。 该比赛被称为"educational",意味着比赛的目的是教育性的,而不是针对专业的竞争性。这种教育性比赛为选手提供了一个学习和提高他们编程技能的机会。即使选手没有在比赛中获得很高的排名,他们也可以从其他选手的解决方案中学习,并通过参与讨论获得更多的知识。 参加"educational codeforces round 103 (rated for div. 2)"对于2级选手来说是很有意义的。他们可以通过解决难度适中的问题来测试和巩固他们的算法和编程技巧。另外,这种比赛对于提高解决问题能力,锻炼思维和提高团队合作能力也是非常有帮助的。 总的来说,"educational codeforces round 103 (rated for div. 2)"是一场为2级选手设计的教育性比赛,旨在提高他们的编程技能和算法能力。参与这样的比赛可以为选手提供学习和进步的机会,同时也促进了编程社区的交流与合作。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值