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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章