UVA - 12034
Description
Tamim and Lina, two of the biggest mega minds of Bangladesh went to a far country. They ate, coded and wandered around, even in their holidays. They passed several months in this way. But everything has an end. A holy person, e-Moti-jicame into their life. e-Moti-ji took them to derby (horse racing). e-Moti-ji enjoyed the race, but as usual Tamim and Lina did their as usual task instead of passing some romantic moments. They were thinking- in how many ways a race can finish! Who knows, maybe this is their romance!
In a race there are n horses. You have to output the number of ways the race can finish. Note that, more than one horse may get the same position. For example, 2 horses can finish in 3 ways.
Input
Input starts with an integer T (1000), denoting the number of test cases. Each case starts with a line containing an integer n (1n1000).
Output
For each case, print the case number and the number of ways the race can finish. The result can be very large, print the result modulo 10056.
Sample Input
3 1 2 3
Sample Output
Case 1: 1 Case 2: 3 Case 3: 13
求n匹马可以并行的前提的排列情况
c[p][q]=c[p][p-q]=(c[p-1][q-1]+c[p-1][q])%mod;(p为马数,q为排名数)
代码:
#include
const int mod=10056;
int c[1005][1005];int f[1005];
int main()
{
int t,n,u=0;
int p,q;
c[1][0]=c[1][1]=1;
for(p=2;p<1005;p++)
{
c[p][0]=1;
for(q=1;q<=p/2;q++)
{
c[p][q]=c[p][p-q]=(c[p-1][q-1]+c[p-1][q])%mod;
}
}
for(p=1;p<1005;p++)
{
f[p]=1;
for(q=1;q
SCU - 1114
下图是个数字三角,请编写一个程序计算从顶部至底部某处一条路径,使得该路径所经过的数字总和最大。
7
3 8
8 1 0
2 7 4 4
1. 每一步可沿左斜线向下或右斜线向下走;
2. 1<=三角形行数<=100
3. 三角形中的数字为整数 0,1,……,99。
4. 如果有多种情况结果都最大,任意输出一种即可。
输入:
第一行一个整数N,代表三角形的行数。
接下来N行,描述了一个数字三角。
输出:
第一行一个整数,代表路径所经过底数字总和。
第二行N个数,代表所经过的数字。
样例:
输入:
4
7
3 8
8 1 0
2 7 4 4
输出:
25
7 3 8 7
水题了,直接从下向上DP就好了,注意一下三角形的储存。推荐
for(i=1;i<=n;i++) { for(j=n-i+1;j<=n+i-1;j+=2) { scanf("%d",&arr[i][j]); } }
当然也有更好的办法。不过我一下也没想到。。。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
POJ - 1458
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ..., ik > of indices of X such that for all j = 1,2,...,k, x ij = zj. For example, Z = < a, b, f, c > is a subsequence of X = < a, b, c, f, b, c > with index sequence < 1, 2, 4, 6 >. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
Input
The program input is from the std input. Each data set in the input contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct.
Output
For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab programming contest abcd mnp
Sample Output
4 2 0
最长公共序列。不解释
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
POJ - 2533
A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let the subsequence of the given numeric sequence ( a1, a2, ..., aN) be any sequence ( ai1, ai2, ..., aiK), where 1 <= i1 < i2 < ... < iK <= N. For example, sequence (1, 7, 3, 5, 9, 4, 8) has ordered subsequences, e. g., (1, 7), (3, 4, 8) and many others. All longest ordered subsequences are of length 4, e. g., (1, 3, 5, 8).
Your program, when given the numeric sequence, must find the length of its longest ordered subsequence.
Input
The first line of input file contains the length of sequence N. The second line contains the elements of sequence - N integers in the range from 0 to 10000 each, separated by spaces. 1 <= N <= 1000
Output
Output file must contain a single integer - the length of the longest ordered subsequence of the given sequence.
Sample Input
7 1 7 3 5 9 4 8
Sample Output
4
最长上升子序列
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
CodeForces - 5C
This is yet another problem dealing with regular bracket sequences.
We should remind you that a bracket sequence is called regular, if by inserting «+» and «1» into it we can get a correct mathematical expression. For example, sequences «(())()», «()» and «(()(()))» are regular, while «)(», «(()» and «(()))(» are not.
You are given a string of «(» and «)» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
Input
The first line of the input file contains a non-empty string, consisting of «(» and «)» characters. Its length does not exceed 106.
Output
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing "0 1".
Examples
Input
)((())))(()())
Output
6 2
Input
))(
Output
0 1
从左到右进行一次搜索,找出所有符合条件的右括号,再从右向左扫一遍,找出所有可以符合条件的右括号。再从头到尾扫一遍找出连续的最长的符合条件的括号。
一开始想劈叉了。。WA了7次。。。在CF上过到62.。。都快绝望了
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
HDU - 1520
There is going to be a party to celebrate the 80-th Anniversary of the Ural State University. The University has a hierarchical structure of employees. It means that the supervisor relation forms a tree rooted at the rector V. E. Tretyakov. In order to make the party funny for every one, the rector does not want both an employee and his or her immediate supervisor to be present. The personnel office has evaluated conviviality of each employee, so everyone has some number (rating) attached to him or her. Your task is to make a list of guests with the maximal possible sum of guests' conviviality ratings.
Input
Employees are numbered from 1 to N. A first line of input contains a number N. 1 <= N <= 6 000. Each of the subsequent N lines contains the conviviality rating of the corresponding employee. Conviviality rating is an integer number in a range from -128 to 127. After that go T lines that describe a supervisor relation tree. Each line of the tree specification has the form:
L K
It means that the K-th employee is an immediate supervisor of the L-th employee. Input is ended with the line
0 0
Output
Output should contain the maximal sum of guests' ratings.
Sample Input
7 1 1 1 1 1 1 1 1 3 2 3 6 4 7 4 4 5 3 5 0 0
Sample Output
5
求最大独立集。树形DP模板题,详见我另一篇博客:https://blog.csdn.net/baiyifeifei/article/details/81394549
一开始也是错了一万发。濒临绝望
#include
#include
#include
#include
using namespace std;
const int size=6005;
int edge[size];
int dp[size][2];
int unroot[size];
int v[size];
vector son[size];
int flag[size];
void dfs(int node)
{
flag[node]=1;
for(int i=0;i>n)
{
memset(dp,0,sizeof(dp));
memset(unroot,0,sizeof(unroot));
memset(edge,0,sizeof(edge));
memset(flag,0,sizeof(flag));
int i;
for(i=1;i<=n;i++)
{
scanf("%d",&v[i]);//读入每个人的快乐值
son[i].clear();
}
int a,b;
while(~scanf("%d%d",&a,&b)&&a)
{
son[b].push_back(a);//a的父节点是b
unroot[a]=1;//假如他有过作子节点的经历,则一定不是根节点
}
int root;
for(i=1;i<=n;i++)
{
if(!unroot[i])
{
root=i;
}
dp[i][1]=v[i];
dp[i][0]=0;
}
dfs(root);
int maxn=max(dp[root][0],dp[root][1]);
cout<
POJ - 1631
'Oh no, they've done it again', cries the chief designer at the Waferland chip factory. Once more the routing designers have screwed up completely, making the signals on the chip connecting the ports of two functional blocks cross each other all over the place. At this late stage of the process, it is too expensive to redo the routing. Instead, the engineers have to bridge the signals, using the third dimension, so that no two signals cross. However, bridging is a complicated operation, and thus it is desirable to bridge as few signals as possible. The call for a computer program that finds the maximum number of signals which may be connected on the silicon surface without crossing each other, is imminent. Bearing in mind that there may be thousands of signal ports at the boundary of a functional block, the problem asks quite a lot of the programmer. Are you up to the task?
A typical situation is schematically depicted in figure 1. The ports of the two functional blocks are numbered from 1 to p, from top to bottom. The signal mapping is described by a permutation of the numbers 1 to p in the form of a list of p unique numbers in the range 1 to p, in which the i:th number specifies which port on the right side should be connected to the i:th port on the left side.Two signals cross if and only if the straight lines connecting the two ports of each pair do.
Input
On the first line of the input, there is a single positive integer n, telling the number of test scenarios to follow. Each test scenario begins with a line containing a single positive integer p < 40000, the number of ports on the two functional blocks. Then follow p lines, describing the signal mapping:On the i:th line is the port number of the block on the right side which should be connected to the i:th port of the block on the left side.
Output
For each test scenario, output one line containing the maximum number of signals which may be routed on the silicon surface without crossing each other.
Sample Input
4 6 4 2 6 3 1 5 10 2 3 4 5 6 7 8 9 10 1 8 8 7 6 5 4 3 2 1 9 5 8 9 2 3 1 7 4 6
Sample Output
3 9 1 4
题目花里胡哨的,其实想一下就会发现。这不就是求最大上升子序列吗?
把前面那个题目随便改改粘贴过来就好了:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
HDU - 1114
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!
Input
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, Pand W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.
Output
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".
Sample Input
3 10 110 2 1 1 30 50 10 110 2 1 1 50 30 1 6 2 10 3 20 4
Sample Output
The minimum amount of money in the piggy-bank is 60. The minimum amount of money in the piggy-bank is 100. This is impossible.
完全背包。需要注意的是要求恰好装满,这里引用背包九讲中的内容(背包九讲真的是很棒的材料,值得一遍又一遍地看):
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
HDU - 2602
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 2 31).
Sample Input
1 5 10 1 2 3 4 5 5 4 3 2 1
Sample Output
14
01背包模板题,不解释
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
POJ - 1949
Farmer John's family pitches in with the chores during milking, doing all the chores as quickly as possible. At FJ's house, some chores cannot be started until others have been completed, e.g., it is impossible to wash the cows until they are in the stalls.
Farmer John has a list of N (3 <= N <= 10,000) chores that must be completed. Each chore requires an integer time (1 <= length of time <= 100) to complete and there may be other chores that must be completed before this chore is started. We will call these prerequisite chores. At least one chore has no prerequisite: the very first one, number 1. Farmer John's list of chores is nicely ordered, and chore K (K > 1) can have only chores 1,.K-1 as prerequisites. Write a program that reads a list of chores from 1 to N with associated times and all perquisite chores. Now calculate the shortest time it will take to complete all N chores. Of course, chores that do not depend on each other can be performed simultaneously.
Input
* Line 1: One integer, N
* Lines 2..N+1: N lines, each with several space-separated integers. Line 2 contains chore 1; line 3 contains chore 2, and so on. Each line contains the length of time to complete the chore, the number of the prerequisites, Pi, (0 <= Pi <= 100), and the Pi prerequisites (range 1..N, of course).
Output
A single line with an integer which is the least amount of time required to perform all the chores.
Sample Input
7 5 0 1 1 1 3 1 2 6 1 1 1 2 2 4 8 2 2 4 4 3 3 5 6
Sample Output
23
Hint
[Here is one task schedule: Chore 1 starts at time 0, ends at time 5. Chore 2 starts at time 5, ends at time 6. Chore 3 starts at time 6, ends at time 9. Chore 4 starts at time 5, ends at time 11. Chore 5 starts at time 11, ends at time 12. Chore 6 starts at time 11, ends at time 19. Chore 7 starts at time 19, ends at time 23. ]
直接有这个递推式就好了dp[i]=max(dp[i],dp[no]+tim);
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
DP这个东西嘛。推出来就简单了,推不出来就GG。还是靠多练习吧