哈理工訓練賽3.10(cf gym 101911)

H - Theater Square

 http://codeforces.com/gym/101911/problem/H

The Theater Square can be represented as a rectangle having height nn and length mm, divided into square 1×11×1 cells. Let's denote the cell located at the intersection of ii-th row and jj-th column as (i,j)(i,j). The rows are numbered from top to bottom, the columns — from left to right.

There is a rectangular fountain inside the Teather Square. The cell in its left upper corner is (x1,y1)(x1,y1), the cell in its right lower corner is (x2,y2)(x2,y2).

The Theater Square soon will be paved with tiles having height 1 and length 2. Every cell (except cells inside the fountain) should be paved, and no cell should be covered by more than one tile. All tiles will be laid out horizontally, so the cells covered by each tile are in the same row. To pave the whole Theater Square it might be necessary to break some tiles. After breaking a tile, two new tiles of size 1×1 are formed (which cannot be broken further). You may consider that the mayor, who ordered the paving of the Theater Square, has infinite number of tiles 1×2.

Since broken tiles are not beautiful, among all possible ways to pave the Theater Square the mayor wants to choose a way such that the number of tiles to be broken into two lesser tiles is minimum possible. Pay attention that tiles should be laid horizontally, no tile can cover cells in different rows.

Help the mayor! Tell him the minimum possible number of tiles to be broken.

Input

The first line contains two integers nn and mm (1≤n,m≤2⋅105)(1≤n,m≤2⋅105) — the height and the length of the Theater Square, respectively.

The second line contains four numbers x1,y1,x2,y2 (1≤x1≤x2≤n,1≤y1≤y2≤m)— the coordinates of left upper corner and right lower corner of the fountain.

Output

Print one number — minimum possible number of tiles mayor has to break in order to pave the whole Theater Square.

Examples

Input

6 5
1 2 3 4

Output

5

Input

6 1
3 1 4 1

Output

2

Input

1 12
1 3 1 8

Output

0

Note

One of the optimal ways to pave the Theater Square in the first example:

55 tiles are to be broken.

題目:

用1x2的瓷磚鋪地(只能橫着放),中間有左上角座標(x1,y1)右下角座標(x2,y2)的噴泉,問至少分開1X2的瓷磚成1X1的瓷磚

分析:

1X1的瓷磚只可能出現在左列或者右列上(包括兩列都有),當噴泉左邊界距離廣場邊界爲奇數時,噴泉左邊肯定有一列是1x1,個數就是噴泉的長,同理噴泉右邊界距離廣場邊界爲奇數時,噴泉右邊邊肯定有一列是1x1,個數就是噴泉的長,另外當廣場寬是奇數列時,我們可以看做從左邊鋪到右邊,把1x1的放在了最右邊,那麼就是廣場的長n,注意如果噴泉右邊界距離廣場邊界爲奇數並且廣場是寬是奇數時,就多加了噴泉的長度,需要減出來。


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m;
    cin>>n>>m;
    int x1,y1,x2,y2;
    cin>>x1>>y1>>x2>>y2;
    int sum=0;
    if((y1-1)&1)//噴泉左邊
        sum+=(x2-x1+1);
    if((m-y2)&1)//噴泉右邊
        sum+=(x2-x1+1);
    if(m&1)//右邊一列都是1x1
        sum+=n-(x2-x1+1);//n-噴泉右邊已經計算的
    sum=sum&1?sum/2+1:sum/2;//兩塊1X1,組成一塊1x2
    cout<<max(0,sum)<<endl;
}

 

 

I - Heist

 http://codeforces.com/gym/101911/problem/I

There was an electronic store heist last night.

All keyboards which were in the store yesterday were numbered in ascending order from some integer number xx. For example, if x=4x=4 and there were 33 keyboards in the store, then the devices had indices 44, 55 and 66, and if x=10x=10and there were 77 of them then the keyboards had indices 1010, 1111, 1212, 1313, 1414, 1515 and 1616.

After the heist, only nn keyboards remain, and they have indices a1,a2,…,ana1,a2,…,an. Calculate the minimum possible number of keyboards that have been stolen. The staff remember neither xx nor the number of keyboards in the store before the heist.

Input

The first line contains single integer nn (1≤n≤1000)(1≤n≤1000) — the number of keyboards in the store that remained after the heist.

The second line contains nn distinct integers a1,a2,…,ana1,a2,…,an (1≤ai≤109)(1≤ai≤109) — the indices of the remaining keyboards. The integers aiai are given in arbitrary order and are pairwise distinct.

Output

Print the minimum possible number of keyboards that have been stolen if the staff remember neither xx nor the number of keyboards in the store before the heist.

Examples

Input

4
10 13 12 8

Output

2

Input

5
7 5 6 4 8

Output

0

題意:

最小的到最大的數,中間缺幾個數

#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<cstdlib>
using namespace std;
#define ios   ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);'
#define ll long long
int n;
int main()
{
    while(~scanf("%d",&n)){
        int minx,maxn;
        minx=0x3f3f3f3f;
        maxn=-1;
        int m;
        for(int i=1;i<=n;i++){
            scanf("%d",&m);
            if(m>maxn)
                maxn=m;
            if(m<minx)
                minx=m;
        }
        printf("%d\n",maxn-minx+1-n);
    }
}

 

J - Buying a TV Set

 http://codeforces.com/gym/101911/problem/J

Monocarp has decided to buy a new TV set and hang it on the wall in his flat. The wall has enough free space so Monocarp can buy a TV set with screen width not greater than aa and screen height not greater than bb. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is ww, and the height of the screen is hh, then the following condition should be met: wh=xywh=xy.

There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers ww and hh there is a TV set with screen width ww and height hh in the shop.

Monocarp isn't ready to choose the exact TV set he is going to buy. Firstly he wants to determine the optimal screen resolution. He has decided to try all possible variants of screen size. But he must count the number of pairs of positive integers ww and hh, beforehand, such that (w≤a)(w≤a), (h≤b)(h≤b) and (wh=xy)(wh=xy).

In other words, Monocarp wants to determine the number of TV sets having aspect ratio xyxy, screen width not exceeding aa, and screen height not exceeding bb. Two TV sets are considered different if they have different screen width or different screen height.

Input

The first line contains four integers a, b, x, y (1≤a,b,x,y≤10181≤a,b,x,y≤1018) — the constraints on the screen width and height, and on the aspect ratio.

Output

Print one integer — the number of different variants to choose TV screen width and screen height so that they meet the aforementioned constraints.

Examples

Input

17 15 5 3

Output

3

Input

14 16 7 22

Output

0

Input

4 2 6 4

Output

1

Input

1000000000000000000 1000000000000000000 999999866000004473 999999822000007597

Output

1000000063

Note

In the first example, there are 33 possible variants: (5,3)(5,3), (10,6)(10,6), (15,9)(15,9).

In the second example, there is no TV set meeting the constraints.

In the third example, there is only one variant: (3,2)(3,2).

題意:

在xx∈[0,a],yy∈[0,b]的範圍內,有多少個xx/yy等於x/y

分析:

要想讓xx/yy等於x/y,那xx,yy肯定分別是x,y的k倍(k>0),xx,yy可能比x,y小也就是k可能是分數,那我們爲了便於計算,讓x,y同時除以他們的最大公約數,此時,便是最小的了,那麼k>=1。要想算一個範圍內有多少數是一個數的倍數,那麼範圍除以這個數就可以,所以兩個範圍分別處以這兩個數取小的那一個,就是可以取到的這兩個的倍數的最大數了

#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<cstdlib>
using namespace std;
#define ios  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
const int maxn=1000005;
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
int main()
{
    ios;
    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    ll gg=gcd(c,d);
    c/=gg,d/=gg;
    cout<<min((a/c),(b/d))<<endl;
}

 

K - Medians and Partition

 http://codeforces.com/gym/101911/problem/K

Let median of some array be the number which would stand in the middle of this array if it was sorted beforehand. If the array has even length let median be smallest of of two middle elements. For example, median of the array [10,3,2,3,2][10,3,2,3,2] is 33 (i.e. [2,2,3–,3,10][2,2,3_,3,10]). Median of the array [1,5,8,1][1,5,8,1] is 11 (i.e. [1,1–,5,8][1,1_,5,8]).

Let array be mm-good if its median is greater or equal than mm.

Let the partition of array [a1,a2,…,an][a1,a2,…,an] be a set of subarrays {b1,b2,…,bk}{b1,b2,…,bk} such that b1=[a1,a2,…,ai1]b1=[a1,a2,…,ai1], b2=[ai1+1,ai1+2,…,ai2]b2=[ai1+1,ai1+2,…,ai2], ..., bk=[aik−1+1,aik−1+2,…,an]bk=[aik−1+1,aik−1+2,…,an]. For example, array [10,3,2,3,2][10,3,2,3,2] can be partitioned as follows: {[10,3,2,3,2]}{[10,3,2,3,2]} or {[10],[3],[2],[3],[2]}{[10],[3],[2],[3],[2]}, or {[10],[3,2,3,2]}{[10],[3,2,3,2]}, or {[10,3],[2],[3,2]}{[10,3],[2],[3,2]} and so on.

You are given array aa of length nn and integer mm. Find the partition of aa into maximum number of subarrays such that each subarray is mm-good.

Input

The first line contains two integers nn and mm (1≤n≤50001≤n≤5000, 1≤m≤50001≤m≤5000) — length of array aa and constant mm.

The second line contains nn integers a1a1, a2a2, ..., anan (1≤ai≤50001≤ai≤5000)— array aa.

Output

If there is no valid partition of array aa into mm-good subarrays, print 00. Otherwise print maximum number of subarrays in partition of array aa such that each subarray is mm-good.

Examples

Input

5 2
10 3 2 3 2

Output

5

Input

5 3
10 3 2 3 2

Output

1

Input

5 4
10 3 2 3 2

Output

0

Note

In the first example array can be partitioned into 55 subarrays: {[10],[3],[2],[3],[2]}{[10],[3],[2],[3],[2]}. Medians of each part greater of equal than 22.

In the second example we can't partition array into several subarrays since medians of [2][2], [3,2][3,2], [2,3,2][2,3,2] and [3,2,3,2][3,2,3,2] are less than 33.

題目:

把一個數列分成幾段,使每一段的中位數都大於等於m,問最多能分成幾段()

分段例子:{[10,3,2,3,2]}可以這樣分段{[10,3,2,3,2]} or {[10],[3],[2],[3],[2]}{[10],[3],[2],[3],[2]}, or {[10],[3,2,3,2]}{[10],[3,2,3,2]}, or {[10,3],[2],[3,2]}{[10,3],[2],[3,2]

分析:

對於一個小於m的值至少需要兩個大於等於m的值的代價來維護中位數不小於m,所以可以組成n-num(小於m的個數)*2個,當然最少是0個

#include<bits/stdc++.h>
using namespace std;
int n,m;
int main()
{
    cin>>n>>m;
    int x,num=0;
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(x<m)
            num++;
    }
    cout<<max(0,n-2*num)<<endl;
}

 

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