您的位置:首页 > 其它

poj1042

2013-07-01 15:56 447 查看
Gone Fishing
http://poj.org/problem?id=1042

 

此题枚举+贪心 解决,注意时间以5分钟为一个单位。

Memory: 244K Time: 125MS

源代码
//优先排序 贪心算法
#include<iostream>
using namespace std;
#define NUM_LIMIT 26
int n;
int f[NUM_LIMIT];
int d[NUM_LIMIT];
int t[NUM_LIMIT-1];
int max;
int array_max[NUM_LIMIT]; //max stay time
void input()
{
//cin>>h;
for (int i = 1; i <= n; ++i){ //!!! ++i i++ the same!
cin >> f[i];
}
for (int i = 1; i <= n; ++i){
cin >> d[i];
}
for (int i = 1; i < n; ++i){
cin >> t[i];
}
}
void most_til(int h, int n, int * arr) //h num of 5 mins
{
for(int i=0;i<NUM_LIMIT;i++)
arr[i] = 0;

}
void calculate()
{
int array_temp[NUM_LIMIT];
//init
max = -1;
int h_count=h*60/5;
t[0]=0;

for (int i=1; i<=n; i++){ //stop at lake i

h_count = h_count-t[i-1];
if(h_count <= 0){
if (max < 0){
max = 0;
for (int j=1; j<=n; j++){
array_max[j]=0;
}
}
break;
}
int temp = most_til(h_count, i, array_temp);
//max=max>temp?max:temp;
if (max < temp) {
max = temp;
copy(array_max, array_temp); //call by reference
}
}
}
void output()
{
for(int i=1; i<n; i++){
cout << array_max[i] << ", ";
}
cout << array_max
<< endl;
cout << "Number of fish expected: " << max << endl << endl;
}
int main()
{
//int n;
//cout<<n<<endl;

while(cin>>n&&n!=0){
input();
calculate();
output();
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  poj