P1095 파수꾼의 탈출(DP 또는 욕심)

13851 단어 낙곡
제목 링크:https://www.luogu.org/problemnew/show/P1095동적 계획:https://blog.csdn.net/weixin_39778570/article/details/87014343ACM 문제집:https://blog.csdn.net/weixin_39778570/article/details/83187443
  :  M,S,T      ,  ,  。
       17/s,  10    60/s,      4/s
            S,          
----------------------------------------------------
               ,5s  20,    ,    120/7s
      119/7s
            ,  DP(     ,          )

DP
#include 
#include 
using namespace std;
int dp[300001];
int main(){
    int m,s,t;
    scanf("%d%d%d",&m,&s,&t);
    for(int i=1;i<=t;i++){//      
        if(m>=10)dp[i]=dp[i-1]+60,m-=10;//    ,  
        else dp[i]=dp[i-1],m+=4;//    
    }
    for(int i=1;i<=t;i++){
		dp[i]=max(dp[i],dp[i-1]+17);//    ,dp[i]            (  )
    	if(dp[i]>=s){printf("Yes
%d"
,i);return 0;}// s, , yes } printf("No
%d"
,dp[t]);// , no return 0; }

탐욕스럽다
#include
#define ll long long
#define R register int 
#define fo(i,j,n) for(R i=j; i<=n;++i) 
using namespace std;
int M,S,T,now;
void solve1(){
	int s1 = 0, s2 = 0;
	fo(i,1,T){
		s1 += 17; //   
		if(M>=10){ //   
			M-=10;
			s2+=60;
		}else M+=4;
		s1 = max(s1,s2); //     ,     
		if(s1>=S){
			cout<<"Yes"<<endl<<i;
			return;
		} 
	}
	cout<<"No"<<endl<<s1;
} 
void solve2(){
	int ans= 0,time=0, blink=0;
	while(ans<S){
		time++;
		if(time>T){
			cout<<"No"<<endl<<ans; 
			return;
		}
		if(M>=10){//    
			blink+=60;
			M-=10;
		}else M+=4;
		ans+=17; //  +  
		ans = max(ans, blink);
	}
	cout<<"Yes"<<endl<<time; 
} 
int main(){
	cin>>M>>S>>T;
	solve2();
	return 0;
}

좋은 웹페이지 즐겨찾기