Codeforces Round #466 (Div. 2)

我太菜了没完赛……

第一次CF就这么飞机了……

==========

User : ChrisSail
Rank: 2543
A: 150
B~F: 0

==========

所以只记了两题在这里。

A. Points on the line

tag : 枚举,暴力


We've got no test cases. A big olympiad is coming up. But the problemsetters' number one priority should be adding another problem to the round.

The diameter of a multiset of points on the line is the largest distance between two points from this set. For example, the diameter of the multiset {1, 3, 2, 1} is 2.

Diameter of multiset consisting of one point is 0.

You are given n points on the line. What is the minimum number of points you have to remove, so that the diameter of the multiset of the remaining points will not exceed d?

Input

The first line contains two integers n and d (1 ≤ n ≤ 100, 0 ≤ d ≤ 100) — the amount of points and the maximum allowed diameter respectively.

The second line contains n space separated integers (1 ≤ xi ≤ 100) — the coordinates of the points.

Output

Output a single integer — the minimum number of points you have to remove.

Examples

input

3 1
2 1 4

output

1

input

3 0
7 7 7

output

0

input

6 3
1 3 4 6 9 10

output

3

Note

In the first test case the optimal strategy is to remove the point with coordinate 4. The remaining points will have coordinates 1 and 2, so the diameter will be equal to 2 - 1 = 1.

In the second test case the diameter is equal to 0, so its is unnecessary to remove any points.

In the third test case the optimal strategy is to remove points with coordinates 1, 9 and 10. The remaining points will have coordinates 3, 4 and 6, so the diameter will be equal to 6 - 3 = 3.


这应该是一道暴力的……但是我想复杂了……所以先挂了四次。

#include<cstdio>
#include<cmath>
#define MAXN 105
using namespace std;
int N,D;
int A[MAXN];

inline void qs(int l,int r){
	int i=l,j=r,s=A[(l+r)>>1],t;
	while(true){
		while(A[i]<s)++i;
		while(A[j]>s)--j;
		if(i<=j){
			t = A[i];A[i] = A[j];A[j] = t;
			++i;--j; 
		}
		if(i>j)break;
	}
	if(i<r)qs(i,r);
	if(l<j)qs(l,j);
} 

int main(){
	
	scanf("%d%d",&N,&D);
	for(register int i=0;i<N;++i){
		scanf("%d",&A[i]);
	}
	
	qs(0,N-1);
	
	int min = 2147483647;
	
	for(register int i=0;i<N;++i){
		
		if(i-1>min)break;
		int cnt = 0;
		for(register int j=i;j<N;++j){
			if(A[j]-A[i]>D)break;
			cnt++;
		}
		if(N-cnt<min)min=N-cnt;
		
	}
	
	printf("%d",min);
	return 0;
}

B. Our Tanya is Crying Out Loud

tag : 贪心


Right now she actually isn't. But she will be, if you don't solve this problem.

You are given integers n, k, A and B. There is a number x, which is initially equal to n. You are allowed to perform two types of operations:

  1. Subtract 1 from x. This operation costs you A coins.

  2. Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.

What is the minimum amount of coins you have to pay to make x equal to 1?

Input

The first line contains a single integer n (1 ≤ n ≤ 2·109).

The second line contains a single integer k (1 ≤ k ≤ 2·109).

The third line contains a single integer A (1 ≤ A ≤ 2·109).

The fourth line contains a single integer B (1 ≤ B ≤ 2·109).

Output

Output a single integer — the minimum amount of coins you have to pay to make x equal to 1.

Examples

input

9
2
3
1

output

6

input

5
5
2
20

output

8

input

19
3
4
2

output

12

Note

In the first testcase, the optimal strategy is as follows:

Subtract 1 from x (9 → 8) paying 3 coins.

Divide x by 2 (8 → 4) paying 1 coin.

Divide x by 2 (4 → 2) paying 1 coin.

Divide x by 2 (2 → 1) paying 1 coin.

The total cost is 6 coins.

In the second test case the optimal strategy is to subtract 1 from x 4 times paying 8 coins in total.


这道题数据范围很大。最初想到广搜,但发现并不需要。

因为数据范围比较大,所以需要考虑使用long long

#include<cstdio>
using namespace std;


int main(){
	long long N;
	long long A,B,k;
	long long ans = 0;
	scanf("%I64d%I64d%I64d%I64d",&N,&k,&A,&B);
	
	while(N>1){
		
		if(k==1){
			ans = (N-1)*A;
			break;
		}//这是一个特殊情况,由于赛时没有考虑到而导致没有分
		
		if(N<k){
			ans += (N-1)*A;N=0;
		}
		else if(N%k!=0){
			long long num = N/k;num*=k;
			ans+=(N-num)*A;N=num; 
		}
		else{
			long long now = N/k;
			long long a = (N-now)*A;
			N=now;
			ans += a>B?B:a;
		}
	}
	printf("%I64d",ans);
	return 0;
} 
posted @ 2018-02-24 20:49  Neworld1111  阅读(220)  评论(0编辑  收藏  举报