Codeforces Round #783(Div.2)

A:Direction Change

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a grid with nn rows and mm columns. Rows and columns are numbered from 11 to nn, and from 11 to mm. The intersection of the aa-th row and bb-th column is denoted by (a,b)(a,b).

Initially, you are standing in the top left corner (1,1)(1,1). Your goal is to reach the bottom right corner (n,m)(n,m).

You can move in four directions from (a,b)(a,b): up to (a−1,b)(a−1,b), down to (a+1,b)(a+1,b), left to (a,b−1)(a,b−1) or right to (a,b+1)(a,b+1).

You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach (n,m)(n,m)?

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1031≤t≤103) — the number of the test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤1091≤n,m≤109) — the size of the grid.

Output

For each test case, print a single integer: −1−1 if it is impossible to reach (n,m)(n,m) under the given conditions, otherwise the minimum number of moves.

Example

input

Copy

6
1 1
2 1
1 3
4 2
4 6
10 5

output

Copy

0
1
-1
6
10
17

Note

Test case 11: n=1n=1, m=1m=1, and initially you are standing in (1,1)(1,1) so 00 move is required to reach (n,m)=(1,1)(n,m)=(1,1).

Test case 22: you should go down to reach (2,1)(2,1).

Test case 33: it is impossible to reach (1,3)(1,3) without moving right two consecutive times, or without leaving the grid.

Test case 44: an optimal moving sequence could be: (1,1)→(1,2)→(2,2)→(2,1)→(3,1)→(3,2)→(4,2)(1,1)→(1,2)→(2,2)→(2,1)→(3,1)→(3,2)→(4,2). It can be proved that this is the optimal solution. So the answer is 66.

题意:初始(1,1)移到(n,m)上下左右移动且前后2次不能同时向一个方向移动 

思路 n==1||m==1 特判断 

其他先走对角线 后判断差值奇偶分类

#include <iostream>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define int long long
using namespace std;

signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int a,b; 
        cin>>a>>b; 
        if(a==1)
        {
            if(b==1) cout<<0;
            else if(b==2) cout<<1;
            else cout<<-1;
            cout<<'\n';
            continue;//一条线特殊判断
        }
        if(b==1) 
        {
            if(a==2) cout<<1;
            else cout<<-1;
            cout<<'\n';
            continue; //同理
        }
        int step=0;
        if(a<b) swap(a,b);
        step=(b-1)*2;
        int x=a-b;
        if(x%2==1) step+=(a-b-1)/2*4+1;
        else step+=(a-b)*2;
        cout<<step<<'\n';
    }
    return 0;
}
/*先走对角线 */

B: Social Distance

time limit per test

1.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

mm chairs are arranged in a circle sequentially. The chairs are numbered from 00 to m−1m−1. nn people want to sit in these chairs. The ii-th of them wants at least a[i]a[i] empty chairs both on his right and left side.

More formally, if the ii-th person sits in the jj-th chair, then no one else should sit in the following chairs: (j−a[i])modm(j−a[i])modm, (j−a[i]+1)modm(j−a[i]+1)modm, ... (j+a[i]−1)modm(j+a[i]−1)modm, (j+a[i])modm(j+a[i])modm.

Decide if it is possible to sit down for all of them, under the given limitations.

Input

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤5⋅1041≤t≤5⋅104) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers nn and mm (2≤n≤1052≤n≤105, 1≤m≤1091≤m≤109) — the number of people and the number of chairs.

The next line contains nn integers, a1a1, a2a2, ... anan (1≤ai≤1091≤ai≤109) — the minimum number of empty chairs, on both sides of the ii-th person.

It is guaranteed that the sum of nn over all test cases will not exceed 105105.

Output

For each test case print "YES" (without quotes) if it is possible for everyone to sit down and fulfil the restrictions, and "NO" (without quotes) otherwise.

You may print every letter in any case you want (so, for example, the strings "yEs", "yes", "Yes" and "YES" will all be recognized as positive answers).

Example

input

Copy

6
3 2
1 1 1
2 4
1 1
2 5
2 1
3 8
1 2 1
4 12
1 2 1 3
4 19
1 2 1 3

output

Copy

NO
YES
NO
YES
NO
YES

Note

Test case 11: n>mn>m, so they can not sit down.

Test case 22: the first person can sit 22-nd and the second person can sit in the 00-th chair. Both of them want at least 11 empty chair on both sides, chairs 11 and 33 are free, so this is a good solution.

Test case 33: if the second person sits down somewhere, he needs 22 empty chairs, both on his right and on his left side, so it is impossible to find a place for the first person, because there are only 55 chairs.

Test case 44: they can sit in the 11-st, 44-th, 77-th chairs respectively.

题意:m把椅子n个人 第i个人入座条件是左右至少空a[i]人 

#include <iostream>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define int long long
using namespace std;
const int N=2e5+10;
int a[N];
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=1;i<=n;i++) cin>>a[i];
        sort(a+1,a+1+n,greater<long long>());
        int p=2*a[1];
        for(int i=2;i<=n-1;i++) 
        {
            p+=a[i]+1;
        }
        p++;
        if(p<=m-1) cout<<"Yes\n";
        else cout<<"No\n";
    }
    return 0;
}
//排序从大到小入座

C.Make it Increasing

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn positive integers, and an array bb, with length nn. Initially bi=0bi=0 for each 1≤i≤n1≤i≤n.

In one move you can choose an integer ii (1≤i≤n1≤i≤n), and add aiai to bibi or subtract aiai from bibi. What is the minimum number of moves needed to make bb increasing (that is, every element is strictly greater than every element before it)?

Input

The first line contains a single integer nn (2≤n≤50002≤n≤5000).

The second line contains nn integers, a1a1, a2a2, ..., anan (1≤ai≤1091≤ai≤109) — the elements of the array aa.

Output

Print a single integer, the minimum number of moves to make bb increasing.

Examples

input

Copy

5
1 2 3 4 5

output

Copy

4

input

Copy

7
1 2 1 2 1 2 1

output

Copy

10

input

Copy

8
1 8 2 7 3 6 4 5

output

Copy

16

Note

Example 11: you can subtract a1a1 from b1b1, and add a3a3, a4a4, and a5a5 to b3b3, b4b4, and b5b5 respectively. The final array will be [−1−1, 00, 33, 44, 55] after 44 moves.

Example 22: you can reach [−3−3, −2−2, −1−1, 00, 11, 22, 33] in 1010 moves.

题意 b为0序列 对bi 进行任意次数+- a[i],求使得其严格单增的最小操作数

思路:枚举0的位置 

#include <iostream>
#include <cstring>
#include <string>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <stack>
#include <map>
#include <vector>
#include <queue>
#include <set>
#define int long long
using namespace std;
const int N=5e3+10;
const int INF=0x3f3f3f3f3f3f3f3f;
int a[N];
signed main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    int ans=INF;
    for(int i=1;i<=n;i++)
    {
        int sum=0;
        int ma=0;
        for(int j=i+1;j<=n;j++) 
        {
            if((ma+1)%a[j]==0) 
            {
                sum+=(ma+1)/a[j];
                ma++;
            }
            else 
            {
                sum+=(ma+1)/a[j]+1;
                ma=((ma+1)/a[j]+1)*a[j];
            }
        } 
        ma=0;
        for(int j=i-1;j>=1;j--)
        {
            if((ma+1)%a[j]==0) 
            {
                sum+=(ma+1)/a[j];
                ma++;
            }
            else 
            {
                sum+=(ma+1)/a[j]+1;
                ma=((ma+1)/a[j]+1)*a[j];
            }
        }
        ans=min(sum,ans);
    }
    cout<<ans;
    return 0;
}
//枚举0的位置

只做出3题之后慢慢补吧。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值