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;
}




發佈了96 篇原創文章 · 獲贊 8 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章