2018-2019 ACM-ICPC, NEERC, Southern Subregional Contest, Qualification Stage

前排队友博客祭天@墨墨墨小白
2

C - Bacteria Gym - 101911C

Recently Monocarp has created his own mini-laboratory!
The laboratory contains n bacteria. Monocarp knows that he can merge any two bacteria having equal sizes, and the resulting bacterium will have the size equal to the sum of sizes of merged bacteria. For example, if two bacteria having sizes equal to 7 merge, one bacterium with size 14 is the result.
It becomes hard to watch for many bacteria, so Monocarp wants to merge all of them into one bacterium. It may not be possible to do this with the bacteria Monocarp has, so he can buy any number of bacteria of any possible integer sizes in a special store.
You have to determine the minimum number of bacteria Monocarp has to buy to merge them with the n bacteria his laboratory contains into exactly one bacterium.

Input

The first line contains one integer n (1≤n≤2⋅105) — the number of bacteria Monocarp’s laboratory contains.
The second line contains n integers a1,a2,…,an (1≤ai≤109), where ai is the size of the i-th bacterium in the laboratory.

Output

If it is impossible to merge the bacteria (possibly after buying some) into only one bacterium, print -1.
Otherwise print the minimum number of bacteria Monocarp has to buy to merge them with the n bacteria his laboratory contains into exactly one bacterium.

Examples

Input
2
1 4
Output
2
Input
3
3 6 9
Output
-1
Input
7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
Output
1

Note

In the first example Monocarp should buy one bacterium having size 1 and one bacterium having size 2. Then Monocarp will have 4 bacteria having sizes [1,4,1,2]. Then two bacteria having sizes 1 can be merged into one having size 2. Then Monocarp will have 3 bacteria having sizes [2,4,2]. Then two bacteria having sizes 2 can be merged into one having size 4. Then Monocarp will have 2 bacteria having sizes [4,4], which can be merged into one having size 8.
In the second example no matter which bacteria Monocarp will buy, he cannot merge all his bacteria.
In the third example Monocarp needs to buy one bacterium having size 1000000000.

解析

类似于合并果子,将两堆相同大小的果子合并,问最后能不能合并成一堆.然后缺什么可以买什么,问能不能合并成一堆,可以输出需要最少另购多少,不可以输出-1.
优先队列,将能合并的合并.如果另一堆b不是a堆的偶数被,那么跳出循环.

#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<long long>q;

long long read()
{
	long long f=1,x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
    return x;
}
int main()
{
	int n,ans=0;
	n=read();
	for(int i=0;i<n;i++)
		long long  a=read(),q.push(-a);
	while(q.size()!=1){
	    long long a=q.top();q.pop();
	    long long b=q.top();q.pop();
	    if(a==b)
			q.push(a+b);
		else if((b/a)%2){
			cout<<-1<<endl;
			return 0;
	    }
	    else
			q.push(2*a),q.push(b),ans++;
	}
	cout<<ans;
}

F - Tickets Gym - 101911F

Last n days Monocarp used public transport to get to the university. He received a ticket with number ti during the i-th day.

Tickets’ numbers are six digit non-negative integers with possible leading zeroes. For example, 123456, 000000, 099999, 999999 are correct ticket numbers, but 1234567, 12345, 9 are not. Every day tickets are numbered from zero. The first passenger gets ticket number 000000, the second one — ticket number 000001, the third one — number 000002 and so on every day. Assume that each day the number of passengers doesn’t exceed 106.

Unluckiness of the ticket is equal to absolute difference between the sum of the first three digits and the sum of the last three. For example, unluckiness of the ticket number 345123 is equal to |(3+4+5)−(1+2+3)|=6, or unluckiness of the ticket number 238526 is equal to |(2+3+8)−(5+2+6)|=0.

One passenger is luckier than other if unluckiness of first passenger’s ticket is strictly less than unluckiness of the second one’s ticket.

For each of n days for given Monocarp’s ticket’s number ti calculate the number of passengers who received their tickets before him during this day and are luckier than Monocarp.

Examine examples for the further understanding of the statement.

Input

The first line contains one integer n (1≤n≤2⋅105) — the number of days during which Monocarp used public transport.

Each of the next n lines contains one six digit integer ti (0≤ti<106, ti can have leading zeroes) — the ticket Monocarp received during the corresponding day.

Output

Print n lines: one integer per line — the number of passengers who received their tickets before Monocarp during the corresponding day and are luckier than Monocarp.

Example

Input
5
001000
000000
999000
453234
654331
Output
1
0
998999
121496
470362

Note

During the first day the only one passenger who got ticket before Monocarp was luckier. This passenger got ticket number 000000.

During the second day Monocarp was the first one, so there was nobody before him.

During the third day all passengers except one who got tickets before Monocarp were more luckier than him. The one whose unluckiness was equal to Monocarp’s unluckiness got ticket number 000999.

解析

有个值叫unluckness,是一个票的号码的前三位和-后三位和的绝对值.a比b幸运,当且仅当a的票号>b的票号.给你一个票号,问你在这个票号前幸运的有多少.

#include<iostream>
#include<algorithm>
#include<sstream>
#include<string.h>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef long long ll;
const int MAX = 1e6;
int a[MAX],f[30];
ll read()
{
	ll f = 1, x = 0; char s = getchar();
	while (s<'0' || s>'9') { if (s == '-')f = -1; s = getchar(); }
	while (s >= '0'&&s <= '9') { x = x * 10 + s - '0'; s = getchar(); }
	x *= f;
	return x;
}
int main()
{
	int n = read();
	for (int i = 0; i <= 999999; i++) {
		int x = 0, y = 0, tmp;
		x += i / 100000;
		x += (i / 10000) % 10;
		x += (i / 1000) % 10;
		y += (i / 100) % 10;
		y += (i / 10) % 10;
		y += i % 10;
		tmp=abs(x - y);
		f[tmp]++ ;
		for (int j = 0; j < tmp; j++)
			a[i] += f[j];
	}
	while (n--) {
		int m,ans=0;
		m=read();
		printf("%d\n",a[m]);
	}
}

J - Buying a TV Set Gym - 101911J

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 a and screen height not greater than b. Monocarp is also used to TV sets with a certain aspect ratio: formally, if the width of the screen is w, and the height of the screen is h, then the following condition should be met: wh=xy.

There are many different TV sets in the shop. Monocarp is sure that for any pair of positive integers w and h there is a TV set with screen width w and height h 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 w and h, beforehand, such that (w≤a), (h≤b) and (wh=xy).

In other words, Monocarp wants to determine the number of TV sets having aspect ratio xy, screen width not exceeding a, and screen height not exceeding b. 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≤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 3 possible variants: (5,3), (10,6), (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).

解析

水题,数学题

#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;

ll read()
{
    ll f=1,x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
    return x;
}
int main()
{
  ll a=read(),b=read(),x=read(),y=read(),minn;
  ll r=__gcd(x,y);
  x/=r,y/=r;
  minn=min(a/x,b/y);
  cout<<minn;
}

H - Theater Square Gym - 101911H

The Theater Square can be represented as a rectangle having height n and length m, divided into square 1×1 cells. Let’s denote the cell located at the intersection of i-th row and j-th column as (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), the cell in its right lower corner is (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 n and m (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:
1
5 tiles are to be broken.

解析

分割广场进行讨论

#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;

ll read()
{
    ll f=1,x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
    return x;
}
int main()
{
  ll n=read(),m=read(),x1=read(),y1=read(),x2=read(),y2=read(),ans=0;
  bool f=0;
  if(m&1){
    ll a=n-(x2-x1+1);
    if(a&1){
      ans+=a/2+1;
      f=1;
  	}
    else
      ans+=a/2;
  }
  ll l=y1-1,r=m-y2;
  if(l&1){
  	if(f){
  		ans+=(x2-x1+1)/2;
  		f=0;
  	}
  	else if((x2-x1+1)%2){
  		ans+=(x2-x1+1)/2+1;
  		f=1;
  	}
  	else
  		ans+=(x2-x1+1)/2;
  }
  if(r&1){
  	if(f){
  		ans+=(x2-x1+1)/2;
  		f=0;
  	}
  	else if((x2-x1+1)%2){
  		ans+=(x2-x1+1)/2+1;
  		f=1;
  	}
  	else
  		ans+=(x2-x1+1)/2;
  }
  cout<<ans<<endl;
}

I - Heist Gym - 101911I

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 x. For example, if x=4 and there were 3 keyboards in the store, then the devices had indices 4, 5 and 6, and if x=10 and there were 7 of them then the keyboards had indices 10, 11, 12, 13, 14, 15 and 16.

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

Input

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

The second line contains n distinct integers a1,a2,…,an (1≤ai≤109) — the indices of the remaining keyboards. The integers ai 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 x 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

Note

In the first example, if x=8 then minimum number of stolen keyboards is equal to 2. The keyboards with indices 9 and 11 were stolen during the heist.

In the second example, if x=4 then nothing was stolen during the heist.

解析

思维签到题

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1000+10;
int a[maxn];
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin>>n;
  for(int i=0;i<n;i++)
    cin>>a[i];
  sort(a,a+n);
  int ans=0;
  for(int i=1;i<n;i++)
    ans+=a[i]-a[i-1]-1;
  cout<<ans;
}

K - Medians and Partition Gym - 101911K

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] is 3 (i.e. [2,2,3–,3,10]). Median of the array [1,5,8,1] is 1 (i.e. [1,1–,5,8]).

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

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

You are given array a of length n and integer m. Find the partition of a into maximum number of subarrays such that each subarray is m-good.

Input

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

The second line contains n integers a1, a2, …, an (1≤ai≤5000)— array a.

Output

If there is no valid partition of array a into m-good subarrays, print 0. Otherwise print maximum number of subarrays in partition of array a such that each subarray is m-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 5 subarrays: {[10],[3],[2],[3],[2]}. Medians of each part greater of equal than 2.

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

解析

数学题,简单推理一下就可以

#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<cstdio>
#ifndef NULL
#define NULL 0
#endif
using namespace std;

typedef long long ll;
int read()
{
    int f=1,x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
    return x;
}
int a[500000];

int main()
{
	int n=read(),m=read(),ans=0;
	for(int i=0;i<n;i++)
		a[i]=read();
	sort(a,a+n);
	if(a[0]>=m)
		cout<<n<<endl;
	else{
		for(int i=0;i<n;i++)
			if(a[i]>=m)
				if(2*i<n){
					ans=n-2*i;
					break;
				}
		cout<<ans<<endl;
	}
}
发布了62 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/UnKfrozen/article/details/88382853