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

你可以移动最少的点,这个区间差不大于d,直接模拟啊

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=10005;
int main()
{
    int a[105],d,n;
    cin>>n>>d;
    for(int i=0;i<n;i++)
        cin>>a[i];
    sort(a,a+n);
    int ma=0;
    for(int i=0;i<n;i++)
    {
       int cnt=0,pos=i;
       while(a[pos]<=a[i]+d&&pos<n)
           pos++,cnt++;
       ma=max(ma,cnt);
    }
    cout<<n-ma<<endl;
    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.

 

 这个就是一个变换,直接递归就可以了。k>1的复杂度就相当于二分很快的

能被整除就看看怎么减少的多,不能就继续下去,不过既然/减少的多了,那么以后肯定还是-1,这是一个剪枝

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,k,a,b,s=0;
    cin>>n>>k>>a>>b;
    if(k==1)
        cout<<(n-1)*a;
    else
    {
        while(n!=1)
        {
            if(n%k==0)
                s+=min((n-n/k)*a,b),n/=k;
            else
                if(n>k)s+=n%k*a,n-=n%k;
            else
                s+=(n-1)*a,n=1;
        }
        cout<<s;
    }
    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 abec, afa is notlexicographically smaller than ab and 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: 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.

 

 这就是一个贪心的题,可以理解为找一个长度为k的26进制数大于当前值且数字为所给的字符

得到r直接sort unique吧,当时有点傻

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int n,k;
    string s;
    cin>>n>>k>>s;
    if(k>n)
    {
        char c='z';
        for(int i=0; s[i]; i++)c=min(c,s[i]);
        for(int i=0; i<k-n; i++)s+=c;
        cout<<s;
    }
    else
    {
        string t=s.substr(0,k),r;
        unordered_set<char>S;
        for(int i=0; s[i]; i++)S.insert(s[i]);
        for(auto X:S)r+=X;
        sort(r.begin(),r.end());
        for(int i=k-1; i>=0; i--)
        {
            if(t[i]==*r.rbegin())t[i]=r[0];
            else
            {
                t[i]=*upper_bound(r.begin(),r.end(),t[i]);
                break;
            }
        }
        cout<<t;
    }
    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.

D大力模拟就可以了啊

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5,MD=1e9;
int a[N];
string s;
int main()
{
    int n;
    cin>>n;
    for(int i=0; i<n; i++)cin>>a[i];
    cin>>s;
    int l1=-MD,l2=MD,r1=-MD,r2=MD;
    for(int i=4; i<n; i++)
    {
        if(s[i-1]=='1'&&s[i-1]==s[i-2]&&s[i-2]==s[i-3]&&s[i-3]==s[i-4]&&s[i]=='0')
            r2=min(r2,min(min(min(min(a[i],a[i-1]),a[i-2]),a[i-3]),a[i-4])-1);
        else if(s[i-1]=='1'&&s[i-1]==s[i-2]&&s[i-2]==s[i-3]&&s[i-3]==s[i-4]&&s[i]=='1')
            r1=max(r1,min(min(min(min(a[i],a[i-1]),a[i-2]),a[i-3]),a[i-4]));
        else if(s[i-1]=='0'&&s[i-1]==s[i-2]&&s[i-2]==s[i-3]&&s[i-3]==s[i-4]&&s[i]=='1')
            l1=max(l1,max(max(max(max(a[i],a[i-1]),a[i-2]),a[i-3]),a[i-4])+1);
        else if(s[i-1]=='0'&&s[i-1]==s[i-2]&&s[i-2]==s[i-3]&&s[i-3]==s[i-4]&&s[i]=='0')
            l2=min(l2,max(max(max(max(a[i],a[i-1]),a[i-2]),a[i-3]),a[i-4]));
    }
    cout<<l1<<" "<<r2;
    return 0;
}

 

posted @ 2018-02-24 23:58  暴力都不会的蒟蒻  阅读(414)  评论(0编辑  收藏  举报