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

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.


solution:

1. Find the number of all available "VK"s and mark all "VK"s. (ans)

2. Search the string to see if there is two adjacent 'V's or 'K's that are not marked yet . If so, ans++.

code:

#include <bits/stdc++.h>
using namespace std;
int v, k, ans;
int vis[105];
string s;
int main(){
	cin >> s;
	for(int i = 0 ; i < s.size() - 1; i++){
		if(s[i] == 'V' && s[i + 1] == 'K'){
			vis[i] = 1;
			vis[i + 1] = 1;
			ans++;
		}
	}
	for(int i = 0; i < s.size() - 1; i++){
		if(!vis[i] && !vis[i + 1]){
			if(s[i] == s[i + 1]){
				ans++;
				break;
			}
		}
	}
	printf("%d\n", ans);
return 0;
}


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

solution:

Greedy.

If any of the characters in y is greater than which in x, put "-1".

Otherwise just print y.

code:

#include <bits/stdc++.h>
using namespace std;
string x, y;
char z[105];
int main(){
	cin >> x >> y;
	int l = x.size();
	for(int i = 0; i < l; i++){
		if(y[i] <= x[i]){
			z[i] = y[i];
		}
		else {
			cout << "-1\n";
			return 0;
		}
	}
	for(int i = 0; i < l; i++){
		cout << z[i];
	}
	cout << endl;
	return 0;
}

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 0001 ≤ 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

solution:

Greedy.

1. Find the time takes to die for each device: t(i).

2. sort the time in an increasing order.

3. if we use power on first x devices, the time T(x) it takes them to die out is (sum of initial powers of first x devices) / (sum of the rate of decreasing of the first x devices - p). 

4. if T(x) < t(x + 1) then break and print T(x), or continue to add device. (x++).

Code:

#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n, p;
double rate, total;
struct NODE{
	int dec, ini;
	double time;
}t[100005];
bool cmp(NODE o, NODE w){
	return o.time < w.time;
}
int main(){
	scanf("%d%d", &n, &p);
	bool flag = 0;
	for(int i = 1; i <= n; i++){
		scanf("%d%d", &t[i].dec, &t[i].ini);
		t[i].time = 1.0 * t[i].ini / t[i].dec;
		rate += t[i].dec;
	}
	if(p >= rate){
		return (printf("-1\n")), 0;
	}
	double krate = 0;
	sort(t + 1, t + n + 1, cmp);
	t[n + 1].time = INF;
	for(int i = 1; i <= n; i++){
		total += t[i].ini;
		krate += t[i].dec;
		if(krate - p <= 0) continue;
		if(1.0 * total / (krate - p) <= t[i + 1].time){
			printf("%.12lf\n", 1.0 * total / (krate - p));
			return 0;
		}
	}
	printf("%.12lf\n", 1.0 * total / (krate - p));
return 0;
}

D. Volatile Kite
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a convex polygon P with n distinct vertices p1, p2, ..., pn. Vertex pi has coordinates (xi, yi) in the 2D plane. These vertices are listed in clockwise order.

You can choose a real number D and move each vertex of the polygon a distance of at most D from their original positions.

Find the maximum value of D such that no matter how you move the vertices, the polygon does not intersect itself and stays convex.

Input

The first line has one integer n (4 ≤ n ≤ 1 000) — the number of vertices.

The next n lines contain the coordinates of the vertices. Line i contains two integers xi and yi ( - 109 ≤ xi, yi ≤ 109) — the coordinates of the i-th vertex. These points are guaranteed to be given in clockwise order, and will form a strictly convex polygon (in particular, no three consecutive points lie on the same straight line).

Output

Print one real number D, which is the maximum real number such that no matter how you move the vertices, the polygon stays convex.

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

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
4
0 0
0 1
1 1
1 0
output
0.3535533906
input
6
5 0
10 0
12 -4
10 -8
5 -8
3 -4
output
1.0000000000

Solution:

1. The maximum available vibration=1/2* the distance from node i to the line formed by nodes i - 1 and i + 1.

formula for the distance : 


then use some math to solve.

Code:

#include <bits/stdc++.h>
using namespace std;
const int INF = ~0U>>1;
int n;
double x[1003], y[1003], ans;
int main(){
	scanf("%d", &n);
	for(int i = 1; i <= n; i++){
		scanf("%lf%lf", &x[i], &y[i]);
	}
	x[n + 1] = x[1];
	y[n + 1] = y[1];
	x[0] = x[n];
	y[0] = y[n];
	ans = INF;
	for(int i = 1; i <= n; i++){
		if(x[i + 1] == x[i - 1]){
			if(abs((x[i] - x[i - 1]) / 2) < ans) ans = abs((x[i] - x[i - 1]) / 2);
			continue;
		}
		double slope = 1.0 * (1.0 * y[i + 1] - y[i - 1]) / (x[i + 1] - x[i - 1]);
		double inter = 1.0 * y[i + 1] - slope * x[i + 1];
		double a = slope, c = inter;
		double dis;
		dis = abs((a * x[i] - 1.0 * y[i] + c) / sqrt(1.0 * (1.0 * a * a + 1.0)) / 2);
		if(dis < ans) ans = dis;
	}
	printf("%.12lf\n", ans);
return 0;
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值