[csu1603]贪心

题意:有n门考试,对于考试i,不复习它能考si分,复习它的每小时能提高ai分,每过一小时ai会减小di,也就是说,连续复习某门科目每小时提高的分为ai, ai-di, ai-2di...,但每门考试最高分不超过100分,给定每门考试的考试时间,且考试本身不占时间,合理安排复习计划,问能否让所有考试都不低于60分,如果可以,总和最大为多少。

思路:比较明显的贪心,但难以想到正确的贪心策略。我们把问题分成两步,所有考试全部通过60分和得分总和最大。对于第一步,对于不复习分数低于60分的科目,先复习一段时间让它分数达到60分,贪心从考试时间往前延伸找空闲时间复习(一旦找不到空闲时间,则无解了)。对于第二步,按时间顺序的逆序处理,对于某个时间t,枚举所有考试时间大于等于t的科目,找到某一科目,使得复习它提高的分数最高,那么时间t就用来复习这门功课。

  1 #pragma comment(linker, "/STACK:10240000,10240000")

  2 

  3 #include <iostream>

  4 #include <cstdio>

  5 #include <algorithm>

  6 #include <cstdlib>

  7 #include <cstring>

  8 #include <map>

  9 #include <queue>

 10 #include <deque>

 11 #include <cmath>

 12 #include <vector>

 13 #include <ctime>

 14 #include <cctype>

 15 #include <set>

 16 #include <bitset>

 17 #include <functional>

 18 #include <numeric>

 19 #include <stdexcept>

 20 #include <utility>

 21 

 22 using namespace std;

 23 

 24 #define mem0(a) memset(a, 0, sizeof(a))

 25 #define mem_1(a) memset(a, -1, sizeof(a))

 26 #define lson l, m, rt << 1

 27 #define rson m + 1, r, rt << 1 | 1

 28 #define define_m int m = (l + r) >> 1

 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)

 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)

 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)

 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)

 33 #define all(a) (a).begin(), (a).end()

 34 #define lowbit(x) ((x) & (-(x)))

 35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}

 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}

 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}

 38 #define pchr(a) putchar(a)

 39 #define pstr(a) printf("%s", a)

 40 #define sstr(a) scanf("%s", a)

 41 #define sint(a) scanf("%d", &a)

 42 #define sint2(a, b) scanf("%d%d", &a, &b)

 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)

 44 #define pint(a) printf("%d\n", a)

 45 #define test_print1(a) cout << "var1 = " << a << endl

 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl

 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl

 48 

 49 typedef long long LL;

 50 typedef pair<int, int> pii;

 51 typedef vector<int> vi;

 52 

 53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};

 54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };

 55 const int maxn = 3e4 + 7;

 56 const int md = 10007;

 57 const int inf = 1e9 + 7;

 58 const LL inf_L = 1e18 + 7;

 59 const double pi = acos(-1.0);

 60 const double eps = 1e-4;

 61 

 62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}

 63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}

 64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}

 65 template<class T>T condition(bool f, T a, T b){return f?a:b;}

 66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}

 67 int make_id(int x, int y, int n) { return x * n + y; }

 68 

 69 struct Node {

 70     int r, s, a, d;

 71     Node(int r = 0, int s = 0, int a = 0, int d = 0): r(r), s(s), a(a), d(d) {}

 72     bool operator < (const Node that) const {

 73         return r < that.r;

 74     }

 75 };

 76 Node node[100];

 77 bool occ[1000];

 78 

 79 int main() {

 80     //freopen("in.txt", "r", stdin);

 81     int n;

 82     while (cin >> n) {

 83         int maxt = 0;

 84         bool ok = true;

 85         mem0(occ);

 86         rep_up0(i, n) {

 87             int s, t, a, d, tmp = 0;

 88             cin >> s >> t >> a >> d;

 89             node[i] = Node(t, s, a, d);

 90             max_update(maxt, t);

 91         }

 92         sort(node, node + n);

 93         rep_up0(i, n) {

 94             int cur = node[i].r;

 95             while (node[i].s < 60) {

 96                 while (occ[cur] && cur) cur--;

 97                 if (!cur || node[i].a <= 0) {

 98                     ok = false;

 99                     break;

100                 }

101                 occ[cur] = true;

102                 node[i].s += node[i].a;

103                 node[i].a -= node[i].d;

104             }

105             if (!ok) break;

106         }

107         rep_down1(i, maxt) {

108             if (!occ[i]) {

109                 int s = 0, p;

110                 rep_up0(j, n) {

111                     if (node[j].r >= i) {

112                         int dat = node[j].a;

113                         if (node[j].s + dat > 100) dat = 100 - node[j].s;

114                         if (dat > s) {

115                             s = dat;

116                             p = j;

117                         }

118                     }

119                 }

120                 if (s) {

121                     node[p].s += s;

122                     node[p].a -= node[p].d;

123                 }

124             }

125         }

126         int ans = 0;

127         rep_up0(i, n) {

128             ans += node[i].s;

129         }

130         if (ok) cout << ans << endl;

131         else puts("you are unlucky");

132     }

133 }
View Code

 

你可能感兴趣的:(贪心)