P1086 peanut picking solution to a problem

This question is just an ordinary analog, not the greedy!

Focus is this sentence: "and then find the rest of the plant where most peanuts, peanut to pick it."

That is, you next have to find most of the remaining peanuts, rather than by greed idea to consider the optimal solution for a limited time

So, should questions asked, this is just a simple simulation;

Idea is very simple: with a structure able to save valuable peanut plants each, the remaining junction of the tube without peanut 0,

Then customize cmp function by value in descending order for sorting, after accumulating from the largest, has been to more than plus or limit the outcome of all plants are added until it is finished, and then outputs the answer ans on it;

AC code is as follows:

#include<bits/stdc++.h>
using namespace std;
int m,n,k,value,num=0,ans=0,time1=0;
struct zbx{
    int x,y,peanut;
}hs[401];
bool cmp(zbx a,zbx b)
{
    return a.peanut>b.peanut;
}
int main(){
    scanf("%d%d%d",&m,&n,&k);
    for(int i=1;i<=m;i++)
        for(int j=1; J <= n-; J ++ ) 
        { 
            Scanf ( " % D " , & value);
             IF (value =! 0 ) 
            {
                 ++ NUM; 
                HS [NUM] .peanut = value; 
                HS [NUM] .x = J; HS [NUM] .y = I; 
            } 
        } 
    Sort (HS + . 1 , NUM + + HS . 1 , CMP);
     for ( int I = . 1 ; I <= NUM; I ++ ) 
    { 
        IF (I == . 1 ) // first need a special judge
        { 
            TIME1 + = HS [ . 1 ] .y + . 1 ;
             IF (TIME1 + HS [ . 1 ] .y> K) 
            { 
                the printf ( " 0 " ); return  0 ; 
            } 
            the else ANS = HS + [ . 1 ] .peanut; 
        } 
        the else / / other prevailing conditions 
        { 
            TIME1 + = ABS (HS [I] .x-HS [I- . 1 ] .x) + ABS (HS [I] .y-HS [I- . 1 ] .y) + . 1 ;
             IF (TIME1 + HS [I] .y>  K)
            {
                the printf ( " % D " , ANS); return  0 ; // if over time, do not continue to accumulate, the output end of the program the answer directly 
            }
             the else  
            { 
                ANS + = HS [I] .peanut; 
            } 
        }     
    } 
    the printf ( " % D " , ANS);
     return  0 ; 
}

Kan结 ✿✿ ヽ (° ▽ °) Bruno ✿

 

Guess you like

Origin www.cnblogs.com/lbssxz/p/10964299.html