(第二次)Educational Codeforces Round 44 (Rated for Div. 2)


A. Chess Placing
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a chessboard of size 1 × n. It is guaranteed that n is even. The chessboard is painted like this: "BWBW...BW".

Some cells of the board are occupied by the chess pieces. Each cell contains no more than one chess piece. It is known that the total number of pieces equals to .

In one step you can move one of the pieces one cell to the left or to the right. You cannot move pieces beyond the borders of the board. You also cannot move pieces to the cells that are already occupied.

Your task is to place all the pieces in the cells of the same color using the minimum number of moves (all the pieces must occupy only the black cells or only the white cells after all the moves are made).

Input

The first line of the input contains one integer n (2 ≤ n ≤ 100n is even) — the size of the chessboard.

The second line of the input contains  integer numbers  (1 ≤ pi ≤ n) — initial positions of the pieces. It is guaranteed that all the positions are distinct.

Output

Print one integer — the minimum number of moves you have to make to place all the pieces in the cells of the same color.

Examples
input
Copy
6
1 2 6
output
Copy
2
input
Copy
10
1 2 3 4 5
output
Copy
10
Note

In the first example the only possible strategy is to move the piece at the position 6 to the position 5 and move the piece at the position 2to the position 3. Notice that if you decide to place the pieces in the white cells the minimum number of moves will be 3.

In the second example the possible strategy is to move  in 4 moves, then  in 3 moves,  in 2 moves and  in 1 move.

#include<bits/stdc++.h>
using namespace std;
const int _max=105;
int a[_max],b[_max];
int main(){
    int n;
	while(cin>>n){
	int sum1=0,sum2=0;
	   memset(a,0,sizeof(a));
	   memset(b,0,sizeof(b));
	for(int i=1;i<=n/2;i++){
		cin>>b[i];
		a[b[i]]=1;
	}
	sort(b+1,b+n/2+1);
	int l=1;
	for(int i=1;i<=n/2;i++){
				sum1+=abs(b[i]-l);
				l+=2;
	}
	l=2;
	for(int i=1;i<=n/2;i++){
				sum2+=abs(b[i]-l);
				l+=2;
		}
	cout<<min(sum1,sum2)<<endl;
	}
    return 0;
}

                                                             B. Switches and Lamps
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given n switches and m lamps. The i-th switch turns on some subset of the lamps. This information is given as the matrix aconsisting of n rows and m columns where ai, j = 1 if the i-th switch turns on the j-th lamp and ai, j = 0 if the i-th switch is not connected to the j-th lamp.

Initially all m lamps are turned off.

Switches change state only from "off" to "on". It means that if you press two or more switches connected to the same lamp then the lamp will be turned on after any of this switches is pressed and will remain its state even if any switch connected to this lamp is pressed afterwards.

It is guaranteed that if you push all n switches then all m lamps will be turned on.

Your think that you have too many switches and you would like to ignore one of them.

Your task is to say if there exists such a switch that if you will ignore (not use) it but press all the other n - 1 switches then all the mlamps will be turned on.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 2000) — the number of the switches and the number of the lamps.

The following n lines contain m characters each. The character ai, j is equal to '1' if the i-th switch turns on the j-th lamp and '0' otherwise.

It is guaranteed that if you press all n switches all m lamps will be turned on.

Output

Print "YES" if there is a switch that if you will ignore it and press all the other n - 1 switches then all m lamps will be turned on. Print "NO" if there is no such switch.

Examples
input
Copy
4 5
10101
01000
00111
10000
output
Copy
YES
input
Copy
4 5
10100
01000
00110
00101
output
Copy
NO
#include<bits/stdc++.h>
using namespace std;
const int _max=2005;
int a[_max][_max],b[_max],c[_max];
int main(){
	int n,m,flag;
	while(cin>>n>>m){
		flag=0;
		char t;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		for(int i=1;i<=n;i++){
			for(int j=1;j<=m;j++){
				cin>>t;
				if(t=='1'){
					    a[i][j]=1;
						c[j]++;
						b[j]++;
				}
			}
		}
		for(int i=1;i<=n;i++){
			if(flag==1) break;
			for(int k=1;k<=m;k++)b[k]=c[k]; 
			for(int j=1;j<=m;j++){
				if(b[j]-a[i][j]==0){
					 break;
				}
				if(j==m&&b[j]-a[i][j]!=0) flag=1;
			}
		}
		if(flag) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
	}
}

C. Liebig's Barrels
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have m = n·k wooden staves. The i-th stave has length ai. You have to assemble n barrels consisting of k staves each, you can use any k staves to construct a barrel. Each stave must belong to exactly one barrel.

Let volume vj of barrel j be equal to the length of the minimal stave in it.

You want to assemble exactly n barrels with the maximal total sum of volumes. But you have to make them equal enough, so a difference between volumes of any pair of the resulting barrels must not exceed l, i.e. |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

Print maximal total sum of volumes of equal enough barrels or 0 if it's impossible to satisfy the condition above.

Input

The first line contains three space-separated integers nk and l (1 ≤ n, k ≤ 1051 ≤ n·k ≤ 1050 ≤ l ≤ 109).

The second line contains m = n·k space-separated integers a1, a2, ..., am (1 ≤ ai ≤ 109) — lengths of staves.

Output

Print single integer — maximal total sum of the volumes of barrels or 0 if it's impossible to construct exactly n barrels satisfying the condition |vx - vy| ≤ l for any 1 ≤ x ≤ n and 1 ≤ y ≤ n.

Examples
input
Copy
4 2 1
2 2 1 2 3 2 2 3
output
Copy
7
input
Copy
2 1 0
10 10
output
Copy
20
input
Copy
1 2 1
5 2
output
Copy
2
input
Copy
3 2 1
1 2 3 4 5 6
output
Copy
0
Note

In the first example you can form the following barrels: [1, 2][2, 2][2, 3][2, 3].

In the second example you can form the following barrels: [10][10].

In the third example you can form the following barrels: [2, 5].

In the fourth example difference between volumes of barrels in any partition is at least 2 so it is impossible to make barrels equal enough.

#include<bits/stdc++.h>
using namespace std;
const int _max=100005;
long long a[_max],sum;
int main(){
	int n,k,l,flag,temp;
	while(cin>>n>>k>>l){
		sum=0;
		temp=0;
		memset(a,0,sizeof(a));
		for(int i=1;i<=n*k;i++){
			cin>>a[i];
		}
		sort(a+1,a+n*k+1);
		for(int i=n*k;i>0;i--){
			temp++;
			if(a[i]-a[1]<=l&&temp>=k){
				temp-=k;
				sum+=a[i];
			}
		} 
		if(temp>0){
			cout<<"0"<<endl;
		}
		else
		cout<<sum<<endl;
	}
	return 0;
}
这道题wa了两次,都是在第七个数据wa的开始想错了,认为把这个排序一下然后看第n个数与第一个数相减是否大于l判断输出是否为零,然后把这n个数据相加,前四个都过了,后来百思不得其解,幸好cf有显示错误数据,看出来比我得出的数据大,想到第一组中可以放第一个数据为首的很多数据,再后来想到从最后一个开始循环,定义一个记数的,从开始差小于l的最大的数开始,每次减去计数的一个组数。
D. Sand Fortress
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are going to the beach with the idea to build the greatest sand castle ever in your head! The beach is not as three-dimensional as you could have imagined, it can be decribed as a line of spots to pile up sand pillars. Spots are numbered 1 through infinity from left to right.

Obviously, there is not enough sand on the beach, so you brought n packs of sand with you. Let height hi of the sand pillar on some spot i be the number of sand packs you spent on it. You can't split a sand pack to multiple pillars, all the sand from it should go to a single one. There is a fence of height equal to the height of pillar with H sand packs to the left of the first spot and you should prevent sand from going over it.

Finally you ended up with the following conditions to building the castle:

  • h1 ≤ H: no sand from the leftmost spot should go over the fence;
  • For any  |hi - hi + 1| ≤ 1: large difference in heights of two neighboring pillars can lead sand to fall down from the higher one to the lower, you really don't want this to happen;
  • : you want to spend all the sand you brought with you.

As you have infinite spots to build, it is always possible to come up with some valid castle structure. Though you want the castle to be as compact as possible.

Your task is to calculate the minimum number of spots you can occupy so that all the aforementioned conditions hold.

Input

The only line contains two integer numbers n and H (1 ≤ n, H ≤ 1018) — the number of sand packs you have and the height of the fence, respectively.

Output

Print the minimum number of spots you can occupy so the all the castle building conditions hold.

Examples
input
Copy
5 2
output
Copy
3
input
Copy
6 8
output
Copy
3
Note

Here are the heights of some valid castles:

  • n = 5, H = 2, [2, 2, 1, 0, ...], [2, 1, 1, 1, 0, ...], [1, 0, 1, 2, 1, 0, ...]
  • n = 6, H = 8, [3, 2, 1, 0, ...], [2, 2, 1, 1, 0, ...], [0, 1, 0, 1, 2, 1, 1, 0...] (this one has 5 spots occupied)

The first list for both cases is the optimal answer, 3 spots are occupied in them.

And here are some invalid ones:

  • n = 5, H = 2, [3, 2, 0, ...], [2, 3, 0, ...], [1, 0, 2, 2, ...]
  • n = 6, H = 8, [2, 2, 2, 0, ...], [6, 0, ...], [1, 4, 1, 0...], [2, 2, 1, 0, ...]


二分 

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll n,H;
const ll cnt=2000000000;
bool check(ll m){
	if(m <= H) {
	  if( m >= cnt ) return true;
	  return m * ( m + 1 ) / 2 >= n;	
	}
	ll x = ( m - H ) / 2 + H;
	if(x >= cnt ) return true;
	if( ( m - H ) % 2 != 0 )
	return x * ( x + 1 ) / 2 + ( x + H ) * ( x + 1 - H ) / 2 >= n;
	return x * ( x + 1 ) / 2 + ( x - 1 + H ) * ( x - H ) / 2 >= n;
}
int main(){
    cin>>n>>H;
    ll l = 1, h = n ;
    while(l <= h){
    	ll m = (l + h) / 2;
    	if(check(m)) h = m - 1;
    	else l = m + 1 ;
		}
		cout<< h+1 << endl;
		return 0;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值