Codeforces Round #466 (Div. 2)(ABC)

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, 4 and 6, so the diameter will be equal to 6 - 3 = 3.

找到相同区间最多包含多少个数就行了。

 

直接贴代码了

 1 #include <iostream>
 2 #define N 105
 3 using namespace std;
 4 
 5 int vis[N];
 6 int n,m;
 7 
 8 int main(){
 9     cin>>n>>m;
10     for(int i=0;i<n;i++){
11         int x;
12         cin>>x;
13         vis[x]++;
14     }
15     if(m==100){
16         cout<<0<<endl;
17         return 0;
18     }
19     int sum = 0;
20     for(int i=1;i<=100-m;i++){
21         int ans=0;
22         for(int j=i;j<=i+m;j++){
23             ans+=vis[j];
24         }
25         sum=max(sum,ans);
26     }
27     cout<<n-sum<<endl;
28     return 0;
29 }

 

 

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 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
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.

 

这个可以循环递归找最小。

当然要记得特判。

 1 #include <iostream>
 2 #define ll long long int
 3 using namespace std;
 4 ll n,k,a,b;
 5 ll work(ll x) {
 6   if (x == 1) return 0;
 7   if (x < k) return (x-1)*a;
 8   return min(x%k*a+work(x/k)+b, (x-1)*a);
 9 }
10 int main(){
11     cin>>n>>k>>a>>b;
12     if (k == 1)
13         cout << (n-1)*a << endl;
14     else
15         cout<<work(n)<<endl;
16     return 0;
17 }

 

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 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: 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.

这个其实逐一分析一下就可以出结果了。

 1 #include <iostream>
 2 
 3 using namespace std;
 4 int n,m;
 5 bool vis[28];
 6 string s;
 7 int main(){
 8     cin>>n>>m>>s;
 9     int len = s.length();
10     int Min = 26;
11     int Max = 1;
12     for(int i=0;i<len;i++){
13         int x = s[i]-'a';
14         vis[x]=true;
15         Min = min(Min,x);
16         Max = max(Max,x);
17     }
18     if(m>n){
19         cout<<s;
20         char c = Min+'a';
21         for(int i=0;i<m-n;i++)
22             cout<<c;
23         cout<<endl;
24     }else{
25         int p = m-1;
26         char MI = Min+'a';
27         int x = s[p]-'a';
28         while(x==Max){
29             p--;
30             x=s[p]-'a';
31         }
32         for(int i=0;i<p;i++)
33             cout<<s[i];
34         for(int i=x+1;i<=26;i++){
35             if(vis[i]){
36                 char c = i+'a';
37                 cout<<c;
38                 break;
39             }
40         }
41         for(int i=p+1;i<m;i++)
42             cout<<MI;
43         cout<<endl;
44     }
45     return 0;
46 }

 

posted @ 2018-02-27 22:31  #忘乎所以#  阅读(226)  评论(0编辑  收藏  举报