G - Problem G

CLICK HERE TO HAVE A SEE

Time limit1000 ms
Memory limit512000 kB

原题:

著名出题人小Q每次比赛后都会写一份《赛题分析》,包含比赛概况、每题的参考算法以及一些统计数值。

对于一道题来说,小Q会统计最短的验题人代码长度(Shortest judge solution)以及赛内参赛队伍最短的AC代码长度(Shortest team solution)。

统计验题人代码长度比较容易,因为验题人最多也不会超过2020个。但是统计选手代码长度就不容易了,因为大赛区动辄三四百支队伍。

请写一个程序,帮助小Q统计最短代码长度。

Input

第一行包含一个正整数T(1≤T≤13)T(1≤T≤13),表示赛题数量。

每道题第一行包含两个整数n,m(2≤n≤20,0≤m≤500)n,m(2≤n≤20,0≤m≤500),分别表示验题人数量以及AC了该题的队伍数量。

第二行包含nn个正整数a1,a2,…,an(50≤ai≤65536)a1,a2,…,an(50≤ai≤65536),依次表示每个验题人的代码字节数。

第三行包含mm个正整数b1,b2,…,bn(50≤bi≤65536)b1,b2,…,bn(50≤bi≤65536),依次表示每支AC队伍的代码字节数。若m=0m=0则该行为空行。

Output

对于第i(1≤i≤T)i(1≤i≤T)道题,输出三行,第一行输出Problem xx:,其中x=i+1000x=i+1000。

第二行输出Shortest judge solution: yy bytes.,其中yy表示最短的验题人代码字节数。

第三行输出Shortest team solution: zz bytes.,其中zz表示最短的选手代码字节数,若不存在请输出N/A。

注意:间隔都是一个空格。

Sample Input

2
3 2
3627 1460 5288
2365 2671
2 0
5510 7682

著名出题人小Q每次比赛后都会写一份《赛题分析》,包含比赛概况、每题的参考算法以及一些统计数值。

对于一道题来说,小Q会统计最短的验题人代码长度(Shortest judge solution)以及赛内参赛队伍最短的AC代码长度(Shortest team solution)。

统计验题人代码长度比较容易,因为验题人最多也不会超过2020个。但是统计选手代码长度就不容易了,因为大赛区动辄三四百支队伍。

请写一个程序,帮助小Q统计最短代码长度。
Input
第一行包含一个正整数T(1≤T≤13)T(1≤T≤13),表示赛题数量。

每道题第一行包含两个整数n,m(2≤n≤20,0≤m≤500)n,m(2≤n≤20,0≤m≤500),分别表示验题人数量以及AC了该题的队伍数量。

第二行包含nn个正整数a1,a2,…,an(50≤ai≤65536)a1,a2,…,an(50≤ai≤65536),依次表示每个验题人的代码字节数。

第三行包含mm个正整数b1,b2,…,bn(50≤bi≤65536)b1,b2,…,bn(50≤bi≤65536),依次表示每支AC队伍的代码字节数。若m=0m=0则该行为空行。
Output
对于第i(1≤i≤
T)i(1≤i≤T)道题,输出三行,第一行输出Problem xx:,其中x=i+1000x=i+1000。

第二行输出Shortest judge solution: yy bytes.,其中yy表示最短的验题人代码字节数。

第三行输出Shortest team solution: zz bytes.,其中zz表示最短的选手代码字节数,若不存在请输出N/A。

注意:间隔都是一个空格。

Sample Input

2
3 2
3627 1460 5288
2365 2671
2 0
5510 7682

Sample Output

Problem 1001:
Shortest judge solution: 1460 bytes.
Shortest team solution: 2365 bytes.
Problem 1002:
Shortest judge solution: 5510 bytes.
Shortest team solution: N/A bytes.

问题分析:
考虑要取第三,第四行输入的数字中的最小的那个输出,并且要输入T之后,在运行的T组数据后要可以结束程序。
个人说法:
这题本人错了很多次在Runtime以及Time Limit Exceeded,
第一次分别用了两个数组来储存第三行和第四行的数字,用了多个for()循环来输出bytes以及用来输入n,m,后来提交超时…
第二次把循环的部分利用函数放在前面,主函数做调用函数来输入数据输出数据,结果超时…
第三次发现一个头文件是#include< algorithm >,主函数利用sort(a,a+5)来对输入的验题人代码字节和选手代码字节进行降序排序,然后a[0]即为最小的数,详情点击C语言sort函数如何使用,结果还是超时…
第四次通过舍友的帮忙,尝试了不用函数以及数组而是直接输入一个数据,每输入一个数就比较一次的方法,最后accepted!

VS通过的我的代码如下:

#include <iostream>
#include<cstdio>
using namespace std;
int main()
{
	int i, n, m, x = 1001;
	cin >> i;
	int min1,min2;
	while (i--)
	{
		int j, min1, min2, a, b;
		cin >> n >> m;
		cin >> a;
		min1 = a;
		for (j = 1;j < n;j++)
		{
			cin >> a;
			if (min1 > a)
			{
				min1 = a;
			}
		}
		if (m == 0)
		{
			goto l;
		}
		cin >> b;
		min2 = b;
		for (j = 1;j < m;j++)
		{
			cin >> b;
			if (min2 > b)
			{
				min2 = b;
			}
		}
	l:;
			
			printf("Problem %d:\n", x++);
			printf("Shortest judge solution: %d bytes.\n",min1 );
			if (m != 0)
			{
				
				printf("Shortest team solution: %d bytes.\n", min2);
			}
			else if (m == 0)
			{
				printf("Shortest team solution: N/A bytes.\n");
			}
	}
}

其中一个舍友的程序:

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int T,sb,me;
    scanf("%d",&T);
    int t=1;
    while(T--)
    {
        scanf("%d%d",&sb,&me);
        int *a=new int[sb];
        int *b=new int[me];
        for(int i=0;i<sb;i++) scanf("%d",&a[i]);
        if(me==0) goto l1;
        for(int i=0;i<me;i++) scanf("%d",&b[i]);
        sort(b,b+me);
        l1:;
        sort(a,a+sb);
        printf("Problem %d:\n",(1000+t));
        printf("Shortest judge solution: %d bytes.\n",a[0]);
        if(me==0) printf("Shortest team solution: N/A bytes.\n");
        else printf("Shortest team solution: %d bytes.\n",b[0]);
        t++;
    }
}

再一位舍友的程序:

#include <iostream>
using namespace std;
void f(int*a,int j)
{
	for (int k = 0; k < j; k++)
	{
		for (int s = k + 1; s < j; s++) {
			if (*(a + k) > *(a + s))
			{
				int temp = *(a + k);
				*(a + k) = *(a + s);
				*(a + s) = temp;
			}
		}
	}
}

int main()
{
	int t, ju, te,n[30],m[503],j; 
	cin >> t;
	for (j = 0; j < t; j++)
	{
		cin >> ju;
		cin >> te;
		for (int g = 0; g < ju; g++)
		{
			cin >> n[g];
		}
		for (int h = 0; h < te; h++)
		{
			cin >> m[h];
		}
		f(m, te);
		f(n, ju);
		cout << "Problem " << (j + 1001)<<":"<<endl;
		cout << "Shortest judge solution: " << *n<<" bytes."<<endl;
		if (te == 0) 
		{
			cout << "Shortest team solution: " << "N/A" << " bytes." << endl;
		}
		else
		{
           cout << "Shortest team solution: " << *m << " bytes." << endl;
		}
		




	}
     
}

猜你喜欢

转载自blog.csdn.net/weixin_43697280/article/details/84963437
G