哈理工训练赛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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值