POJ 1042 Gone Fishing(模拟+贪心)

来源:互联网 发布:廊坊市网络招聘 编辑:程序博客网 时间:2024/05/21 10:39

题目地址:http://poj.org/problem?id=1042

思路:模拟+贪心

AC代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;struct node{    int first;    int reduce;}a[30];int b[30];int time[30];int pre[30][30];int c[30];int main(){    int n,h;    while(scanf("%d",&n) && n)    {        scanf("%d",&h);        h *= 12;        memset(pre,0,sizeof(pre));        memset(c,0,sizeof(c));        for(int i=1; i<=n; i++)            scanf("%d",&a[i].first);        for(int i=1; i<=n; i++)            scanf("%d",&a[i].reduce);        time[1] = 0;        for(int i=2; i<=n; i++)        {            int x;            scanf("%d",&x);            time[i] = time[i-1] + x;        }        for(int i=1; i<=n; i++)        {            int total = h - time[i];            for(int j=1; j<=i; j++)                b[j] = a[j].first;            for(int j=1; j<=total; j++)            {                int max1 = b[1];                int tt = 1;                for(int k=1; k<=i; k++)                {                    if(b[k] > max1)                    {                        max1 = b[k];                        tt = k;                    }                }                b[tt] -= a[tt].reduce;                if(b[tt] < 0)                    b[tt] = 0;                c[i] += max1;                pre[i][tt]++;            }        }        int max1 = -1;        int l;        for(int i=1; i<=n; i++)        {            if(c[i] > max1)            {                max1 = c[i];                l = i;            }        }        for(int i=1; i<=n-1; i++)            printf("%d, ",pre[l][i]*5);        printf("%d\n",pre[l][n]*5);        printf("Number of fish expected: %d\n\n",max1);    }    return 0;}


0 0