Nebius Welcome Round (div.1 + div.2)题解

目录

A. Lame King

题面翻译

思路:

代码实现

B. Vaccination

题面翻译:

思路:

代码实现

C. Pull Your Luck

思路:

代码实现

D. Accommodation

题面翻译

思路

代码实现

E. Routing


A. Lame King

You are given a checkerboard of size 201×201, i. e. it has 201 rows and 201 columns. The rows of this checkerboard are numbered from −100 to 100 from bottom to top. The columns of this checkerboard are numbered from −100 to 100 from left to right. The notation (r,c) denotes the cell located in the r-th row and the c-th column.

There is a king piece at position (0,0) and it wants to get to position (a,b) as soon as possible. In this problem our king is lame. Each second, the king makes exactly one of the following five moves.

  • Skip move. King's position remains unchanged.
  • Go up. If the current position of the king is (r,c) he goes to position (r+1,c).
  • Go down. Position changes from (r,c) to (r−1,c).
  • Go right. Position changes from (r,c) to (r,c+1).
  • Go left. Position changes from (r,c) to (r,c−1).

King is not allowed to make moves that put him outside of the board. The important consequence of the king being lame is that he is not allowed to make the same move during two consecutive seconds. For example, if the king goes right, the next second he can only skip, go up, down, or left.

What is the minimum number of seconds the lame king needs to reach position (a,b)?

Input

The first line of the input contains a single integer t (1≤t≤104) — the number of test cases. Then follow t lines containing one test case description each.

Each test case consists of two integers a and b (−100≤a,b≤100) — the position of the cell that the king wants to reach. It is guaranteed that either a≠0 or b≠0.

Output

Print t integers. The i-th of these integers should be equal to the minimum number of seconds the lame king needs to get to the position he wants to reach in the i-th test case. The king always starts at position (0,0).

Example

input

5

-4 1

4 4

0 -6

-5 -4

7 -8

output

7
8
11
9
15

Note

One of the possible solutions for the first example is: go down, go right, go down, go right, go down, go left, go down.

One of the possible solutions for the second example is to alternate "go right" and "go up" moves 4 times each.

One of the possible solutions for the third example is to alternate "go left" and "skip" moves starting with "go left". Thus, "go left" will be used 6 times, and "skip" will be used 5 times.

题面翻译

国王从(0,0)点移动到(x,y)最少需要几秒

移动规则:每秒可以向上、下、左、右、移动一步,或者保持不动,连续两秒不能往同一个方向移动

思路:

贪心直接算,先往离的远的地方走,然后两个方向交替行走,一边到达后,走一步停一步

代码实现

#include
using namespace std;
typedef long long ll;
int main(){
    int T;
    cin>>T;
    while(T--){
        int a,b;
        cin>>a>>b;
        a=abs(a),b=abs(b);
        int ans=0;
        if(abs(a-b)<=1)cout<b)swap(a,b);
            ans+=2*a+1;
            b=b-a-1;
            ans+=2*b;
            cout<

B. Vaccination

Ethan runs a vaccination station to help people combat the seasonal flu. He analyses the historical data in order to develop an optimal strategy for vaccine usage.

Consider there are n patients coming to the station on a particular day. The i-th patient comes at the moment ti. We know that each of these patients can be asked to wait for no more than w time moments. That means the i-th patient can get vaccine at moments ti,ti+1,…,ti+w.

Vaccines come in packs, each pack consists of k doses. Each patient needs exactly one dose. Packs are stored in a special fridge. After a pack is taken out of the fridge and opened, it can no longer be put back. The lifetime of the vaccine outside the fridge is d moments of time. Thus, if the pack was taken out of the fridge and opened at moment x, its doses can be used to vaccinate patients at moments x,x+1,…,x+d. At moment x+d+1 all the remaining unused doses of this pack are thrown away.

Assume that the vaccination station has enough staff to conduct an arbitrary number of operations at every moment of time. What is the minimum number of vaccine packs required to vaccinate all n patients?

Input

The first line of the input contains the number of test cases t (1≤t≤104). Then follow t descriptions of the test cases.

The first line of each test case contains four integers n, k, d and w (1≤n,k≤2⋅10^5, 0≤d,w≤10^6). They are the number of patients, the number of doses per vaccine pack, the number of moments of time the vaccine can live outside the fridge, and the number of moments of time each of the patients can wait, respectively.

The second line of each test case contains a non-decreasing sequence t1,t2,…,tn (0≤t1≤t2≤…≤tn≤106). The i-th element of this sequence is the moment when the i-th patient comes to the vaccination station.

It is guaranteed that the sum of n over all test cases won't exceed 2⋅105.

Output

Output one integer, the minimum number of vaccine packs required to vaccinate all n patients.

Example

input

5

6 3 5 3

1 2 3 10 11 18

6 4 0 0

3 3 3 3 3 4

9 10 2 2

0 1 2 3 4 5 6 7 8

3 10 3 6

10 20 30

5 5 4 4

0 2 4 6 8

output

2
3
2
3
1

Note

In the first example, the first pack can be opened at moment 1 to vaccinate patient 1. The vaccine is durable enough to be used at moments 2 and 3 for patients 2 and 3, respectively. Then the staff needs to ask patients 4 and 5 to wait for moment 13. At moment 13 the staff opens the second vaccine pack and serves patients 4 and 5. Finally, the last patient comes at moment 18 and immediately gets the last dose of the second pack while it is still fine.

In the second example, the vaccine should be used exactly at the moment it is taken out of the fridge. Moreover, all the patients want to be served at exactly the same moment they come. That means the staff needs to open two packs at moment 3 and use five doses on patients 1, 2, 3, 4, and 5. There will be three doses left ouf of these two packs but they can't be used for patient 66. When patient 6 comes at moment 4 the staff needs to open a new pack just to use only one dose out of it.

题面翻译:

有n个病人,每个人来医院的时间是ti,他们能等待的最长时间为w
每包试剂盒里有k个试剂,每盒试剂的有效期为d,到期后必须扔掉
怎么安排病人看病,拆开的试剂盒最少

注:病人可以看病的时间为[ ti , ti+w ],试剂盒可以用的时间为 [ x , x+d ] (x为拆开时间)

思路:

模拟

拿第一个病人的最终时间+试剂盒保质期 和 之后病人的开始时间对比,如果超时或治病人数超过k,则再开一包试剂

代码实现

#include
using namespace std;
typedef long long ll;
struct patient{
    int start,wait;
}p[200005];
int main(){
    int T;
    cin>>T;
    while(T--){
        int n,k,d,w;
        cin>>n>>k>>d>>w;
        for(int i=0;i

C. Pull Your Luck

While James is gone on business, Vesper takes her time and explores what the legendary Casino Royale has to offer to people who are fond of competitive programming.

Her attention was grabbed by the very new "Pull Your Luck" roulette which functions in a pretty peculiar way. The roulette's wheel consists of n sectors number from 00 to n−1. There is no ball and the winning sector is determined by a static arrow pointing to one of the sectors. Sectors' indexes go in the natural order and the wheel always spins in the direction of indexes increment. That means that sector i+1 goes right after sector i for all i from 00 to n−2, and sector 00 goes right after sector n−1.

After a bet is made, the player is allowed to pull the triggering handle herself and cause the wheel to spin. If the player's initial pull is made with the force equal to positive integer f, the wheel will spin for f seconds. During the first second it will advance f sectors, the next second it will advance f−1 sectors, then f−2 sectors, and so on until it comes to a complete stop. After the wheel comes to a complete stop, the sector which the arrow is pointing to is the winning one.

The roulette's arrow currently points at sector x. Vesper knows that she can pull the handle with any integer force from 11 to p inclusive. Note that it is not allowed to pull the handle with force 00, i. e. not pull it all. The biggest prize is awarded if the winning sector is 00. Now Vesper wonders if she can make sector 00 win by pulling the triggering handle exactly once?

Input

The first line of the input contains a single integer t (1≤t≤104) — the number of test cases. Then follow t lines containing one test description each.

Each test description consists of three integers n, x and p (3≤n≤105, 0≤x

It is guaranteed that the sum of n over all test cases doesn't exceed 2⋅105.

Output

Print t lines, the i-th line should contain the answer for the i-th test case. If it is possible to pull the handle with the integer force from 1 to p in order to make sector 0 win, print "Yes". Otherwise, print "No".

You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.

Example

input

7

5 2 1

5 2 2

10 0 100

11 7 100

3 1 1000

31 0 10

100 49 7

output

No
Yes
Yes
Yes
No
No
No

Note

In the first example, the only possible way to pull the handle is with force 11. That is not enough to make the arrow point at sector 00, at least force 22 is required to do so.

In the second example, Vesper can pull the handle with the force 22 so the wheel will spin 2+1=32+1=3 sectors ahead and the arrow will point at sector 00.

In the third example, Vesper can pull the handle with the force 44 so the wheel will spin 4+3+2+1=104+3+2+1=10 sectors and will point at sector 00 again.

In the fourth example, Vesper can pull the handle with the force 55 so the wheel will spin 5+4+3+2+1=155+4+3+2+1=15 sectors. That will make the wheel make one full turn plus 44 more sectors.

In the fifth example, whatever force Vesper chooses to pull the handle with, she can only make sectors 11 and 22 win.

思路:

暴力枚举,找出 i * (i+1)/2 mod n = n-x 的 i 即找到解

代码实现

#include
using namespace std;
typedef long long ll;
int main(){
    int T;
    cin>>T;
    while(T--){
        ll n,x,p;
        cin>>n>>x>>p;
        ll t=n-x;
        bool flag=0;
        for(int i=1;i<=p;i++){
            if(i/2>n+1)break;
            if(i%2==0){
                if((i/2)%n*(i+1)%n==t){
                    flag=1;
                    break;
                }
                if((i/2)%n*(i+1)%n+n==t){
                    flag=1;
                    break;
                }
            }
            else{
                if(i%n*((i+1)/2)%n+n==t){
                    flag=1;
                    break;
                }
                if(i%n*((i+1)/2)%n==t){
                    flag=1;
                    break;
                }
            }
        }
        if(flag)cout<<"Yes"<

D. Accommodation

Annie is an amateur photographer. She likes to take pictures of giant residential buildings at night. She just took a picture of a huge rectangular building that can be seen as a table of n×m windows. That means that the building has n floors and each floor has exactly m windows. Each window is either dark or bright, meaning there is light turned on in the room behind it.

Annies knows that each apartment in this building is either one-bedroom or two-bedroom. Each one-bedroom apartment has exactly one window representing it on the picture, and each two-bedroom apartment has exactly two consecutive windows on the same floor. Moreover, the value of m is guaranteed to be divisible by 4 and it is known that each floor has exactly m4 two-bedroom apartments and exactly m2 one-bedroom apartments. The actual layout of apartments is unknown and can be different for each floor.

Annie considers an apartment to be occupied if at least one of its windows is bright. She now wonders, what are the minimum and maximum possible number of occupied apartments if judged by the given picture?

Formally, for each of the floors, she comes up with some particular apartments layout with exactly m4 two-bedroom apartments (two consecutive windows) and m2 one-bedroom apartments (single window). She then counts the total number of apartments that have at least one bright window. What is the minimum and maximum possible number she can get?

Input

The first line of the input contains two positive integers n and m (1≤n⋅m≤5⋅105) — the number of floors in the building and the number of windows per floor, respectively. It is guaranteed that m is divisible by 44.

Then follow n lines containing m characters each. The j-th character of the i-th line is "0" if the j-th window on the i-th floor is dark, and is "1" if this window is bright.

Output

Print two integers, the minimum possible number of occupied apartments and the maximum possible number of occupied apartments, assuming each floor can have an individual layout of m4 two-bedroom and m2 one-bedroom apartments.

Examples

input

5 4
0100
1100
0110
1010
1011

output

7 10

input

1 8
01011100

output

3 4

题面翻译

这里有一个矩形建筑,可以看作是一张 n×m 的网格。 这意味着该建筑物有 n 层 并且每层正好有 m 个窗户。

每扇窗户要么是黑暗的要么是明亮的,这意味着它后面的房间里有灯打开。

每间公寓要么是一居室,要么是两居室。 每间一居室公寓,恰好有一个窗户代表它,而每间两居室公寓在同一层楼上恰好有两个连续的窗户。 此外,m 保证能被 4 整除,并且已知每层楼正好有 m/4 两居室公寓,正好 m/2 一居室公寓

公寓的实际布局未知,每层楼可能有所不同。

如果至少有一个窗户是明亮的,就认为公寓有人住。 如据给定的图片判断,最小和最大可能的入住公寓数量是多少?

给定一个 n*m 的01矩阵,1表示亮灯,0表示没有亮灯。

思路

比赛的时候没求出来最大数量,下来啃了好久

贪心,把每行读进去,计算这行的最大数量和最小数量,然后加起来即可,其实就是往里放两居室的过程,两居室放对了就求出来了

最少数量求法:总居室数最少,我们尽量让所有的"11"都放两居室,每遇到一个“11”,db++
因为放进去的两居室亮了两盏灯,所以房间总数为(亮灯数 - 放入的两居室数)

最多数量求法:总居室最多,尽量让两居室不亮灯或亮一盏灯,把两居室放入“01”,“00”,“10”中
亮两盏灯的两居室数 = 总两居室数 - 放入的两居室数
房间总数 = 亮灯数 - 亮两盏灯的两居室数

代码实现

#include
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int ans1=0,ans2=0;  //ans1最少,ans2最多
    while(n--){
        string s;
        cin>>s;
        int db=0,cnt=0;     //db为放进去的两居室个数,cnt为开着的灯数
        for(int i=0;i

E. Routing

鸟题是 状态压缩dp求哈密顿距离,还有个优化,学完状压dp再补

你可能感兴趣的:(算法)