QLU-第七次训练

A   coins

You have unlimited number of coins with values 1,2,…,n1,2,…,n. You want to select some set of coins having the total value of SS.

It is allowed to have multiple coins with the same value in the set. What is the minimum number of coins required to get sum SS?

Input

The only line of the input contains two integers nn and SS (1≤n≤1000001≤n≤100000, 1≤S≤1091≤S≤109)

Output

Print exactly one integer — the minimum number of coins required to obtain sum SS.

Examples

Input

5 11

Output

3

Input

6 16

Output

3

Note

In the first example, some of the possible ways to get sum 1111 with 33 coins are:

  • (3,4,4)(3,4,4)
  • (2,4,5)(2,4,5)
  • (1,5,5)(1,5,5)
  • (3,3,5)(3,3,5)

It is impossible to get sum 1111 with less than 33 coins.

In the second example, some of the possible ways to get sum 1616 with 33 coins are:

  • (5,5,6)(5,5,6)
  • (4,6,6)(4,6,6)

It is impossible to get sum 1616 with less than 33 coins.

大水题,没什么好说的,直接做就完了,也没坑

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
int n,s;
int main()
{
    while(~scanf("%d%d",&n,&s)){
        int ans=s%n==0?s/n:(s/n)+1;
        printf("%d\n",ans);
    }
}

Views Matter 

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You came to the exhibition and one exhibit has drawn your attention. It consists of nn stacks of blocks, where the ii-th stack consists of aiaiblocks resting on the surface.

The height of the exhibit is equal to mm. Consequently, the number of blocks in each stack is less than or equal to mm.

There is a camera on the ceiling that sees the top view of the blocks and a camera on the right wall that sees the side view of the blocks.

Find the maximum number of blocks you can remove such that the views for both the cameras would not change.

Note, that while originally all blocks are stacked on the floor, it is not required for them to stay connected to the floor after some blocks are removed. There is no gravity in the whole exhibition, so no block would fall down, even if the block underneath is removed. It is not allowed to move blocks by hand either.

Input

The first line contains two integers nn and mm (1≤n≤1000001≤n≤100000, 1≤m≤1091≤m≤109) — the number of stacks and the height of the exhibit.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤m1≤ai≤m) — the number of blocks in each stack from left to right.

Output

Print exactly one integer — the maximum number of blocks that can be removed.

Examples

input

Copy

5 6
3 3 3 3 3

output

Copy

10

input

Copy

3 5
1 2 4

output

Copy

3

input

Copy

5 5
2 3 1 4 4

output

Copy

9

input

Copy

1 1000
548

output

Copy

0

input

Copy

3 3
3 1 1

output

Copy

1

Note

The following pictures illustrate the first example and its possible solution.

Blue cells indicate removed blocks. There are 1010 blue cells, so the answer is 1010.

这个一上来读错题了。。。一直在求需要保留多少个,然后样例一直不过。。。。。。英语不大好

这个题问最多可以移除多少个,那么我们可以先求需要保留多少个,然后总数减去需要保留的就是答案

那么现在问题就是要求需要保留多少个,我们可以发现每一列都是可以和其他列交换的,也就是列的顺序无所谓,那么可以想到给他按列的数量排个序,从排完的第一列开始与下一列比较,如果比下一列多,那就多的需要保留,在更新一下当前的高度,如果和下一列一样多或者比下一列少(像5个3,  3 3 3 3 3,当到第二列时高度为2,这时比下一列3小),那么当前这一列最上面的保留,高度更新为当前高度-1.如果高度为1了,那么后面的都需要保留。

最后再用总数减去求出来的需要保留的即为答案。 

 还有要注意的是这个题需要开long long

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e5+10;
ll a[N],ans,n,m;
bool cmp(ll a,ll b)
{
    return a>b;
}
int main()
{
    while(~scanf("%lld%lld",&n,&m)){
            met(a,0);
            ans=0;
            ll num=0;
        for(int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            num+=a[i];
        }
        sort(a+1,a+1+n,cmp);     //排序
        int h=a[1];
        for(int i=1;i<=n;i++){
                if(h==1){        //高度为1,后面的包括这一个都保留
                    ans+=n-i+1;
                    break;
                }
            if(h>a[i+1]){       //比下一列多
                ans+=h-a[i+1];
                h=a[i+1];
            }
           else if(h<=a[i+1]){  //小于等于下一列
                ans++;
                h--;
            }
            if(h==1){     //高度为1 ,因为这时当前一列已经把需要保留的加上了,所以这时候再求需要保留的不需要加当前一列
                ans+=n-i;
                break;
            }
        }
        printf("%lld\n",num-ans);
    }
}

C Vasya and Book

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya is reading a e-book. The file of the book consists of nn pages, numbered from 11 to nn. The screen is currently displaying the contents of page xx, and Vasya wants to read the page yy. There are two buttons on the book which allow Vasya to scroll dd pages forwards or backwards (but he cannot scroll outside the book). For example, if the book consists of 1010 pages, and d=3d=3, then from the first page Vasya can scroll to the first or to the fourth page by pressing one of the buttons; from the second page — to the first or to the fifth; from the sixth page — to the third or to the ninth; from the eighth — to the fifth or to the tenth.

Help Vasya to calculate the minimum number of times he needs to press a button to move to page yy.

Input

The first line contains one integer tt (1≤t≤1031≤t≤103) — the number of testcases.

Each testcase is denoted by a line containing four integers nn, xx, yy, dd (1≤n,d≤1091≤n,d≤109, 1≤x,y≤n1≤x,y≤n) — the number of pages, the starting page, the desired page, and the number of pages scrolled by pressing one button, respectively.

Output

Print one line for each test.

If Vasya can move from page xx to page yy, print the minimum number of times he needs to press a button to do it. Otherwise print −1−1.

Example

input

Copy

3
10 4 5 2
5 1 3 4
20 4 19 3

output

Copy

4
-1
5

Note

In the first test case the optimal sequence is: 4→2→1→3→54→2→1→3→5.

In the second test case it is possible to get to pages 11 and 55.

In the third test case the optimal sequence is: 4→7→10→13→16→194→7→10→13→16→19.

 

大水题,只要判断一下能否直接到达,或者先到1再到y目的地,或者先到n再到y目的地,比较一下大小,如果都不行输出-1

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
int n,x,y,d;
int main()
{
    int t;
    sc(t);
    while(t--){
        scanf("%d%d%d%d",&n,&x,&y,&d);
        double ans1=0,ans2=0;
        int ans=0;
        if((abs(y-x))%d==0){
            printf("%d\n",(abs(y-x))/d);
            continue;
        }
        if ((y-1)%d==0)
            ans1=((x-1)%d==0?(x-1)/d:(x-1)/d+1)+(y-1)/d;
        if((n-y)%d==0)
            ans2=((n-x)%d==0?(n-x)/d:(n-x)/d+1)+(n-y)/d;
        if(ans1!=0&&ans2!=0)
            ans=min((int)ans1,(int)ans2);
        else if(ans1!=0)
            ans=(int)ans1;
        else if(ans2!=0)
            ans=(int)ans2;
        else ans=-1;
        printf("%d\n",ans);
    }
}

D    Vova and Trophies(前缀+后缀)

 

Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.

The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.

Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.

Input

The first line contains one integer nn (2≤n≤1052≤n≤105) — the number of trophies.

The second line contains nn characters, each of them is either G or S. If the ii-th character is G, then the ii-th trophy is a golden one, otherwise it's a silver trophy.

Output

Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

Examples

Input

10
GGGSGGGSGG

Output

7

Input

4
GGGG

Output

4

Input

3
SSS

Output

0

Note

In the first example Vova has to swap trophies with indices 44 and 1010. Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 77.

In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 44.

In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 0.

 

一开始做的时候想的是记录下银牌的位置,然后比一下每三块银牌之间的金牌的个数,举个例子,就相当于第一块银牌和第三块银牌之间有多少个牌子,然后再看除了第一块和第三块之间的金牌还有没有其他的金牌,决定是否算上第二个银牌,但是无情的WA了两发后,我发现这样无法判断最后几块是金牌的情况。。。

卡了一会后,突然发现可以用两个数组存,因为之前的方法类似于前缀的思想,那再加上一个后缀的数组不就完了。。。然后在开两个用来记数的数组

#include<bits/stdc++.h>
#define exp 1e-8
#define mian main
#define pii pair<int,int>
#define pll pair<ll,ll>
#define ll long long
#define pb push_back
#define PI  acos(-1.0)
#define inf 0x3f3f3f3f
#define w(x) while(x--)
#define int_max 2147483647
#define lowbit(x) (x)&(-x)
#define gcd(a,b) __gcd(a,b)
#define pq(x)  priority_queue<x>
#define ull unsigned long long
#define sc(x) scanf("%d",&x)
#define scl(x) scanf("%lld",&x)
#define pl(a,n) next_permutation(a,a+n)
#define ios ios::sync_with_stdio(false)
#define met(a,x) memset((a),(x),sizeof((a)))
using namespace std;
const int N=1e5+10;
int pre[N],prenum[N],last[N],lastnum[N],n;
char s[N];
void init()
{
    met(pre,0);
    met(prenum,0);         //记这个数前一共有多少个金牌(包括这个)
    met(last,0);
    met(lastnum,0);           //记这个数后一共有多少个金牌(包括这个)
}
int main()
{
    while(~sc(n)){
            init();
          scanf("%s",s+1);
     for(int i=1;i<=n;i++){       //求前缀
        prenum[i]=prenum[i-1];
        if(s[i]=='G'){
            pre[i]=pre[i-1]+1;
            prenum[i]++;
        }
        else
            pre[i]=0;
     }
     for(int i=n;i>=1;i--){         //求后缀的情况
        lastnum[i]=lastnum[i+1];
        if(s[i]=='G'){
            last[i]=last[i+1]+1;
            lastnum[i]++;
        }
        else
            last[i]=0;
     }
     int ans=0;
     for(int i=1;i<=n;i++){
        if(s[i]=='G')
            ans=max(ans,pre[i-1]+last[i+1]+1);
        else{
            if(!(prenum[i-1]==pre[i-1]&&lastnum[i+1]==last[i+1]))      //除了这个数的连续前缀和连续后缀外,还有其他的金牌
                ans=max(ans,pre[i-1]+last[i+1]+1);
            else
                ans=max(ans,pre[i-1]+last[i+1]);
        }
     }
     printf("%d\n",ans);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值