codeforces #592(Div.2)

codeforces #592(Div.2)

A Pens and Pencils

 

Tomorrow is a difficult day for Polycarp: he has to attend a lectures and b practical classes at the university! Since Polycarp is a diligent student, he is going to attend all of them.

While preparing for the university, Polycarp wonders whether he can take enough writing implements to write all of the lectures and draw everything he has to during all of the practical classes. Polycarp writes lectures using a pen (he can't use a pencil to write lectures!); he can write down cc lectures using one pen, and after that it runs out of ink. During practical classes Polycarp draws blueprints with a pencil (he can't use a pen to draw blueprints!); one pencil is enough to draw all blueprints during d practical classes, after which it is unusable.

Polycarp's pencilcase can hold no more than k writing implements, so if Polycarp wants to take x pens and y pencils, they will fit in the pencilcase if and only if x+y≤k.

Now Polycarp wants to know how many pens and pencils should he take. Help him to determine it, or tell that his pencilcase doesn't have enough room for all the implements he needs tomorrow!

Note that you don't have to minimize the number of writing implements (though their total number must not exceed k).

Input

The first line of the input contains one integer tt (1≤t≤100) — the number of test cases in the input. Then the test cases follow.

Each test case is described by one line containing five integers a, b, c, d and k, separated by spaces (1≤a,b,c,d,k≤100) — the number of lectures Polycarp has to attend, the number of practical classes Polycarp has to attend, the number of lectures which can be written down using one pen, the number of practical classes for which one pencil is enough, and the number of writing implements that can fit into Polycarp's pencilcase, respectively.

In hacks it is allowed to use only one test case in the input, so t=1should be satisfied.

Output

For each test case, print the answer as follows:

If the pencilcase can't hold enough writing implements to use them during all lectures and practical classes, print one integer −1. Otherwise, print two non-negative integers x and y — the number of pens and pencils Polycarp should put in his pencilcase. If there are multiple answers, print any of them. Note that you don't have to minimize the number of writing implements (though their total number must not exceed k).

Example

input

3
7 5 4 5 8
7 5 4 5 2
20 53 45 26 4

output

7 1
-1
1 3

Note

There are many different answers for the first test case; x=7,y=1 is only one of them. For example, x=3, y=1 is also correct.

x=1, y=3 is the only correct answer for the third test case.

题目大意:有一名非常勤奋好学的学生,要为明天的课程准备钢笔和铅笔,他有一个笔袋最多装k只笔。题中主要介绍了两种课程,一种要使用钢笔,一种要使用铅笔(两种笔只能在相应的课上使用),使用钢笔的课程有a大小的工作量,一支钢笔最多完成c大小的工作量就用完了;使用铅笔的课程有b大小的工作量,一支铅笔最多完成d大小的工作量。T组数据输入,对于每组数据,如果该学生能按照课程要求准备好笔则输出笔袋中铅笔数和钢笔数(不要求一定要把笔袋装满,但一定要满足课程要求)否则输出-1

解题思路:原题目的题面说的比较花,但总结起来就上面的内容并不算多,翻译过来后一眼就能看出来,按照题意模拟一遍即可。代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int a,b,c,d,k,x,y;
int main()
{
	int t;
	cin>>t;
	while(t--){
		scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
		x=a/c;
		if(a%c!=0)x++;
		y=b/d;
		if(b%d!=0)y++;
		if(x+y>k)cout<<"-1"<<endl;
		else cout<<k-y<<" "<<y<<endl;
	}
	return 0; 
}

 B.Rooms and Staircases

Nikolay lives in a two-storied house. There are nn rooms on each floor, arranged in a row and numbered from one from left to right. So each room can be represented by the number of the floor and the number of the room on this floor (room number is an integer between 1 and n).

If Nikolay is currently in some room, he can move to any of the neighbouring rooms (if they exist). Rooms with numbers i and i+1 on each floor are neighbouring, for all 1≤i≤n−1. There may also be staircases that connect two rooms from different floors having the same numbers. If there is a staircase connecting the room x on the first floor and the room on the second floor, then Nikolay can use it to move from one room to another.

The picture illustrates a house with n=4. There is a staircase between the room 2 on the first floor and the room 2 on the second floor, and another staircase between the room 4 on the first floor and the room on the second floor. The arrows denote possible directions in which Nikolay can move.

The picture corresponds to the string "0101" in the input.

Nikolay wants to move through some rooms in his house. To do this, he firstly chooses any room where he starts. Then Nikolay moves between rooms according to the aforementioned rules. Nikolay never visits the same room twice (he won't enter a room where he has already been).

Calculate the maximum number of rooms Nikolay can visit during his tour, if:

  • he can start in any room on any floor of his choice,
  • and he won't visit the same room twice.

Input

The first line of the input contains one integer tt (1≤t≤100) — the number of test cases in the input. Then test cases follow. Each test case consists of two lines.

The first line contains one integer nn (1≤n≤1000) — the number of rooms on each floor.

The second line contains one string consisting of nn characters, each character is either a '0' or a '1'. If the ii-th character is a '1', then there is a staircase between the room i on the first floor and the room i on the second floor. If the i-th character is a '0', then there is no staircase between the room i on the first floor and the room i on the second floor.

In hacks it is allowed to use only one test case in the input, so t=1 should be satisfied.

Output

For each test case print one integer — the maximum number of rooms Nikolay can visit during his tour, if he can start in any room on any floor, and he won't visit the same room twice.

Example

input

4
5
00100
8
00000000
5
11111
3
110

output

6
8
10
6

Note

In the first test case Nikolay may start in the first room of the first floor. Then he moves to the second room on the first floor, and then — to the third room on the first floor. Then he uses a staircase to get to the third room on the second floor. Then he goes to the fourth room on the second floor, and then — to the fifth room on the second floor. So, Nikolay visits 66 rooms.

There are no staircases in the second test case, so Nikolay can only visit all rooms on the same floor (if he starts in the leftmost or in the rightmost room).

In the third test case it is possible to visit all rooms: first floor, first room → second floor, first room → second floor, second room → first floor, second room → first floor, third room → second floor, third room → second floor, fourth room → first floor, fourth room → first floor, fifth room → second floor, fifth room.

In the fourth test case it is also possible to visit all rooms: second floor, third room → second floor, second room → second floor, first room → first floor, first room → first floor, second room → first floor, third room.

题目大意:小N来到 了一个神奇的酒店。酒店分两层,有的房间与另一层之间有楼梯连接,有的房间则没有,在不走已经走过的房间的情况下,小N想知道自己最多能走过几个房间。t组输入,每组数据分为第一行为 n代表每一层的房间数,接下来输入一串长度为n的01串,如果是0表示该房间没有楼梯,如果是1则说明该房间与另一层有楼梯。输出他最大能去几个房间,可以从任意房间开始,在房间中的走法与图片所示一致。

解题思路:先思考走法,比较合理的走法是从距离楼梯较远的一端走到楼梯再通过楼梯进入另一层走回去(实际情况中楼梯可能出现多个,我们选择离两端最远的楼梯作为改变楼层的楼梯)。考虑到楼梯出现与否是与01串相关的,所以有最多的情况是如果首或尾出现1,则他可以从没有楼梯的一端出发,走到房间尽头然后通过楼梯进入下一层,再走完另一层的房间,此时最大经过房间数是2n;最少的情况是所有的房间都没有楼梯,则他只能从一端走到另一端,此时最大经过房间数是n;然后就是在01串中间出现1(楼梯了),可以设定两个变量,分别为两端第一次出现楼梯的房间编号(从0开始计,a在左边,b在右边),假设只出现1个“1”时,可以找到离该楼梯最远的一端出发,走个来回,假设出现了多个“1”,则选择判断是a左边的房间多还是b右边的房间多,选择可以走最多房间的情况(即距离端点房间数少的)。T组输入,代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 1e9
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--)
const int N=1e3+10;
const int mod=1e9+7;
int n,x,a,b;
int main()
{
	int t;
	cin>>t;
	string s;
	while(t--){
		scanf("%d",&n);
		int sum=n;
		cin>>s;
		if(s[0]=='1'||s[n-1]=='1')cout<<sum*2<<endl;
		else {
			a=0,b=0;
			a=s.find("1");
			per(i,s.length()-1,0){
				if(s[i]=='1'){
					b=i;
					break;
				}
			}
			if(b==0)cout<<sum<<endl;
			else{
				if(a==b)sum=(a+1)*2>(n-b)*2?(a+1)*2:(n-b)*2;
				else sum=(n-a)*2>(b+1)*2?(n-a)*2:(b+1)*2;
				cout<<sum<<endl;
			}
 
		}
	}
	return 0; 
}

 C.The Football Season

The football season has just ended in Berland. According to the rules of Berland football, each match is played between two teams. The result of each match is either a draw, or a victory of one of the playing teams. If a team wins the match, it gets ww points, and the opposing team gets 00 points. If the game results in a draw, both teams get dd points.

The manager of the Berland capital team wants to summarize the results of the season, but, unfortunately, all information about the results of each match is lost. The manager only knows that the team has played nn games and got pp points for them.

You have to determine three integers x, y and z — the number of wins, draws and loses of the team. If there are multiple answers, print any of them. If there is no suitable triple (x,y,z) report about it.

Input

The first line contains four integers n, p, w and d (1<=n<=1e12,0<=p<=1e17,1<=d<w<=1e5 )— the number of games, the number of points the team got, the number of points awarded for winning a match, and the number of points awarded for a draw, respectively. Note that w>d, so the number of points awarded for winning is strictly greater than the number of points awarded for draw.

Output

If there is no answer, print −1−1.

Otherwise print three non-negative integers xx, yy and zz — the number of wins, draws and losses of the team. If there are multiple possible triples (x,y,z)(x,y,z), print any of them. The numbers should meet the following conditions:

  • x⋅w+y⋅d=p,
  • x+y+z=n.

Examples

input

30 60 3 1

output

17 9 4

input

10 51 5 4

output

-1

input

20 0 15 5

output

0 0 20

Note

One of the possible answers in the first example — 17 wins, 9 draws and 4 losses. Then the team got 17⋅3+9⋅1=60 points in 17+9+4=30 games.

In the second example the maximum possible score is 10⋅5=50. Since p=51, there is no answer.

In the third example the team got 0 points, so all 20games were lost.

题目大意:足球队两两比赛,赢了获得w分,平局获得d分,输了获得0分(w严格大于d)。但在上个赛季中某球队的比赛数据丢失了部分,经理仅知道比过多少场以及最后的分,请你帮他算出x胜场y平局z负场。若没有结果输出-1.

解题思路:对于三种结果x、y、z,一定有x*m+y*d=p,x+y+z=n;显而易见输了的没分,所以就不用刻意算负场,算完等分的场次后易求,问题就变成求x和y。可以先排除几种没有结果的情况,①n场比赛都赢了也达不到p分时;②总得分为奇数,而胜场和负场得分均为偶数时。那么是通过y值的改变去求x呢,还是通过改变x值去求y?我当时的想法是用x求y,pp了。但综测的时候被1000000000000 1000000000000 6 3 这组数据卡超时了。逆推后,有这么个想法,因为负场不得分,所以可以是x尽可能大y尽可能小,这时就有y*d<w,因为如果y*d比胜场分多时,完全可以再赢一场,而为了避免出现y*d%w==0的情况,可以对d进行极值处理,d最小为1所以最极端的情况是y<=w.且y*d<=p。代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 1e9
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--)
const int N=1e3+10;
const int mod=1e9+7;
LL n,p,w,d,x,y,z;
int main()
{
	cin>>n>>p>>w>>d;
	if(w*n<p||(p%2!=0&&w%2==0&&d%2==0))cout<<"-1";
	else {
		for(y=0;y*d<=p&&y<=w;y++){
			if((p-y*d)%w)continue;
			x=(p-y*d)/w;
			if(n-y-x>=0){
				cout<<x<<" "<<y<<" "<<n-y-x;
				z=1;
				break;
			}
		}
	    if(z!=1)cout<<"-1"<<endl;
	}
	return 0;
}

 

E  .Minimizing Difference

You are given a sequence a1,a2,…,an consisting of nn integers.

You may perform the following operation on this sequence: choose any element and either increase or decrease it by one.

Calculate the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.

Input

The first line contains two integers nn and kk (2≤n≤1e5,1<=k<=1e14) — the number of elements in the sequence and the maximum number of times you can perform the operation, respectively.

The second line contains a sequence of integers a1,a2,…,an(1≤ai≤1e9)

Output

Print the minimum possible difference between the maximum element and the minimum element in the sequence, if you can perform the aforementioned operation no more than k times.

Examples

input

4 5
3 1 7 5

output

2

input

3 10
100 100 100

output

0

input

10 9
4 5 5 7 5 4 5 2 4 3

output

1

Note

In the first example you can increase the first element twice and decrease the third element twice, so the sequence becomes [3,3,5,5] and the difference between maximum and minimum is 2. You still can perform one operation after that, but it's useless since you can't make the answer less than 2.

In the second example all elements are already equal, so you may get 0 as the answer even without applying any operations.

题意: 给定长度为n的序列n,可以进行k次操作(每次操作为增加1或减1),问在k次操作内能使序列中最大值最小值差值改变为多小。

思路:首先,我们只关心最小值和最大值变为最小。可以先进行排序,然后逐渐更新左右两边的极值。声明左指针为left,右指针为right,假设我们将序列存进数组a[N],N=1e5+10;从1开始计数,则left=1,right=n。

根据题目要求,我们保证每次更新后left所指值为最小值,right值为最大值,明显的,序列左边值应该增加,右边应该减小。以a[left]为例,只考虑一般情况,当a[left]值更新几次后,会有a[left]>=a[left+1],所以最合适的更新次数应该是a[left+1]-a[left],更新后,,这时left指针左边也有数据了,我们可以用ctl来表示左边要更新的数有几个,而数组是从1开始计数的所以left大小即为要更新的数ctl(相应的右边的数为ctr=(n-right+1)),那么要更新多少次呢?设更新次数为temp,则有temp=ctl*(a[left+1]-a[left]),如果k<=temp,则操作用完,a[left]+=k/ctl;反之更新k值,left,right值,开始下一次循环。实际操作中我们应该尽量少更新数这样可能操作更多次,所以是更新left指针还是right指针取决于(ctl与ctr的值)。代码如下:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int,int> ii;
#define inf 1e9
#define F first
#define S second
#define dbg(x) cout<<#x<<" value : "<<x<<"\n";
#define rep(i,j,k) for(int i = (j); i <= (k); i++)
#define rep__(i,j,k) for(int i = (j); i < (k); i++)
#define per(i,j,k) for(int i = (j); i >= (k); i--)
#define per__(i,j,k) for(int i = (j); i > (k); i--)
#define mst(a,b) memset(a,b,sizeof(a))
#define sf(n) scanf("%d",&n)
#define pf(n) printf("%d\n",n)
#define sllf(n) scanf("%lld",&n)
#define pllf(n) printf("%lld\n",n)
const int N=1e5+10;
const int mod=1e9+7;
LL a[N];
int main()
{
	int n;
	LL k;
	sllf(n);
	sllf(k);
	rep(i,1,n)sf(a[i]);
	sort(a+1,a+1+n);
	LL left=1,right=n;
	while(a[left]==a[left+1])left++;
	while(a[right]==a[right-1])right--;
	while(k&&left<right){
		LL ctl=left,ctr=(n-right+1);
		if(ctl<ctr){
			LL temp=ctl*(a[left+1]-a[left]);
			if(k<=temp){
				a[left]+=k/ctl;
				break;
			}
			k-=temp;
			a[left]=a[left+1];
		}else{
			LL temp=ctr*(a[right]-a[right-1]);
			if(k<=temp){
				a[right]-=k/ctr;
				break;
			}
			k-=temp;
			a[right]=a[right-1];
		}
		while(a[left]==a[left+1])left++;
		while(a[right]==a[right-1])right--;
	}
	pllf(a[right]-a[left]);
	return 0;
}

 

posted @ 2019-10-14 08:53  whocarethat  阅读(178)  评论(0编辑  收藏  举报