Codeforces Round #343 (Div. 2) 解题报告
2016-02-25 20:33
309 查看
629A- Far Relative’s Birthday Cake
Door’s family is going celebrate Famil Doors’s birthday party. They love Famil Door so they are planning to make his birthday cake weird!The cake is a n × n square consisting of equal squares with side length 1. Each square is either empty or consists of a single chocolate. They bought the cake and randomly started to put the chocolates on the cake. The value of Famil Door’s happiness will be equal to the number of pairs of cells with chocolates that are in the same row or in the same column of the cake. Famil Doors’s family is wondering what is the amount of happiness of Famil going to be?
Please, note that any pair can be counted no more than once, as two different cells can’t share both the same row and the same column.
Input
In the first line of the input, you are given a single integer n (1 ≤ n ≤ 100) — the length of the side of the cake.
Then follow n lines, each containing n characters. Empty cells are denoted with ‘.’, while cells that contain chocolates are denoted by ‘C’.
Output
Print the value of Famil Door’s happiness, i.e. the number of pairs of chocolate pieces that share the same row or the same column.
input
3
.CC
C..
C.C
output
4
input
4
CC..
C..C
.CC.
.CC.
output
9
题目链接:cf-629A
题目思路:统计相邻两个C的对数
以下是代码:
#include <vector> #include <map> #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> using namespace std; string s[105]; int main(){ int n,ans = 0; cin >> n; for(int i = 0; i < n; i++) cin >> s[i]; for (int i = 0; i < n; i++) { int cnt = 0; for (int j = 0; j < n; j++) { if (s[i][j] == 'C') cnt++; } ans += cnt * (cnt - 1) / 2; } for (int j = 0; j < n; j++) { int cnt = 0; for (int i = 0; i < n; i++) { if (s[i][j] == 'C') cnt++; } ans += cnt * (cnt - 1) / 2; } cout << ans << endl; return 0; }
629B-Far Relative’s Problem
Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of them can come to the party in a specific range of days of the year from ai to bi. Of course, Famil Door wants to have as many friends celebrating together with him as possible.Far cars are as weird as Far Far Away citizens, so they can only carry two people of opposite gender, that is exactly one male and one female. However, Far is so far from here that no other transportation may be used to get to the party.
Famil Door should select some day of the year and invite some of his friends, such that they all are available at this moment and the number of male friends invited is equal to the number of female friends invited. Find the maximum number of friends that may present at the party.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 5000) — then number of Famil Door’s friends.
Then follow n lines, that describe the friends. Each line starts with a capital letter ‘F’ for female friends and with a capital letter ‘M’ for male friends. Then follow two integers ai and bi (1 ≤ ai ≤ bi ≤ 366), providing that the i-th friend can come to the party from day ai to day bi inclusive.
Output
Print the maximum number of people that may come to Famil Door’s party.
input
4
M 151 307
F 343 352
F 117 145
M 24 128
output
2
input
6
M 128 130
F 128 131
F 131 140
F 131 141
M 131 200
M 140 200
output
4
题目链接:cf-629B
题目大意:给出每个人的性别和空余时间。求人数最多的一天。该天需符合:
男女人数相同
都处于空闲时间
题目思路:直接暴力即可
以下是代码:
#include <vector> #include <map> #include <set> #include <algorithm> #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #include <string> #include <cstring> using namespace std; int a[1000],b[1000]; int main(){ int n; cin >> n; int ans = 0; for (int i = 0; i < n; i++) { char ch; int l,r; cin >> ch >> l >> r; for (int i = l; i <= r; i++) { if (ch == 'M') a[i]++; else b[i]++; } for (int i = 1; i <= 366; i++) { ans = max(ans,min(a[i],b[i])); } } cout << ans * 2 << endl; return 0; }
629C-Famil Door and Brackets
As Famil Door’s birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string consisted of round brackets since Famil Door loves string of brackets of length n more than any other strings!The sequence of round brackets is called valid if and only if:
the total number of opening brackets is equal to the total number of closing brackets;
for any prefix of the sequence, the number of opening brackets is greater or equal than the number of closing brackets.
Gabi bought a string s of length m (m ≤ n) and want to complete it to obtain a valid sequence of brackets of length n. He is going to pick some strings p and q consisting of round brackets and merge them in a string p + s + q, that is add the string p at the beginning of the string s and string q at the end of the string s.
Now he wonders, how many pairs of strings p and q exists, such that the string p + s + q is a valid sequence of round brackets. As this number may be pretty large, he wants to calculate it modulo 109 + 7.
Input
First line contains n and m (1 ≤ m ≤ n ≤ 100 000, n - m ≤ 2000) — the desired length of the string and the length of the string bought by Gabi, respectively.
The second line contains string s of length m consisting of characters ‘(’ and ‘)’ only.
Output
Print the number of pairs of string p and q such that p + s + q is a valid sequence of round brackets modulo 109 + 7.
input
4 1
(
output
4
input
4 4
(())
output
1
input
4 3
(((
output
0
题目链接:cf-629C
题目大意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000
这个长度为n的字符串要求有两个性质:
就是任意前缀,左括号数量大于右括号数量
字符串中左括号的数量等于右括号
现在让你可以在长度为m的原串前加一个括号串p,在原串后加一个括号串q 最后p+m+q=n
问有多少种组合p,q能得到目标串
题目思路:
定义dp[i][j],为前缀长为i,且左括号数量-右括号数量=j的串有多少个
算出s段左括号与右括号的差值记为cnt,记录p段至少需要的左括号数目为need。
然后枚举p的长度和平衡值 对于长度i, 当-d<=j时,p可以加到前面
然后当p确定后,q的长度也确定,因为最终 左=右 ,所以q 的(右-左)的代价也知道了
假设当前是i,平衡度是j,所以只要将dp[i][j]*dp[n-m-i][j+cnt]加到答案就行了
注意:dp[i][j]代表前缀i,平衡度为j的方案数, dp[n-m-i][j+cnt]为后缀n-m-i,平衡度为-(j+cnt)的方案数,是对称的,很重要
即:dp[n - m - i][j + cnt] = dp[n - m - i][-(j + cnt)] // dp第二维不可为负数
参考博客:here
以下是代码:
#include <bits/stdc++.h> #define mst(a) memset(a,0,sizeof (a)) #define FOR(i,n) for (int i = 0; i < n; i++) #define INF 1e9 #define eps 1e-10 #define mod 1000000007 using namespace std; typedef long long ll; string s; ll dp[2005][2005]; //前缀长为i,且左括号数量-右括号数量=j的串有多少个 int n,m; void solve() //预处理 { dp[0][0] = 1; for (int i = 1; i <= n - m; i++) { for (int j = 0; j <= i; j++) { if (j == 0) dp[i][j] = dp[i - 1][j + 1]; else dp[i][j] = (dp[i - 1][j + 1] + dp[i - 1][j - 1]) % mod; } } } int main(){ cin >> n >> m; cin >> s; s = " " + s; solve(); int need = INF,cnt = 0; //cnt : s段左括号与右括号的差值记为cnt //need : 记录p段至少需要的左括号数目 for (int i = 1; i <= m; i++) { if (s[i] == '(') cnt++; else cnt--; need = min(cnt,need); } long long ans = 0; for (int i = 0; i <= n - m; i++) { for (int j = 0; j <= i; j++) { if (j >= -need && cnt + j <= n - m - i) { ans = (ans + dp[i][j] * dp[n - m - i][cnt + j]) % mod; } } } cout << ans << endl; return 0; }
629D - Babaei and Birthday Cake
As you know, every birthday party has a cake! This time, Babaei is going to prepare the very special birthday party’s cake.Simple cake is a cylinder of some radius and height. The volume of the simple cake is equal to the volume of corresponding cylinder. Babaei has n simple cakes and he is going to make a special cake placing some cylinders on each other.
However, there are some additional culinary restrictions. The cakes are numbered in such a way that the cake number i can be placed only on the table or on some cake number j where j < i. Moreover, in order to impress friends Babaei will put the cake i on top of the cake j only if the volume of the cake i is strictly greater than the volume of the cake j.
Babaei wants to prepare a birthday cake that has a maximum possible total volume. Help him find this value.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of simple cakes Babaei has.
Each of the following n lines contains two integers ri and hi (1 ≤ ri, hi ≤ 10 000), giving the radius and height of the i-th cake.
Output
Print the maximum volume of the cake that Babaei can make. 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 .
input
2
100 30
40 10
output
942477.796077000
input
4
1 1
9 7
1 4
10 7
output
3983.539484752
题目链接:cf-629D
题目大意:有n个圆柱形的蛋糕,已知它们的底面半径r和侧面高h。把这n个蛋糕叠起来,第i个蛋糕放在第j个蛋糕上面(1<=j < i)的条件是第i个蛋糕的体积必须大于第j个蛋糕的体积。求叠放起来的蛋糕体积最大能是多少?
题目思路:类似于nlogn的最大上升子序列和。
参考博客:here
以下是代码:
#include <bits/stdc++.h> #define mst(a) memset(a,0,sizeof (a)) #define FOR(i,n) for (int i = 0; i < n; i++) #define INF 1e9 #define eps 1e-10 #define PI acos(-1.0) using namespace std; typedef long long ll; map <ll,ll> mp; //记录每一个以i结尾的最大和, int main(){ int n; cin >> n; ll ans = 0; mp[0] = 0; for (int i = 0; i < n; i++) { ll r,h; cin >> r >> h; ll v = r * r * h; map<ll,ll>::iterator it = mp.lower_bound(v); //对于每个蛋糕i先找到前面顺序第一个大于等于其体积的蛋糕s, it--; ll temp = mp[v] = v + it->second; //然后加入蛋糕s前面一个蛋糕s-1所在符合条件的且和最大的序列, ans = max(ans,temp); //删去不需要的值 it = mp.find(v); it++; while(it != mp.end() && (it -> second < temp)) mp.erase(it++); //删去蛋糕s及其后面那些叠放和都不大于sum的连续区间。这样做是保证留下的最底层一个蛋糕所在序列的和一定是最大值 (这里并不是很懂) } printf("%.9f\n",ans * PI); return 0; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations
- Codeforces Round #236 (Div. 2)------A,B
- codeforces 257 div2 B
- Codeforces Gym100571A Cursed Query
- Codeforces Gym100342E Minima