第六届浙江省高校程序设计竞赛

ZOJ-3202

题目http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3202

分析:找出最大值的下标跟次小值

#include
#include
using namespace std;
const int maxn=1007;
int b[maxn];
struct node{
	int id,num;
}a[maxn];
int cmp(node a1,node b1){
	return a1.num>b1.num;
}
int main(){
	int n;
	int T;
	scanf("%d",&T);
	while(T--){
	scanf("%d",&n);
	for(int i=0;i
ZOJ-3203

题目http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3203

第六届浙江省高校程序设计竞赛_第1张图片

分析:数学很重要,一直再用三角形相似,加上极端情况,忽略了关键一步,可以用均值不等式啊,痛~

详细解答--->>>

题意:

首先给出T组数据,每组给出灯离地面高度H,人的身高h,灯跟墙的距离D,人站在不同位置,影子的长度都不一样,求出最长的影子的长度。

题解:

计算出来的这条式子,可以直接进行三分,求出极值点,但是同样,我们可以通过这条公式用O(1)的方法求出来

三分写法:求最值

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const double esp=1e-8;
double H,h,D;
double cal(double x)
{
	return D-x+H-(H-h)*D/x;
}
double three_devide(double l,double r)
{
	double left=l,right=r,mid,midmid;
	while (left+esp=cal(midmid))
			right=midmid;
		else
			left=mid;
	}
	return cal(right);
}
int main()
{
    int T;
	scanf("%d",&T);
	while (T--)
    {
        scanf("%lf%lf%lf",&H,&h,&D);
        double l=D-h*D/H,r=D;
        double ans=three_devide(D-h*D/H,r);
        printf("%.3f\n",ans);
    }
	return 0;
}
公式法:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const double esp=1e-8;
double H,h,D;
int main()
{
	int T;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%lf%lf%lf",&H,&h,&D);
		double mx=D-h*D/H;
		if (D>=sqrt((H-h)*D) && mx<=sqrt((H-h)*D))
            mx=D+H-2*sqrt((H-h)*D);
		else if (sqrt((H-h)*D)
#include 
int main(){
    int T;
	scanf("%d",&T);
    while(T--){
        double H,h,D;double ans=0;
        scanf("%lf%lf%lf",&H,&h,&D);
        for(double i=0;i<=h;i+=0.0005){
            double w=i+D*(h-i)/(H-i);
            if(w>ans)
                ans=w;
        }
        printf("%.3lf\n",ans);
    }
    return 0;
}
ZOJ3204

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3204

题目大意:题目大意:有几台电脑,怎么用最少的费用把他

           们连接起来

   

解题思路:最小生成树问题。先将边从小到大排序,依次把

           边两端的点用并查集合并。需要注意的是,题目中

           给出的矩阵上三角和下三角是相同的,即只需处理

           一半就可以啦。

          克鲁斯卡尔算法

#include
#include
#include
#include
#include
using namespace std;
struct node{
	int a,b;
	int n;
}edge[10005];
struct rea{
	int a,b;
}relans[500];
int father[105];
int cmp(struct node a,struct node b){
	if(a.n==b.n){
		if(a.a==b.a){
			return a.b
ZOJ3207

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3370


输入:第一行为一个正整数N(1<=N<100)。以下N行每行一个关键单词,由字母,数字或下划线构成。接下来一个正整数K(1<=K<100)。以下K行每行第一个数为Ni,表示该行有Ni个单词,接下来是这Ni个单词,每个单词之间用空格分隔。

输出:对于K行的每一行,输出每行含有多少个关键单词。

分析:读入所有关键单词到map中去,然后对于每行依次判断每行中的单词是否在map中即可。


#include
#include
#include
using namespace std;
mapmp;
int main(){
	mp.clear();
	int T;
	string str;
	scanf("%d",&T);
	while(T--){
		cin>>str;
		if(mp[str]==0){
			mp[str]++;
		} 
	}
	int k;
	int m,sum=0;
	string str1; 
	scanf("%d",&k);
	while(k--){
		sum=0;
		scanf("%d",&m);
		while(m--){
			cin>>str1;
			sum+=mp[str1];
		}
		printf("%d\n",sum); 
	} 
	return 0;
}

ZOJ3209

点击打开DLX讲解
学习---->>>点击打开链接+点击打开链接

ZOJ3210[水]


#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int maxn=100007;
int a[maxn],b[maxn];
int main(){
 int T,N;
 cin>>T;
 while(T--){
  cin>>N;
  for(int i=0;i>a[i];
  }
  for(int i=0;i>b[i];
  }
  int flag=0,flag2=0;
  for(int i=0;i

ZOJ3211 Dream City[动态规划DP]

JAVAMAN is visiting Dream City and he sees a yard of gold coin trees. There are n trees in the yard. Let's call them tree 1, tree 2 ...and tree n. At the first day, each tree i has ai coins on it (i=1, 2, 3...n). Surprisingly, each tree i can grow bi new coins each day if it is not cut down. From the first day, JAVAMAN can choose to cut down one tree each day to get all the coins on it. Since he can stay in the Dream City for at most m days, he can cut down at most m trees in all and if he decides not to cut one day, he cannot cut any trees later. (In other words, he can only cut down trees for consecutive m or less days from the first day!)

Given n, m, ai and bi (i=1, 2, 3...n), calculate the maximum number of gold coins JAVAMAN can get.

Input

There are multiple test cases. The first line of input contains an integer T (T <= 200) indicates the number of test cases. Then T test cases follow.

Each test case contains 3 lines: The first line of each test case contains 2 positive integers n and m (0 < m <= n <= 250) separated by a space. The second line of each test case contains n positive integers separated by a space, indicating ai. (0 < ai <= 100, i=1, 2, 3...n) The third line of each test case also contains n positive integers separated by a space, indicating bi. (0 < bi <= 100, i=1, 2, 3...n)

Output

For each test case, output the result in a single line.

Sample Input

2
2 1
10 10
1 1
2 2
8 10
2 3

Sample Output

10
21

Hint s:
Test case 1: JAVAMAN just cut tree 1 to get 10 gold coins at the first day.
Test case 2: JAVAMAN cut tree 1 at the first day and tree 2 at the second day to get 8 + 10 + 3 = 21 gold coins in all.

关键:找出状态转移方程

分析:刚开始考虑复杂了,其实就是按照每天砍树从小到大排序,长的果子数从小到大排序,列出方程即可。

#include
#include
#include
#include
#include
using namespace std;
const int maxn=300;
int dp[maxn][maxn];
struct tree
{
    int t,g;
} w[maxn];
int cmp(tree a,tree b)
{
    if(a.g==b.g) return a.tcountt)
                    countt=dp[i][j];
            }
        }
      printf("%d\n",countt);
    }
    return 0;
}
ZOJ3212 K-nice

This is a super simple problem. The description is simple, the solution is simple. If you believe so, just read it on. Or if you don't, just pretend that you can't see this one.

We say an element is inside a matrix if it has four neighboring elements in the matrix (Those at the corner have two and on the edge have three). An element inside a matrix is called "nice" when its value equals the sum of its four neighbors. A matrix is called "k-nice" if and only if k of the elements inside the matrix are "nice".

Now given the size of the matrix and the value of k, you are to output any one of the "k-nice" matrix of the given size. It is guaranteed that there is always a solution to every test case.

Input

The first line of the input contains an integer T (1 <= T <= 8500) followed by T test cases. Each case contains three integers n, m, k (2 <= n, m <= 15, 0 <= k <= (n - 2) * (m - 2)) indicating the matrix size n * m and it the "nice"-degree k.

Output

For each test case, output a matrix with n lines each containing m elements separated by a space (no extra space at the end of the line). The absolute value of the elements in the matrix should not be greater than 10000.

Sample Input

2
4 5 3
5 5 3

Sample Output

2 1 3 1 1
4 8 2 6 1
1 1 9 2 9
2 2 4 4 3
0 1 2 3 0
0 4 5 6 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

题意:随便写个矩阵,满足题意即可

分析:用0,1矩阵。初始化为1,要想满足改为0即可。

#include
int main()
{
	int t,n,m,k,a[15][15];
	int i,j,sum;
	scanf("%d",&t);
	while(t--)
	{
		sum=0;//当sum为k个时,说明已经放好了
		scanf("%d%d%d",&n,&m,&k);
		for(i=0;i


你可能感兴趣的:(训练题解)