http://codeforces.com/contest/629/problem/A
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.
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'.
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.
3 .CC C.. C.C
4
4 CC.. C..C .CC. .CC.
9
If we number rows from top to bottom and columns from left to right, then, pieces that share the same row in the first sample are:
- (1, 2) and (1, 3)
- (3, 1) and (3, 3)
- (2, 1) and (3, 1)
- (1, 3) and (3, 3)
题目大意:
给一个地图,每行每列为空或者有C,然后如果一行的C>=2,那么就用排列组合的方法计算就可以了。
#include
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int atlas[100 + 5][100 + 5];
int main(){
int n;
scanf("%d", &n);
char ch[105];
for (int i = 0; i < n; i ++){
scanf("%s", ch);
for (int j = 0; ch[j] != '\0'; j++){
if (ch[j] == '.') atlas[i][j] = 0;
else atlas[i][j] = 1;
}
}
ll res = 0;
for (int i = 0; i < n; i++){
int cnt = 0;
for (int j = 0; j < n; j++){
if (atlas[i][j] == 1) cnt++;
}
res += cnt * (cnt - 1) / 2;
}
for (int i = 0; i < n; i++){
int cnt = 0;
for (int j = 0; j < n; j++){
if (atlas[j][i] == 1) cnt++;
}
res += cnt * (cnt - 1) / 2;
}
printf("%d\n", res);
return 0;
}
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.
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.
Print the maximum number of people that may come to Famil Door's party.
4 M 151 307 F 343 352 F 117 145 M 24 128
2
6 M 128 130 F 128 131 F 131 140 F 131 141 M 131 200 M 140 200
4
In the first sample, friends 3 and 4 can come on any day in range [117, 128].
In the second sample, friends with indices 3, 4, 5 and 6 can come on day 140.
题目大意:
哪个区间的值最大.
思路:
其实只要暴力就可以了。。。然而我还脑残的离散化了。。。
然后得出来以后res再*2就好了
#include
using namespace std;
typedef long long ll;
typedef pair
P;
const int inf = 0x3f3f3f3f;
const int maxn = 5000 + 5;
pair
a[maxn];
int m[maxn];
int fm[maxn];
int main(){
int n;
scanf("%d", &n);
char ch[5];
vector
v;
for (int i = 0; i < n; i++){
int f, l, r;
scanf("%s%d%d", ch, &l, &r);
if (ch[0] == 'F') f = 0;
else f = 1;
a[i] = make_pair(f, make_pair(l, r));
v.push_back(l);
v.push_back(r);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i = 0; i < n; i++){
a[i].second.first = lower_bound(v.begin(), v.end(), a[i].second.first) - v.begin();
a[i].second.second = lower_bound(v.begin(), v.end(), a[i].second.second) - v.begin();
}
int res = 0;
for (int i = 0; i < n; i++){
int l = a[i].second.first;
int r = a[i].second.second;
int flag = a[i].first;
for (int j = l; j <= r; j++){
if (flag == 1){
m[j]++;
//printf("man = %d\n", m[j]);
}
else {
fm[j]++;
//printf("female = %d\n", fm[j]);
}
}
}
for (int i = 0; i < 400; i++){
res = max(res, min(m[i], fm[i]));
}
printf("%d\n", res * 2);
return 0;
}
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.
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.
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.
4 1 (
4
4 4 (())
1
4 3 (((
0
In the first sample there are four different valid pairs:
- p = "(", q = "))"
- p = "()", q = ")"
- p = "", q = "())"
- p = "", q = ")()"
In the second sample the only way to obtain a desired string is choose empty p and q.
In the third sample there is no way to get a valid sequence of brackets.
题目大意:
一个字符串长度为n,先给一个长度为m的中间字符串s,然后问,有几组由p+s+q组成的字符串能满足一下条件
①字符串的前缀中(多余)
②最后整个字符串(和)的数量相同
思路:
表示这道题目前缀卡了我半天(因为不明白是什么意思,所以才不知道minx在for循环中的作用)
对于这道题目,首先我们先说明dp[i][j],i是目前的p的字符的个数,然后j是目前‘(’或者‘)’的个数 备注:因为‘(’和‘)’的个数是相对的,也就是说对于dp来说是可以相互公用的。所以就可以用dp[i][j] = dp[i-1][j-1]+dp[i-1][j+1]来进行计算了。
最后只要暴力枚举一下就好了,枚举的是p的长度和p中j的个数,j表示的是(比)多多少,然后通过关系就可以知道)比(多多少了。
#include
using namespace std;
typedef long long ll;
const int maxn = 2000 + 5;
const int mod = 1000000000 + 7;
const int inf = 0x3f3f3f3f;
int n, m;
ll dp[maxn][maxn];
char ch[100000 + 5];
int main(){
dp[0][0] = 1;
for (int i = 1; i <= maxn-1; i++){
dp[i][0] = dp[i - 1][1];
for (int j = 1; j <= i; j++){
dp[i][j] = (dp[i][j] + dp[i-1][j-1] + dp[i-1][j+1]) % mod;
}
}
scanf("%d%d", &n, &m);
scanf("%s", ch);
ll minx = inf;
ll sum = 0;
for (int i = 0; ch[i] != '\0'; i++){
if (ch[i] == '(') sum++;
else sum--;
minx = min(minx, sum);
}
ll res = 0;
for (int i = 0; i <= n - m; i++){
for (int j = 0; j <= n - m; j++){
if (j + minx < 0) continue;
if (j + sum > n - m - i) break;
res = (res + dp[i][j] * dp[n-m-i][j+sum]) % mod;
}
}
printf("%I64d\n", res);
return 0;
}
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.
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.
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 .
2 100 30 40 10
942477.796077000
4 1 1 9 7 1 4 10 7
3983.539484752
In first sample, the optimal way is to choose the cake number 1.
In second sample, the way to get the maximum volume is to use cakes with indices 1, 2 and 4.
首先说一下这道题目的心情。。。表示这道题目很早就写出来了,然后错在了一个int和longlong的精度范围,然后整整一个下午!五个小时的时间就报废掉了!唉,不说了,都是泪啊。。。说点有用的吧(这次绝对印象深刻了)
题目大意:
给一个圆柱,然后圆柱可以往上面叠,但是叠是有条件的。
①i<j,即越往上面,在数组中的编号越大
②v[i]<v[j],即越往上面体积越大。
问,这个圆柱最大的体积是多少。
思路:
看着很像最长上升子序列,但是并不是,因为就算你是最长的,第二长的序列的体积也有可能比你大(关键是最长上升子序列的重点在于最长)。
然后我们考虑一下dp,首先,dp要开一个10W * 10W的数组,这就MLE了,其次如果要遍历所有方案,那就最坏情况是O(n*n),也不可能。于是我们就考虑到了线段树来优化这个dp。
首先,我们用一个数组ind和d分别储存这些数组,然后ind是保存原来的体积数据(体积我们就暂时用r*r*h来表示),然后我们将d数组排序,和去重即可(为的是满足a[i]<a[j]),然后我们开始枚举ind数组,然后一边往线段树里面放,一边更新维护,假设当前a[i]所在的位置是d中的pos。那么,在pos-1中的所有的数据都满足比目前位置的pos小,然后我们就只需要把之前的pos-1的这些线段位置中的最大值找出来,然后加上ind[i]就可以了。
query访问的是区间,update访问的是点,但是要维护整个所访问到的地方
#include
using namespace std;
typedef long long ll;
const int maxn = 100000 + 20;
const double pi = acos(-1);
int n;
struct point{
int l, r;
ll val;
point(int l = 0, int r = 0, ll val = 0):l(l), r(r), val(val) {}
};
point tree[maxn << 3];
ll d[maxn];
ll ind[maxn];
ll mx;
void buildtree(int o, int l, int r){
if (l == r){
tree[o] = point (l, r, 0);
return ;
}
int mid = (l + r) / 2;
if (mid >= l){
buildtree(o << 1, l, mid);
}
if (mid < r){
buildtree(o << 1 | 1, mid + 1, r);
}
tree[o] = point(l, r, 0);
}
ll query(int o, int pl, int pr){
if (tree[o].l >= pl && tree[o].r <= pr){
return tree[o].val;
}
int mid = tree[o].l + (tree[o].r - tree[o].l) / 2;
if (mid >= pl){
mx = max(query(o << 1, pl, pr), mx);
}
if (mid < pr){
mx = max(query(o << 1 | 1, pl, pr), mx);
}
return 0;
}
void pushup(int o){
tree[o].val = max(tree[o << 1].val, tree[o << 1 | 1].val);
}
void update(int o, int pos, ll res){
if (tree[o].l == tree[o].r){
tree[o].val = max(tree[o].val, res);
return ;
}
int mid = tree[o].l + (tree[o].r - tree[o].l) / 2;
if (mid >= pos){
update(o << 1, pos, res);
}
if (mid < pos){
update(o << 1 | 1, pos, res);
}
pushup(o);
}
int main(){
scanf("%d", &n);
buildtree(1, 0, n);
int r, h;
for (int i = 0; i < n; i++){
scanf("%d%d", &r, &h);
ll tmp = (ll)r * r * h;
ind[i] = d[i] = tmp;
}
sort(d, d + n);
int lo = unique(d, d + n) - d;
ll ans = 0, res = 0;
for (int i = 0; i < n; i++){
int pos = lower_bound(d, d + lo, ind[i]) - d + 1;
mx = 0;
query(1, 0, pos - 1);
res = mx + ind[i];
update(1, pos, res);
ans = max(res, ans);
}
printf("%f\n", pi * ans);
return 0;
}