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;
}
}
}