Codeforces Round #579 (Div. 3) F2. Complete the Projects (hard version) dp + 贪心
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #579 (Div. 3) F2. Complete the Projects (hard version) dp + 贪心
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
文章目錄
- 題意:
- 思路:
題意:
思路:
排序方式跟easyeasyeasy版本的一樣,但是hardhardhard版本是輸出最多能選多少,所以我們對b<0b<0b<0的情況不能直接貪心的來選了,考慮用dpdpdp來解決。
設f[i][j]f[i][j]f[i][j]表示當前選了前iii個任務,能力值為jjj的時候選的最多任務數。
轉移的話比較簡單,按照背包來寫就好,但是轉移的順序不能改變,也就是說我們需要提前按照ai+bia_i+b_iai?+bi?排序之后才能進行dpdpdp,因為先選哪個顯然是有影響的,排序保證了先選的一定比后面選的更優。
// Problem: F1. Complete the Projects (easy version) // Contest: Codeforces - Codeforces Round #579 (Div. 3) // URL: https://codeforces.com/problemset/problem/1203/F1 // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native") //#pragma GCC optimize(2) #include<cstdio> #include<iostream> #include<string> #include<cstring> #include<map> #include<cmath> #include<cctype> #include<vector> #include<set> #include<queue> #include<algorithm> #include<sstream> #include<ctime> #include<cstdlib> #include<random> #include<cassert> #define X first #define Y second #define L (u<<1) #define R (u<<1|1) #define pb push_back #define mk make_pair #define Mid ((tr[u].l+tr[u].r)>>1) #define Len(u) (tr[u].r-tr[u].l+1) #define random(a,b) ((a)+rand()%((b)-(a)+1)) #define db puts("---") using namespace std;//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); } //void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); } //void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }typedef long long LL; typedef unsigned long long ULL; typedef pair<int,int> PII;const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f; const double eps=1e-6;LL n,m; int f[110][60011]; struct Node {int a,b; }; vector<Node>v1,v2;bool cmp1(Node a,Node b) {return a.a<b.a; }bool cmp2(Node a,Node b) {return a.a+a.b>b.a+b.b; }int main() { // ios::sync_with_stdio(false); // cin.tie(0);scanf("%lld%lld",&n,&m);for(int i=1;i<=n;i++) {int a,b; scanf("%d%d",&a,&b);if(b>=0) v1.pb({a,b});else v2.pb({a,b});}sort(v1.begin(),v1.end(),cmp1);sort(v2.begin(),v2.end(),cmp2);int cnt=0;for(auto x:v1) {if(m>=x.a) m+=x.b,cnt++;else break;}for(int i=1;i<=v2.size();i++) {int a=v2[i-1].a,b=v2[i-1].b;for(int j=0;j<=m;j++) f[i][j]=f[i-1][j];for(int j=0;j<=m;j++) {if(j+b>=0&&j>=a) f[i][j+b]=max(f[i][j+b],f[i-1][j]+1);}}int ans=0;for(int i=0;i<=m;i++) ans=max(ans,f[v2.size()][i]);printf("%d\n",ans+cnt);return 0; } /**/總結
以上是生活随笔為你收集整理的Codeforces Round #579 (Div. 3) F2. Complete the Projects (hard version) dp + 贪心的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 左旋肉碱减肥有用吗
- 下一篇: 黄瓜减肥7天瘦10斤具体方法