P1833 櫻花(二進制優化dp)

顏值不夠,學習來湊,還是決定認真得學一遍dp

題目傳送門

題目描述
愛與愁大神後院裏種了n棵櫻花樹,每棵都有美學值Ci。愛與愁大神在每天上學前都會來賞花。愛與愁大神可是生物學霸,他懂得如何欣賞櫻花:一種櫻花樹看一遍過,一種櫻花樹最多看Ai遍,一種櫻花樹可以看無數遍。但是看每棵櫻花樹都有一定的時間Ti。愛與愁大神離去上學的時間只剩下一小會兒了。求解看哪幾棵櫻花樹能使美學值最高且愛與愁大神能準時(或提早)去上學。

輸入格式
共n+1行:

第1行:三個數:現在時間Ts(幾點:幾分),去上學的時間Te(幾點:幾分),愛與愁大神院子裏有幾棵櫻花樹n。

第2行~第n+1行:每行三個數:看完第i棵樹的耗費時間Ti,第i棵樹的美學值Ci,看第i棵樹的次數Pi(Pi=0表示無數次,Pi是其他數字表示最多可看的次數Pi)。

輸出格式
只有一個整數,表示最大美學值。

輸入輸出樣例
輸入

6:50 7:00 3
2 1 0
3 3 1
4 5 4

輸出

11

#include<bits/stdc++.h>
#include<bitset>
#include<unordered_map>
#define pb push_back
#define bp __builtin_popcount
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=1e6+100;
const int MOD=1e4+7;
const double PI=3.1415926535;
int lowbit(int x){return x&-x;}
inline ll dpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t); b >>= 1; t = (t*t); }return r; }
struct node{
    int x,y;
};
vector<node>G;
ll dp[maxn];
int read() {
	int x = 0, ch = getchar();
	while(!isdigit(ch)) ch = getchar();
	while(isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
	return x;
} 
int bx,by,ex,ey,n,t,c,p;
int main()
{

    ios::sync_with_stdio(false);
    scanf("%d:%d %d:%d %d",&bx,&by,&ex,&ey,&n);

    int m=ex*60+ey-bx*60-by;

    for(int i=1;i<=n;i++)
    {
        t=read();c=read();p=read();
        if(p==0)p=m/t;
        for(int j=1;j<=p;j=j*2)//這裏用j<<1會tle
        {
         G.push_back(node{t*j,c*j});
         p-=j;
        }
        if(p)G.push_back(node{t*p,c*p});
    }
     for(auto it:G)
     {
         for(int j=m;j>=0;j--)
         {
             if(j>=it.x)
             dp[j]=max(dp[j],dp[j-it.x]+it.y);
         }
     }

     cout<<dp[m]<<endl;

   //system("pause");
   return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章