Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
穷举法 超时了...#include <iostream> #include <vector> using namespace std; struct problem{ int count; int priporty[10][2]; }; int n,m; int choose[11][3]={0}; int fianlResult[11][3]={0}; int IntelligenceOfTeams[4]; problem problems[11]; float averageTime; float getSingleTime(int m,int n){ for (int i = problems[n].count;i >= 1 ;i--) { if ( problems[n].priporty[i][0] <= IntelligenceOfTeams[m]) { return problems[n].priporty[i][1]; } } return 0; } float getMintime(vector<int> times){ int lis[11][2]; int i; for (i = 0 ;i < times.size();i+=2) { lis[i/2][0] = times[i]; lis[i/2][1] = times[i+1]; } for (i = 0;i < times.size()/2-1;i++) { for (int j = i;j < times.size()/2-1;j++) { if (lis[j][1] > lis[j+1][1]) { int temp[2]; temp[0] = lis[j][0]; temp[1] = lis[j][1]; lis[j][0] = lis[j+1][0]; lis[j][1] = lis[j+1][1]; lis[j+1][0] = temp[0]; lis[j+1][1] = temp[1]; } } } int sum = 0; float finalSun = 0; for (i = 0;i < times.size()/2;i++) { choose[lis[i][0]][1] = sum; sum += lis[i][1]; finalSun += sum; choose[lis[i][0]][2] = sum; } return finalSun; } float getaverageTime(){ vector<int> times[4]; for (int i = 1;i <= n;i++) { switch(choose[i][0]) { case 1: times[1].push_back(i); times[1].push_back(getSingleTime(1,i)); break; case 2: times[2].push_back(i); times[2].push_back(getSingleTime(2,i)); break; case 3: times[3].push_back(i); times[3].push_back(getSingleTime(3,i)); break; } } float average=0; for (int j = 1;j <= m;j++) { if (times[j].size() != 0) { average+= getMintime(times[j]); } } return (average/n); } void cases(int num){ if (num == 1) { for (int i = 1;i <= m;i++) { if (problems[num].priporty[1][0] <= IntelligenceOfTeams[i]) { choose[num][0] = i; float tempave = getaverageTime(); if (averageTime > tempave || averageTime <= 0) { averageTime = tempave; for (int i = 1;i <= 10;i++) { fianlResult[i][0] = choose[i][0]; fianlResult[i][1] = choose[i][1]; fianlResult[i][2] = choose[i][2]; } } } } }else{ for (int i = 1;i <= m;i++) { if (problems[num].priporty[1][0] <= IntelligenceOfTeams[i]) { choose[num][0] = i; cases(num-1); } } } } int main(){ cin >> m >> n; int count = 1; while (m != 0 && n != 0) { averageTime = -1; int i,j; for (i = 1;i <= m;i++) { cin>>IntelligenceOfTeams[i]; } for (j = 1;j <= n;j++) { cin >> problems[j].count; for (int k = 1;k <= problems[j].count;k++) { cin>>problems[j].priporty[k][0]; cin>>problems[j].priporty[k][1]; } } for (j = 1;j <= n;j++) { cout << problems[j].count << endl; for (int k = 1;k <= problems[j].count;k++) { cout << problems[j].priporty[k][0] << endl; cout << problems[j].priporty[k][1] << endl; } } cases(n); cout << "Case " << count << endl; printf("Average solution time = %.2f\n",averageTime); for (i = 1;i <= n;i++) { cout<<"Problem "<<i<<" is solved by member "<<fianlResult[i][0]<<" from "<<fianlResult[i][1]<<" to "<<fianlResult[i][2]<<endl; } cin >> m >> n; count++; } return 0; } 哪位同仁给点意见? Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator