Codeforces Round #579 (Div. 3) Complete the Projects(贪心、DP)

 http://codeforces.com/contest/1203/problem/F1

 

Codeforces Round #579 (Div. 3) Complete the Projects(贪心、DP) | 91博客

Examples

input 1
3 4
4 6
10 -2
8 -1
output 1
YES
input 2
3 5
4 -5
4 -2
1 3
output 2
YES
input 3
4 4
5 2
5 -3
2 1
4 -2
output 3
YES
input 4
3 10
10 0
10 -10
30 0
output 4
NO

Note

In the first example, the possible order is: 1,2,3.

In the second example, the possible order is: 2,3,1.

In the third example, the possible order is: 3,1,4,2.

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include <set>
 9 #include <math.h>
10 const int INF=0x3f3f3f3f;
11 typedef long long LL;
12 const int mod=1e9+7;
13 const double PI=acos(-1);
14 const int maxn=1e5+10;
15 using namespace std;
16 
17 struct node1
18 {
19     int a;
20     int b;
21 }zheng[105];
22 
23 struct node2
24 {
25     int a;
26     int b;
27 }fu[105];
28 
29 int n1,n2;
30 
31 bool cmp1(node1 x,node1 y)
32 {
33     return x.a<y.a; 
34 }
35 
36 bool cmp2(node2 x,node2 y)
37 {
38     if(x.a+x.b!=y.a+y.b)
39         return x.a+x.b>y.a+y.b;
40     else
41         return x.b>y.b;
42 }
43 
44 int main()
45 {
46     int n,r;
47     scanf("%d %d",&n,&r);
48     for(int i=1;i<=n;i++)
49     {
50         int x,y;
51         scanf("%d %d",&x,&y);
52         if(y<0)
53         {
54             fu[n2].a=x;
55             fu[n2].b=y;
56             n2++;
57         }
58         else
59         {
60             zheng[n1].a=x;
61             zheng[n1].b=y;
62             n1++;
63         }
64     }
65     sort(zheng,zheng+n1,cmp1);
66     for(int i=0;i<n1;i++)
67     {
68         if(r<zheng[i].a)
69         {
70             printf("NO\n");
71             return 0;
72         }
73         r+=zheng[i].b;
74     }
75     sort(fu,fu+n2,cmp2);
76     for(int i=0;i<n2;i++)
77     {
78         if(r<fu[i].a)
79         {
80             printf("NO\n");
81             return 0;
82         }
83         r+=fu[i].b;
84     }
85     if(r<0)
86         printf("NO\n");
87     else
88         printf("YES\n");
89     return 0;
90 }

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 #define endl '\n'
 7  
 8 const int maxn = 100;
 9 int dp[600001];
10 pair<int, int> a[maxn];
11 queue<int> q;
12  
13 int main(){
14     ios::sync_with_stdio(false);
15     cin.tie(NULL);
16     
17     int n, r;
18     cin >> n >> r;
19     
20     for(int i = 0; i < n; i++){
21         cin >> a[i].first >> a[i].second;
22     } 
23     
24     sort(a, a + n);
25     
26     int l = 0, sum = r, ans = 0;
27     for(; l < n && a[l].first <= sum; l++){
28         if(a[l].second >= 0){
29             q.push(l);
30             ans++;
31         }
32     }
33     
34     while(!q.empty()){
35         int c = q.front();
36         q.pop();
37         
38         sum += a[c].second;
39         
40         for(; l < n && a[l].first <= sum; l++){
41             if(a[l].second >= 0){
42                 q.push(l);
43                 ans++;
44             }
45         } 
46     }
47     
48     sort(a, a + l, [&](pair<int, int> f, pair<int, int> g){
49         return f.first + f.second > g.first + g.second;    
50     });
51     dp[0] = ans;
52     
53     for(int i = 0; i < l; i++){
54         if(a[i].second >= 0) continue;
55         int y = -a[i].second;
56         
57         for(int j = sum - max(y, a[i].first); j >= 0; j--){
58             dp[j + y] = max(dp[j + y], dp[j] + 1);
59             ans = max(ans, dp[j + y]);
60         }
61     }
62     
63     cout << (ans == n ? "YES" : "NO") << endl;
64  
65     return 0;
66 }

 

 

 


 

 http://codeforces.com/contest/1203/problem/F2

 

 

Codeforces Round #579 (Div. 3) Complete the Projects(贪心、DP) | 91博客

Examples

input 1
3 4
4 6
10 -2
8 -1
output 1
 3
input 2
5 20
45 -6
34 -15
10 34
1 27
40 -45
output 2
 5
input 3
3 2
300 -300
1 299
1 123
output 3
 3

 

先粘题解,以后再填坑

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <queue>
 8 #include<bits/stdc++.h>
 9 using namespace std;
10  
11 #define PI acos(-1)
12 #define hell 1000000007
13 #define HELL 998244353
14 #define io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
15 #define fix(n) cout << fixed << setprecision(n)
16 #define mset(a,n) memset(a,n,sizeof a)
17 #define rep(i,a,b) for (__typeof((b)) i=(a);i<(b);i++)
18 #define repp(i,a,b,p) for(__typeof((b)) i=(a);i<(b);i+=p)
19 #define ren(i,a,b) for(__typeof((a)) i=(a);i>=(b);i--)
20 #define renn(i,a,b,p) for(__typeof((a) i=(a);i>=(b);i-=p)
21 #define ADD(a,b,c) ((a)%c+(b)%c)%c
22 #define SUB(a,b,c) ((a)%c-(b)%c+c)%c
23 #define MUL(a,b,c) (((a)%c)*((b)%c))%c
24 #define lbd lower_bound
25 #define ubd upper_bound
26 #define ll long long
27 #define ld long double
28 #define pb push_back
29 #define fi first
30 #define se second
31 #define vll vector<ll>
32 #define pll pair<ll,ll>
33 #define vpll vector<pll>
34 #define all(v) (v).begin(), (v).end()
35 #define sz(x) (ll)x.size()
36 #define endl "\n"
37 #define out(n) cout<<n<<" "
38 #define outl(n) cout<<n<<endl
39 #define line cout<<endl
40 #define bug(n) {outl(n);return;}
41 #define N 105
42 ll n,r,dp[N][300005];
43 pll a[N];
44 bool comp(pll a, pll b){
45     if(a.se>0&&b.se>0)return a.fi<b.fi;
46     if(a.se>0||b.se>0)return a.se>0;
47     return a.fi+a.se>b.fi+b.se;
48 }
49 ll fun(ll i, ll r){
50     if(dp[i][r]!=-1)return dp[i][r];
51     if(i==n+1)return 0;
52     ll ans=fun(i+1,r);
53     if(r>=a[i].fi&&r>=-a[i].se)ans=max(ans,1+fun(i+1,r+a[i].se));
54     return dp[i][r]=ans;
55 }
56 void solve(){
57     cin>>n>>r;
58     mset(dp,-1);
59     rep(i,1,n+1)cin>>a[i].fi>>a[i].se;
60     sort(a+1,a+n+1,comp);
61     bug(fun(1,r));
62 }
63 void prep(){
64  
65 }
66 int main(){
67     io;
68     ll t=1;
69     // cin>>t;
70     prep();
71     fix(12);
72     while(t--)
73         solve();
74     return 0;
75 }

 

 

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <queue>
 5 using namespace std;
 6 #define endl '\n'
 7  
 8 const int maxn = 100;
 9 int dp[600001];
10 pair<int, int> a[maxn];
11 queue<int> q;
12  
13 int main(){
14     ios::sync_with_stdio(false);
15     cin.tie(NULL);
16     
17     int n, r;
18     cin >> n >> r;
19     
20     for(int i = 0; i < n; i++){
21         cin >> a[i].first >> a[i].second;
22     } 
23     
24     sort(a, a + n);
25     
26     int l = 0, sum = r, ans = 0;
27     for(; l < n && a[l].first <= sum; l++){
28         if(a[l].second >= 0){
29             q.push(l);
30             ans++;
31         }
32     }
33     
34     while(!q.empty()){
35         int c = q.front();
36         q.pop();
37         
38         sum += a[c].second;
39         
40         for(; l < n && a[l].first <= sum; l++){
41             if(a[l].second >= 0){
42                 q.push(l);
43                 ans++;
44             }
45         } 
46     }
47     
48     sort(a, a + l, [&](pair<int, int> f, pair<int, int> g){
49         return f.first + f.second > g.first + g.second;    
50     });
51     dp[0] = ans;
52     
53     for(int i = 0; i < l; i++){
54         if(a[i].second >= 0) continue;
55         int y = -a[i].second;
56         
57         for(int j = sum - max(y, a[i].first); j >= 0; j--){
58             dp[j + y] = max(dp[j + y], dp[j] + 1);
59             ans = max(ans, dp[j + y]);
60         }
61     }
62     
63     cout << ans << endl;
64  
65     return 0;
66 }

 

 
 
 
 

 

 

原文:https://www.cnblogs.com/jiamian/p/11349587.html

91 发布于 2019-08-14 07:11 评论(0) 阅读(17)
发表评论(最多500字):

推荐博客排行

统计信息

  • 博客[ 6429 ]
  • 评论[ 4 ]
  • 阅读[ 142460 ]