CF 629 A组合 B暴力 Cdp D线段树优化DP

来源:互联网 发布:唇模招聘淘宝 编辑:程序博客网 时间:2024/05/15 07:52

http://codeforces.com/contest/629/problem/A

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.CCC..C.C
output
4
input
4CC..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,那么就用排列组合的方法计算就可以了。


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
4M 151 307F 343 352F 117 145M 24 128
output
2
input
6M 128 130F 128 131F 131 140F 131 141M 131 200M 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.


题目大意:

哪个区间的值最大.

思路:

其实只要暴力就可以了。。。然而我还脑残的离散化了。。。

然后得出来以后res再*2就好了


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.


题目大意:

一个字符串长度为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表示的是(比)多多少,然后通过关系就可以知道)比(多多少了。



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
2100 3040 10
output
942477.796077000
input
41 19 71 410 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.



首先说一下这道题目的心情。。。表示这道题目很早就写出来了,然后错在了一个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访问的是点,但是要维护整个所访问到的地方







0 0
原创粉丝点击