A. Maximum in Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
An n × n table a is defined as follows:
The first row and the first column contain ones, that is: ai, 1 = a1, i = 1 for all i = 1, 2, ..., n.
Each of the remaining numbers in the table is equal to the sum of the number above it and the number to the left of it. In other words, the remaining elements are defined by the formula ai, j = ai - 1, j + ai, j - 1.
These conditions define all the values in the table.
You are given a number n. You need to determine the maximum value in the n × n table defined by the rules above.
Input
The only line of input contains a positive integer n (1 ≤ n ≤ 10) — the number of rows and columns of the table.
Output
Print a single line containing a positive integer m — the maximum value in the table.
Examples
Input
Copy
1
Output
Copy
1
Input
Copy
5
Output
Copy
70
Note
In the second test the rows of the table look as follows:
{1, 1, 1, 1, 1},
{1, 2, 3, 4, 5},
{1, 3, 6, 10, 15},
{1, 4, 10, 20, 35},
{1, 5, 15, 35, 70}.
AC:
#include<stdio.h>
int main()
{
int dp[11][11];
int i = 0,j = 0,k = 0;
for(i=1;i<=10;i++)
{
dp[i][1]=1;
dp[1][i]=1;
}
for(i=2;i<=10;i++)
{
for(j=2;j<=10;j++)
{
dp[i][j]=dp[i-1][j]+dp[i][j-1];
}
}
scanf("%d",&k);
printf("%d\n",dp[k][k]);
return 0;
}
To The Max
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15848 Accepted Submission(s): 7291
Problem Description
Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1 x 1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.
Input
The input consists of an N x N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N 2 integers separated by whitespace (spaces and newlines). These are the N 2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].
Output
Output the sum of the maximal sub-rectangle.
Sample Input
4
0 -2 -7 0 9 2 -6 2
-4 1 -4 1 -1
8 0 -2
Sample Output
15
Source
Greater New York 2001
AC:
#include<stdio.h>
#include<string.h>
int a[110][110] = {0};
int mmax=0,n = 0;
int dp(int hang,int row)
{
int mnum=0,curmax=0,j = 0;
for(j=1;j<=n;j++)
{
mnum+=(a[row+hang-1][j]-a[row-1][j]);
if(mnum>curmax)
curmax=mnum;
else if(mnum<0)
mnum=0;
}
if(curmax>mmax)
mmax=curmax;
return 1;
}
int main()
{
int i = 0,j = 0,temp = 0;
while(~scanf("%d",&n))
{
temp = 0;
mmax=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&temp);
a[i][j]=a[i-1][j]+temp;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i+1;j++)
dp(i,j);
}
printf("%d\n",mmax);
}
return 0;
}
How Many Tables
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 49416 Accepted Submission(s): 24569
Problem Description
Today is Ignatius’ birthday. He invites a lot of friends. Now it’s dinner time. Ignatius wants to know how many tables he needs at least. You have to notice that not all the friends know each other, and all the friends do not want to stay with strangers.
One important rule for this problem is that if I tell you A knows B, and B knows C, that means A, B, C know each other, so they can stay in one table.
For example: If I tell you A knows B, B knows C, and D knows E, so A, B, C can stay in one table, and D, E have to stay in the other one. So Ignatius needs 2 tables at least.
Input
The input starts with an integer T(1<=T<=25) which indicate the number of test cases. Then T test cases follow. Each test case starts with two integers N and M(1<=N,M<=1000). N indicates the number of friends, the friends are marked from 1 to N. Then M lines follow. Each line consists of two integers A and B(A!=B), that means friend A and friend B know each other. There will be a blank line between two cases.
Output
For each test case, just output how many tables Ignatius needs at least. Do NOT print any blanks.
Sample Input
2
5 3
1 2
2 3
4 5
5 1
2 5
Sample Output
2
4
Author
Ignatius.L
Source
杭电ACM省赛集训队选拔赛之热身赛
Recommend
Eddy
AC:
#include<stdio.h>
#include<string.h>
int dp[1010] = {0};
int findnode(int x)
{
if(x != dp[x])
dp[x] = findnode(dp[x]);
return dp[x];
}
int main()
{
int total = 0,n = 0,m =0,i = 0,a = 0,b = 0,ans = 0;
scanf("%d",&total);
while(total--)
{
n = 0,m =0,i = 0,a = 0,b = 0,ans = 0;
scanf("%d%d",&n,&m);
for(i=0;i<1010;i++)
dp[i]=i;
while(m--)
{
scanf("%d%d",&a,&b);
int dx=findnode(a);
int dy=findnode(b);
if(dx!=dy)
dp[dx]=dy;
}
for(i=1;i<=n;i++)
{
if(dp[i]==i)
ans++;
}
printf("%d\n",ans);
}
return 0;
}