Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A -- D
来源:互联网 发布:程序员经常去的论坛 编辑:程序博客网 时间:2024/05/17 00:10
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.
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 a single integer, the maximum number of times "VK" can appear as a substring of the given string after changing at most one character.
VK
1
VV
1
V
0
VKKKKKKKKKVVVVVVVVVK
3
KVKV
1
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;}
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.
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.
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.
abaa
ba
nzwzlniwel
xiyez
abba
-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;}
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.
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.
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 .
2 12 22 1000
2.0000000000
1 1001 1
-1
3 54 35 26 1
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;}
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.
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).
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 .
40 00 11 11 0
0.3535533906
65 010 012 -410 -85 -83 -4
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;}
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A -- D
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) D. Volatile Kite
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A. Vicious Keyboard
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) B. Valued Keys
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) C. Voltage Keepsake
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-C. Voltage Keepsake
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) ABCD
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- 【解题报告】Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) D (简单计算几何)
- CF-Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2)-A-Vicious Keyboard
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- 【解题报告】Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
- Codeforces 797E Array Queries 分块思想
- Lucene入门
- 《精彩绝伦的CSS》——布局(二)居中块状框
- 1028. List Sorting (25)-PAT甲级
- 用python生成与调用cntk模型代码演示
- Codeforces Round #409 (rated, Div. 2, based on VK Cup 2017 Round 2) A -- D
- [leetCode刷题笔记]2017.04.16
- 我的编程之旅——开篇记
- 在北京大学,得罪方正者必成“问题人”
- 网站在浏览器头部的小图标如何弄
- 单向链表的C语言实现与基本操作
- 128.泡泡窗功能的实现
- 关于win7共享设置(转存旧文章)
- 1083. List Grades (25)-PAT甲级