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?
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 a single integer — the minimum number of points you have to remove.
3 1 2 1 4
1
3 0 7 7 7
0
6 3 1 3 4 6 9 10
3
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<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;
}
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:
- Subtract 1 from x. This operation costs you A coins.
- Divide x by k. Can be performed only if x is divisible by k. This operation costs you B coins.
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 a single integer — the minimum amount of coins you have to pay to make x equal to 1.
9 2 3 1
6
5 5 2 20
8
19 3 4 2
12
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.
#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;
}
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 abec, afa is not lexicographically smaller than ab and a is not lexicographically smaller than a.
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 the string t conforming to the requirements above.
It's guaranteed that the answer exists.
3 3 abc
aca
3 2 abc
ac
3 3 ayy
yaa
2 3 ba
baa
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: aaa, aab, aac,aba, abb, abc, aca, acb, .... Among them, those are lexicographically greater than abc: aca, acb, .... Out of those the lexicographically smallest is aca.
#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;
}
"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.
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 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.
5 1 2 3 4 5 00001
6 15
10 -10 -9 -8 -7 -6 6 7 8 9 10 0000111110
-5 5
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.
#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;
}
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.
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 a single integer — the smallest possible sum of values of these subarrays of some partition of a.
3 5 1 2 3
6
12 10 1 1 10 10 10 10 10 10 9 10 10 10
92
7 2 2 3 6 4 5 7 1
17
8 4 1 3 4 5 5 3 4 1
23
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.
#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;
}
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:
- 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}
- 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.
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 — a1, a2, ..., 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 = 1, li, ri, where 1 ≤ li ≤ ri ≤ n — the bounds of the subarray.
The second type of query is described by three integers ti = 2, pi, xi, where 1 ≤ pi ≤ n is the index of the element, which must be changed and 1 ≤ xi ≤ 109 is the new value.
For each query of the first type output a single integer — the Mex of {c0, c1, ..., c109}.
10 4 1 2 3 1 1 2 2 2 9 9 1 1 1 1 2 8 2 7 1 1 2 8
2 3 2
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.
#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;
}