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;
}