【贪心】守望者的逃离

原题传送门

思路


这道题非常好,很烧脑,它很难,这个“难”并非体现在代码层面,而是思维层面,这道题的代码十分简单,但能想出这个代码却十分困难,如果想不到这么好的贪心算法,就只能把他当DP题做了。

贪心思路

S1:走路(17m/s)和 S2:闪烁(60m/s)同时进行
当 S2>S1 时:用 S2 来更新 S1;
当 S2<S1 时:就接着进行
主程序while循环大意:
一个时间单位可以进行的动作;

Code


#include<iostream>
using namespace std;
int quantity,s,gktime;
int main()
{
    cin>>quantity>>s>>gktime;
    int s1=0,s2=0,usetime=0;
    while(s>s1 && gktime>0)
    {
        s1+=17;
        if(quantity>=10) s2+=60,quantity-=10;
        else quantity+=4;
        s1=max(s1,s2);
        gktime--;usetime++;
    }
    if(s1<s)
    cout<<"No"<<endl<<s1;
    else
    cout<<"Yes"<<endl<<usetime;
    return 0;
}
原文地址:https://www.cnblogs.com/gongdakai/p/11246085.html