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);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章