Codeforces Round #466 (Div. 2)

A. Points on the line
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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
Copy
3 1
2 1 4
output
1
input
Copy
3 0
7 7 7
output
0
input
Copy
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 19 and 10. The remaining points will have coordinates 3,4 and 6, so the diameter will be equal to 6 - 3 = 3.

→ Source
#include<bits/stdc++.h>
using namespace std;
int n,d,a[105],ans;
int main(){
	scanf("%d%d",&n,&d);
	for(int i=1;i<=n;++i)	scanf("%d",&a[i]);
	sort(a+1,a+n+1);
	for(int i=1;i<=n;++i)
		for(int j=i;j<=n;++j)
			if(a[j]-a[i]<=d)	ans=max(ans,j-i+1);
	printf("%d",n-ans);
	return 0;
}



B. Our Tanya is Crying Out Loud
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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

You are given integers nkA 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
Copy
9
2
3
1
output
6
input
Copy
5
5
2
20
output
8
input
Copy
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.

→ Source
#include<bits/stdc++.h>
using namespace std;
long long n,k,A,B,ans=0;
int main(){
	cin>>n>>k>>A>>B;
	if(k==1){
		cout<<(n-1)*A;
		return 0;
	}
	while(n!=1){
		if(n%k==0){
			long long x=n/k;
			ans+=min(A*(n-x),B);
			n=x;
		}
		else{
			long long x=n/k*k;
			if(x==0){
				cout<<ans+(n-1)*A;
				return 0;
			}
			ans+=A*(n-x);
			n=x;
		}
	}
	cout<<ans;
	return 0;
}


C. Phone Numbers
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

And where the are the phone numbers?

You are given a string s consisting of lowercase English letters and an integer k. Find the lexicographically smallest string t of length k, such that its set of letters is a subset of the set of letters of s and s is lexicographically smaller than t.

It's guaranteed that the answer exists.

Note that the set of letters is a set, not a multiset. For example, the set of letters of abadaba is {a, b, d}.

String p is lexicographically smaller than string q, if p is a prefix of q, is not equal to q or there exists i, such that pi < qi and for all j < i it is satisfied that pj = qj. For example, abc is lexicographically smaller than abcd , abd is lexicographically smaller than abecafa is not lexicographically smaller than ab and a is not lexicographically smaller than a.

Input

The first line of input contains two space separated integers n and k (1 ≤ n, k ≤ 100 000) — the length of s and the required length of t.

The second line of input contains the string s consisting of n lowercase English letters.

Output

Output the string t conforming to the requirements above.

It's guaranteed that the answer exists.

Examples
input
Copy
3 3
abc
output
aca
input
Copy
3 2
abc
output
ac
input
Copy
3 3
ayy
output
yaa
input
Copy
2 3
ba
output
baa
Note

In the first example the list of strings t of length 3, such that the set of letters of t is a subset of letters of s is as follows: aaaaabaac,abaabbabcacaacb.... Among them, those are lexicographically greater than abcacaacb.... Out of those the lexicographically smallest is aca.

→ Source
#include<bits/stdc++.h>
using namespace std;
int n,k,tp,rnk[35];
char s[200005],a[34],p[200005];
bool b[34];
int main(){
	scanf("%d%d",&n,&k);
	scanf("%s",s+1);
	for(int i=1;i<=n;++i)
		if(!b[s[i]-'a']) b[s[i]-'a']=1,a[++tp]=s[i];
	sort(a+1,a+tp+1);
	for(int i=1;i<=tp;++i)	rnk[a[i]-'a']=i;
	if(n>=k){
		for(int i=1;i<=k;++i)	p[i]=s[i];
		for(int i=k;i>=1;--i){
			if(rnk[p[i]-'a']<tp){
				p[i]=a[rnk[p[i]-'a']+1];
				break;
			}
			else	p[i]=a[1];
		}
	}
	else{
		for(int i=1;i<=n;++i)	p[i]=s[i];
		for(int i=n+1;i<=k;++i)	p[i]=a[1];
	}
	for(int i=1;i<=k;++i)	printf("%c",p[i]);
	return 0;
}


D. Alena And The Heater
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

"We've tried solitary confinement, waterboarding and listening to Just In Beaver, to no avail. We need something extreme."

"Little Alena got an array as a birthday present..."

The array b of length n is obtained from the array a of length n and two integers l and r (l ≤ r) using the following procedure:

b1 = b2 = b3 = b4 = 0.

For all 5 ≤ i ≤ n:

  • bi = 0 if ai, ai - 1, ai - 2, ai - 3, ai - 4 > r and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 1
  • bi = 1 if ai, ai - 1, ai - 2, ai - 3, ai - 4 < l and bi - 1 = bi - 2 = bi - 3 = bi - 4 = 0
  • bi = bi - 1 otherwise

You are given arrays a and b' of the same length. Find two integers l and r (l ≤ r), such that applying the algorithm described above will yield an array b equal to b'.

It's guaranteed that the answer exists.

Input

The first line of input contains a single integer n (5 ≤ n ≤ 105) — the length of a and b'.

The second line of input contains n space separated integers a1, ..., an ( - 109 ≤ ai ≤ 109) — the elements of a.

The third line of input contains a string of n characters, consisting of 0 and 1 — the elements of b'. Note that they are not separated by spaces.

Output

Output two integers l and r ( - 109 ≤ l ≤ r ≤ 109), conforming to the requirements described above.

If there are multiple solutions, output any of them.

It's guaranteed that the answer exists.

Examples
input
Copy
5
1 2 3 4 5
00001
output
6 15
input
Copy
10
-10 -9 -8 -7 -6 6 7 8 9 10
0000111110
output
-5 5
Note

In the first test case any pair of l and r pair is valid, if 6 ≤ l ≤ r ≤ 109, in that case b5 = 1, because a1, ..., a5 < l.

→ Source
#include<bits/stdc++.h>
using namespace std;
const int inf=1e9,N=100005;
int n,a[N],MinL=-inf,MaxL=inf,MinR=-inf,MaxR=inf;
char b[N];
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)	scanf("%d",&a[i]);
	scanf("%s",b+1);
	for(int i=5;i<=n;){
		if(b[i]=='0' && b[i-1]=='1'){
			for(int j=0;j<=4;++j){
				MaxR=min(MaxR,a[i-j]-1);
			}
			i+=4;
			continue;
		}
		if(b[i]=='1' && b[i-1]=='0'){
			for(int j=0;j<=4;++j){
				MinL=max(MinL,a[i-j]+1);
			}
			i+=4;
			continue;
		}
		++i;
	}
	printf("%d %d",MinL,MaxR);
	return 0;
}


E. Cashback
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Since you are the best Wraith King, Nizhniy Magazin «Mir» at the centre of Vinnytsia is offering you a discount.

You are given an array a of length n and an integer c.

The value of some array b of length k is the sum of its elements except for the  smallest. For example, the value of the array[3, 1, 6, 5, 2] with c = 2 is 3 + 6 + 5 = 14.

Among all possible partitions of a into contiguous subarrays output the smallest possible sum of the values of these subarrays.

Input

The first line contains integers n and c (1 ≤ n, c ≤ 100 000).

The second line contains n integers ai (1 ≤ ai ≤ 109) — elements of a.

Output

Output a single integer  — the smallest possible sum of values of these subarrays of some partition of a.

Examples
input
Copy
3 5
1 2 3
output
6
input
Copy
12 10
1 1 10 10 10 10 10 10 9 10 10 10
output
92
input
Copy
7 2
2 3 6 4 5 7 1
output
17
input
Copy
8 4
1 3 4 5 5 3 4 1
output
23
Note

In the first example any partition yields 6 as the sum.

In the second example one of the optimal partitions is [1, 1], [10, 10, 10, 10, 10, 10, 9, 10, 10, 10] with the values 2 and 90 respectively.

In the third example one of the optimal partitions is [2, 3], [6, 4, 5, 7], [1] with the values 3, 13 and 1 respectively.

In the fourth example one of the optimal partitions is [1], [3, 4, 5, 5, 3, 4], [1] with the values 1, 21 and 1 respectively.

→ Source
#include<bits/stdc++.h>
#define pa pair<long long,long long>
using namespace std;
deque<pa>q;
long long n,c;
long long f[100005],sum;
int main(){
	scanf("%I64d%I64d",&n,&c);
	for(long long i=1;i<=n;++i){
		long long x;
		scanf("%I64d",&x);sum+=x;
		while(!q.empty() && q.front().second<=i-c) q.pop_front();
		while(!q.empty() && q.back().first>=x)	q.pop_back();
		q.push_back(pa(x,i));
		if(i>=c)	f[i]=max(f[i-1],f[i-c]+q.front().first);
	}
	printf("%I64d",sum-f[n]);
	return 0;
}


F. Machine Learning
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You come home and fell some unpleasant smell. Where is it coming from?

You are given an array a. You have to answer the following queries:

  1. You are given two integers l and r. Let ci be the number of occurrences of i in al: r, where al: r is the subarray of a from l-th element to r-th inclusive. Find the Mex of {c0, c1, ..., c109}
  2. You are given two integers p to x. Change ap to x.

The Mex of a multiset of numbers is the smallest non-negative integer not in the set.

Note that in this problem all elements of a are positive, which means that c0 = 0 and 0 is never the answer for the query of the second type.

Input

The first line of input contains two integers n and q (1 ≤ n, q ≤ 100 000) — the length of the array and the number of queries respectively.

The second line of input contains n integers — a1a2...an (1 ≤ ai ≤ 109).

Each of the next q lines describes a single query.

The first type of query is described by three integers ti = 1liri, where 1 ≤ li ≤ ri ≤ n — the bounds of the subarray.

The second type of query is described by three integers ti = 2pixi, where 1 ≤ pi ≤ n is the index of the element, which must be changed and 1 ≤ xi ≤ 109 is the new value.

Output

For each query of the first type output a single integer  — the Mex of {c0, c1, ..., c109}.

Example
input
Copy
10 4
1 2 3 1 1 2 2 2 9 9
1 1 1
1 2 8
2 7 1
1 2 8
output
2
3
2
Note

The subarray of the first query consists of the single element — 1.

The subarray of the second query consists of four 2s, one 3 and two 1s.

The subarray of the fourth query consists of three 1s, three 2s and one 3.

→ Source
#include<bits/stdc++.h>
#define N 100005
using namespace std;
int n,t,cnt,blocksize,a[N],b[2*N],pre[N],block[N],sum[2*N],exist[N];
struct Question{
	int l,r,tim,id,ans;
	Question(){}
	Question(int _,int __,int ___,int ____){l=_;r=__;tim=___;id=____;}
}q[N];int m;
inline bool cmp(Question a,Question b){return block[a.l]<block[b.l] || block[a.l]==block[b.l] && block[a.r]<block[b.r] || block[a.l]==block[b.l] && block[a.r]==block[b.r] && a.tim<b.tim;}
inline bool cmpid(Question a,Question b){return a.id<b.id;}
struct Modify{
	int x,y,pre;
	Modify(){}
	Modify(int _,int __,int ___){x=_;y=__;pre=___;}
}c[N];int tp;
inline void init(){
	scanf("%d%d",&n,&t);blocksize=max(1,min(n,(int)(pow(n,2.0/3))));
	for(int i=1;i<=n;++i)	scanf("%d",&a[i]),pre[i]=a[i],b[++cnt]=a[i],block[i]=(i-1)/blocksize+1;
	for(int i=1;i<=t;++i){
		int opt,x,y;
		scanf("%d%d%d",&opt,&x,&y);
		if(opt==1)	q[++m]=Question(x,y,tp,i);
		else{
			c[++tp]=Modify(x,y,pre[x]),b[++cnt]=y;
			pre[x]=y;
		}
	}
	sort(b+1,b+cnt+1);cnt=unique(b+1,b+cnt+1)-b-1;
	for(int i=1;i<=n;++i)	a[i]=lower_bound(b+1,b+cnt+1,a[i])-b;
	for(int i=1;i<=tp;++i)	c[i].y=lower_bound(b+1,b+cnt+1,c[i].y)-b,c[i].pre=lower_bound(b+1,b+cnt+1,c[i].pre)-b;
}
inline void solve(){
	sort(q+1,q+m+1,cmp);
	for(int l=1,r=0,i=1;i<=m;++i){
		for(int j=q[i-1].tim+1;j<=q[i].tim;++j){
			if(l<=c[j].x && c[j].x<=r){
				--exist[sum[a[c[j].x]]];
				++exist[--sum[a[c[j].x]]];
				a[c[j].x]=c[j].y;
				--exist[sum[a[c[j].x]]];
				++exist[++sum[a[c[j].x]]];
			}
			else a[c[j].x]=c[j].y;
		}
		for(int j=q[i-1].tim;j>q[i].tim;--j){
			if(l<=c[j].x && c[j].x<=r){
				--exist[sum[a[c[j].x]]];
				++exist[--sum[a[c[j].x]]];
				a[c[j].x]=c[j].pre;
				--exist[sum[a[c[j].x]]];
				++exist[++sum[a[c[j].x]]];
			}
			else a[c[j].x]=c[j].pre;
		}
		while(r<q[i].r){
			++r;
			--exist[sum[a[r]]];
			++exist[++sum[a[r]]];
		}
		while(l>q[i].l){
			--l;
			--exist[sum[a[l]]];
			++exist[++sum[a[l]]];
		}
		while(r>q[i].r){
			--exist[sum[a[r]]];
			++exist[--sum[a[r]]];
			--r;
		}
		while(l<q[i].l){
			--exist[sum[a[l]]];
			++exist[--sum[a[l]]];
			++l;
		}
		q[i].ans=1;
		while(exist[q[i].ans])	++q[i].ans;
	}
	sort(q+1,q+m+1,cmpid);
	for(int i=1;i<=m;++i)	printf("%d\n",q[i].ans);
}
int main(){
	init();
	solve();
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zP1nG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值