1.喝咖啡
//82% #include<iostream> #include<bits/stdc++.h> using namespace std; int main() { int N,A,X; vector<int>bug; int bug_sum=0; cin>>N; cin>>A; cin>>X; for(int i=0;i<N;i++) { int temp; cin>>temp; bug_sum=bug_sum+temp; bug.push_back(temp); } int coffee=60*A*X; if(coffee>=bug_sum) { if(bug_sum%A==0) cout<<bug_sum/A<<endl; else cout<<(bug_sum/A)+1<<endl; } else { cout<<bug_sum-coffee+60*X<<endl; } }2.01背包
#include <algorithm> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <unordered_map> #include <vector> #define fir first #define se second #define ll long long #define pb push_back #define mp make_pair #define ull unsigned long long #define cl(a, b) memset(a, b, sizeof(a)) #define quickio(a) ios::sync_with_stdio(a) #define datatest() freopen("data.in", "r", stdin) #define makeans() freopen("data.out", "w", stdout) #define makedata() freopen("data.in", "w", stdout) #define pii pair<int, int> #define pll pair<ll, ll> #define pdd pair<double, double> using namespace std; const int maxn = 100 + 10; const int maxm = 1e6 + 10; const int inf = 0x3f3f3f3f; const ll mod = 1e9 + 7; const int maxblock = sqrt(maxn) + 10; const double eps = 1e-7; const ll INF = 1e16; int n, t; int v[maxn], l[maxn], c[maxn]; int dp[10010]; int weight[maxn * 7], cost[maxn * 7]; int main() { scanf("%d %d", &n, &t); for (int i = 1; i <= n; i++) { scanf("%d %d %d", &v[i], &l[i], &c[i]); } int cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= 10; j++) { if (c[i] >= (1 << j)) { weight[++cnt] = (1 << j) * v[i]; cost[cnt] = (1 << j) * l[i]; c[i] -= (1 << j); } } if (c[i]) { weight[++cnt] = (c[i]) * v[i]; cost[cnt] = (c[i]) * l[i]; } } memset(dp, 0, sizeof(dp)); for (int i = 1; i <= cnt; i++) { // cout << "weight[i]=" << weight[i] << endl; for (int j = t; j >= weight[i]; j--) { dp[j] = max(dp[j], dp[j - weight[i]] + cost[i]); } } printf("%d\n", dp[t]); return 0; }
全部评论
(2) 回帖