Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)

A. Vicious Keyboard
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Tonio has a keyboard with only two letters, "V" and "K".

One day, he has typed out a string s with only these two letters. He really likes it when the string "VK" appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times "VK" can appear as a substring (i. e. a letter "K" right after a letter "V") in the resulting string.

Input

The first line will contain a string s consisting only of uppercase English letters "V" and "K" with length not less than 1 and not greater than 100.

Output

Output a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.

Examples
input
VK
output
1
input
VV
output
1
input
V
output
0
input
VKKKKKKKKKVVVVVVVVVK
output
3
input
KVKV
output
1
Note

For the first case, we do not change any letters. "VK" appears once, which is the maximum number of times it could appear.

For the second case, we can change the second character from a "V" to a "K". This will give us the string "VK". This has one occurrence of the string "VK" as a substring.

For the fourth case, we can change the fourth character from a "K" to a "V". This will give us the string "VKKVKKKKKKVVVVVVVVVK". This has three occurrences of the string "VK" as a substring. We can check no other moves can give us strictly more occurrences.

 

 

题意: 给定一个只有VK组成的字符串,最多修改一个字符,使VK最多,问最多有几个VK?

 

很尴尬,B题C题一发过,A题卡了N次

主要是:

KV 应该是0

 

其他的应该模拟就好了。

 1 #include<bits/stdc++.h>
 2 using namespace std ;
 3 
 4 int main()
 5 {
 6     string s ;
 7     cin >> s ;
 8     string s2 = s ;
 9     int ans2 = 0 ;
10 /*    int ans = 0 ;
11     bool flag = false ;
12     for( int i = 0 ; i < s.size() ; )
13         {
14             if( s.substr(i,2) == "VK" )
15                 ans++ , i+=2;
16             else
17                 {
18                     if( s[i] == 'V' && s[i+1] != 'K'  && i + 1 < s.size() && !flag )
19                         flag = true ,i++;
20                     else if( s[i] != 'V' && s[i+1] == 'K' && i+1 < s.size() && !flag )
21                         flag = true ,i++;
22                     i++;
23                 }
24         }
25     if( flag )
26         ans++;*/
27     for( int i = 0 ; i < s2.size() ; )
28         {
29             if( s2.substr(i,2) == "VK" )
30                 {
31                     s2[i] = '1' , s2[i+1] = '1' ;
32                     ans2++ , i+=2;
33                 }
34             else
35                 i++;
36 
37         }
38     bool ff = false ;
39     for( int i = 0 ; i < s2.size() ; i++)
40         {
41             if( s2[i] != '1' && i+1 < s2.size() && s2[i+1] != '1' )
42                 {
43                     if( s2[i] == 'V' && s2[i+1] != 'K' )
44                         ff = true ;
45                     else if( s2[i] !='V' && s2[i+1] == 'K' )
46                         ff = true ;
47                     if( ff ) 
48                         break ;
49                 }
50 
51         }
52 
53 
54     if( ff )
55         ans2++;
56     cout << ans2 << endl ;
57     return 0 ;
58 }
A题

 

 

B. Valued Keys
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You found a mysterious function f. The function takes two strings s1 and s2. These strings must consist only of lowercase English letters, and must be the same length.

The output of the function f is another string of the same length. The i-th character of the output is equal to the minimum of the i-th character of s1 and the i-th character of s2.

For example, f("ab", "ba") = "aa", and f("nzwzl", "zizez") = "niwel".

You found two strings x and y of the same length and consisting of only lowercase English letters. Find any string z such that f(x, z) = y, or print -1 if no such string z exists.

Input

The first line of input contains the string x.

The second line of input contains the string y.

Both x and y consist only of lowercase English letters, x and y have same length and this length is between 1 and 100.

Output

If there is no string z such that f(x, z) = y, print -1.

Otherwise, print a string z such that f(x, z) = y. If there are multiple possible answers, print any of them. The string z should be the same length as x and y and consist only of lowercase English letters.

Examples
input
ab
aa
output
ba
input
nzwzl
niwel
output
xiyez
input
ab
ba
output
-1
Note

The first case is from the statement.

Another solution for the second case is "zizez"

There is no solution for the third case. That is, there is no z such that f("ab", z) =  "ba".

 

题意:

已知函数f( string x , string y ) = string z  其中zi = min ( xi , yi ) 

给出x,z,求任意一个满足条件的y,如果不满足,输出-1

这题如果xi <= zi ,那么 yi = xi 

否则 输出-1

 

 1 #include<bits/stdc++.h>
 2 using namespace std ;
 3 char z[200];
 4 int main()
 5 {
 6     string x , y ;
 7     cin >> x >> y ;
 8     bool flag = true ;
 9     for( int i = 0 ; i < x.size() ; i++)
10     {
11         if( y[i]  == x[i] )
12             z[i] = y[i] ;
13         else if( y[i] > x[i] )
14             {
15                 flag = false ; 
16             }
17         else if( y[i] < x[i] )
18             z[i] = y[i] ;
19     }
20     
21     if( !flag )
22         cout << -1 << endl ;
23     else
24         cout << z << endl ;
25     return 0 ; 
26 }
B

 

 

C. Voltage Keepsake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have n devices that you want to use simultaneously.

The i-th device uses ai units of power per second. This usage is continuous. That is, in λ seconds, the device will use λ·ai units of power. The i-th device currently has bi units of power stored. All devices can store an arbitrary amount of power.

You have a single charger that can plug to any single device. The charger will add p units of power per second to a device. This charging is continuous. That is, if you plug in a device for λ seconds, it will gain λ·p units of power. You can switch which device is charging at any arbitrary unit of time (including real numbers), and the time it takes to switch is negligible.

You are wondering, what is the maximum amount of time you can use the devices until one of them hits 0 units of power.

If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.

Input

The first line contains two integers, n and p (1 ≤ n ≤ 100 000, 1 ≤ p ≤ 109) — the number of devices and the power of the charger.

This is followed by n lines which contain two integers each. Line i contains the integers ai and bi (1 ≤ ai, bi ≤ 100 000) — the power of the device and the amount of power stored in the device in the beginning.

Output

If you can use the devices indefinitely, print -1. Otherwise, print the maximum amount of time before any one device hits 0 power.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.

Namely, let's assume that your answer is a and the answer of the jury is b. The checker program will consider your answer correct if .

Examples
input
2 1
2 2
2 1000
output
2.0000000000
input
1 100
1 1
output
-1
input
3 5
4 3
5 2
6 1
output
0.5000000000
Note

In sample test 1, you can charge the first device for the entire time until it hits zero power. The second device has enough power to last this time without being charged.

In sample test 2, you can use the device indefinitely.

In sample test 3, we can charge the third device for 2 / 5 of a second, then switch to charge the second device for a 1 / 10 of a second.

 

 

 

题意:

 

给一个 充电器,每秒可充p个电,

有n个物品,每个物品 耗电量为a,储存电量为b

问可使所有物品共同使用多少秒?如果可无限使用,输出-1

 

做法:

 

二分时间,把所有电量( ai - time*bi) 小于0的累加起来,

然后看time*p能否把亏损的电量补足。

 

这里比较遗憾的是 边界没考虑清楚,mid > 1e10 输出-1 有点小了

有一个极限数据就是1e10,因精度问题FST了。

 

以及,在这次学会了使用LONG DOUBLE , 以及二分次数的方法避免eps

 

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const double eps = 1e-6;
 4 struct p
 5 {
 6     int a , b ;
 7 } a[100000+10];
 8 int main()
 9 {
10     int n ;
11     long long int p ;
12     cin >> n >> p ;
13     for( int i = 1 ; i <= n ; i++)
14         cin >> a[i].a >> a[i].b ;
15 
16     double l = 0 , r = 1e12 , mid = ( l + r ) / 2 ;
17 
18     while( fabs( r - l ) > eps  )
19         {
20             double  temp = 0 ;
21 
22             for( int i = 1 ; i <= n ; i++)
23                 {
24                     double t = a[i].b - mid*a[i].a ;
25                     if( t < 0 )
26                         temp+=t;
27                 }
28             if( mid * p + temp >= 0 )
29             {
30                 l = mid ;
31             mid = ( l + r ) / 2 ;
32             }
33             else
34             {
35                 r = mid ;
36                 mid = ( l + r ) / 2 ;
37             }
38         }
39         if( mid > 1e10 )
40             cout << -1 << endl ;
41         else
42             cout << mid << endl ;
43         return 0 ; 
44 }
C题

 

posted on 2017-04-17 19:04  Dark猫  阅读(233)  评论(0)    收藏  举报

导航