Codeforces Round #343 (Div. 2) A B C D E

A. Far Relative’s Birthday Cake
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
input
3
.CC
C..
C.C
output
4
input
4
CC..
C..C
.CC.
.CC.
output
9
Note

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. (1, 2) and (1, 3)
  2. (3, 1) and (3, 3)
Pieces that share the same column are:
  1. (2, 1) and (3, 1)
  2. (1, 3) and (3, 3)

题目大意:

给出一个二维地图,求在两个C在同一行或者同一列中出现的对数。


解题思路:

用两个数组分别记录每行、每列C的出现次数,然后用组合公式求在每行、每列中选任意2个C的组合数,最后全部加起来即可。


#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 100 + 5;
int n;
char G[MAXN][MAXN];
int x[MAXN], y[MAXN];

int main() {
#ifdef NIGHT_13
	freopen("in.txt", "r", stdin);
#endif
	while (scanf("%d%*c", &n) != EOF) {
		memset(x, 0, sizeof(x));
		memset(y, 0, sizeof(y));
		for (int i = 1; i <= n; ++i) {
			for (int j = 1; j <= n; ++j) {
				G[i][j] = getchar();
				if (G[i][j] == 'C') {
					++x[i];
					++y[j];
				}
			}
			getchar();
		}

		int ans = 0;

		for (int i = 1; i <= n; ++i) {
			ans += x[i] * (x[i] - 1);
			ans += y[i] * (y[i] - 1);
		}
		printf("%d\n", ans / 2);
	}
	return 0;
}





B. Far Relative’s Problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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
Note

In the first sample, friends 3 and 4 can come on any day in range [117, 128].

In the second sample, friends with indices 345 and 6 can come on day 140.


题目大意:

有n个人,给出每个人的性别,空闲时间段;

现要选出一个时间段,选若干个在这个时间段空闲的人参加聚会,参加聚会的人男女人数要相同。

问最多可以选出多少人在某一时间段参加聚会。


解题思路:

区间重叠问题,稍微加了点变化。

典型思路就是用数组记录,起点+1,终点-1,最后从左扫到右,把之前的记录累加起来,更新最大和即可。

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN = 366 + 5;
int n;
int x[MAXN], y[MAXN];
int x2[MAXN], y2[MAXN];

int main() {
#ifdef NIGHT_13
	freopen("in.txt", "r", stdin);
#endif
	while (scanf("%d%*c", &n) != EOF) {
		memset(x, 0, sizeof(x));
		memset(y, 0, sizeof(y));

		memset(x2, 0, sizeof(x2));
		memset(y2, 0, sizeof(y2));

		for (int i = 1; i <= n; ++i) {
			char ch = getchar();
			int a, b;
			scanf("%d%d%*c", &a, &b);
			if (ch == 'F') { ++x[a], --x2[b]; }
			else { ++y[a], --y2[b]; }
		}

		int a = 0, b = 0, ans = 0;
		for (int i = 1; i < MAXN; ++i) {
			a += x[i];
			b += y[i];
			ans = max(ans, min(a, b));
			a += x2[i];
			b += y2[i];
		}
		printf("%d\n", ans*2);
	}
	return 0;
}



C. Famil Door and Brackets
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  1. the total number of opening brackets is equal to the total number of closing brackets;
  2. 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.

Examples
input
4 1
(
output
4
input
4 4
(())
output
1
input
4 3
(((
output
0
Note

In the first sample there are four different valid pairs:

  1. p = "(", q = "))"
  2. p = "()", q = ")"
  3. p = "", q = "())"
  4. 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.


题目大意:

给出括号串S,要求分别S左右添加括号,最后能让括号全部匹配的可能总数。

解题思路:

先预处理出所有的满足条件2(左括号大于等于右括号)的可能组合次数,用dp[i][j](长度为i的串,左括号比右括号多j个)记录下来。

然后结合S串筛出满足题目两个条件的记录,加起来。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int MAXN =  1E5 + 5;
const int MOD = 1E9 + 7;
int n, m;
char G[MAXN];
int dp[2005][2005];

void Init() {
	dp[0][0] = 1;
	for (int i = 1; i < 2005; ++i) {
		dp[i][0] = dp[i - 1][1];
		for (int j = 1; j <= i; ++j) {
			dp[i][j] = ((LL)dp[i - 1][j - 1] + dp[i - 1][j + 1]) % MOD;
		}
	}
}

int main() {
#ifdef NIGHT_13
	freopen("in.txt", "r", stdin);
#endif
	Init();
	while (scanf("%d%d%s", &n, &m, G) != EOF) {

		int preMin = 0, pre = 0;
		for (int i = 0; i < m; ++i) {
			G[i] == '(' ? ++pre : --pre;
			preMin = min(preMin, pre);
		}

		int sub = n - m, ans = 0;
		for (int i = 0; i <= sub; ++i) {
			for (int j = 0; j <= i; ++j) {
				if (j + preMin >= 0 && j + pre <= sub - i) {
					ans = ((LL)dp[i][j] * dp[sub - i][j + pre] + ans) % MOD;
				}
			}
		}
		
		printf("%d\n", ans);
	}	
	return 0;
}



D. Babaei and Birthday Cake
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 .

Examples
input
2
100 30
40 10
output
942477.796077000
input
4
1 1
9 7
1 4
10 7
output
3983.539484752
Note

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 12 and 4.


题目大意:

给出n个蛋糕的半径和高度。

现按蛋糕给出的顺序,依次选出几个蛋糕叠成一个,要求选出的蛋糕中,v[i] < v[j],i < j时。

问最大可以叠出多大体积的蛋糕。


解题思路:

典型的求最大上升子序列和问题,很容易想到状态转移方程:dp(i) = max(dp(i)) + v(j) { i < j && v(i) < v(j) }

很容易可以写出n^2的算法,不过显然会超时。

考虑到map是按key值顺序排列的,所以可以在logn的时间内找出找出合法的dp(i),所以用map来dp,key值就是v(i), value值是dp(i)

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
using namespace std;

typedef long long LL;
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5 + 10;
const double PI = 3.141592653589793;
int n;

struct Node {
    int r, h;
} node[MAXN];

double dp[MAXN];
map<LL, LL> pvSv;

int main() {
#ifdef NIGHT_13
    freopen("in.txt", "r", stdin);
#endif
    while (scanf("%d", &n) != EOF) {
        pvSv.clear();
        pvSv[0] = 0;

        for (int i = 1; i <= n; ++i) {
            scanf("%d%d", &node[i].r, &node[i].h);
            LL v = (LL)node[i].r * node[i].r * node[i].h;

            auto st = pvSv.lower_bound(v);
            auto ed = st;

            LL last = (--st)->second + v;
            for (; ed != pvSv.end() && ed->second <= last; ++ed) {}

            pvSv.erase(++st, ed);
            pvSv[v] = last;
        }

        printf("%.9f\n", (--pvSv.end())->second * PI);
    }
    return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值