codeforces 1200B. Block Adventure(简单模拟)

题目链接:https://codeforces.com/contest/1200/problem/B

WA了八发。。。

思路:在保证能跳到下一个砖块的前提下,包里的砖块数最大,防止需要砖块时不够的情况。每一种情况都得考虑到:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <map>
 8 #define mem(a,b) memset(a,b,sizeof(a));
 9 using namespace std;
10 #define INF 0x3f3f3f3f
11 typedef long long ll;
12 int dir[4][2] = {0,1,0,-1,1,0,-1,0};
13 const int maxn = 100005;
14 int main()
15 {
16     int t,n,m,k;
17     ll a[105];
18     cin >> t;
19     while(t--)
20     {
21         bool flag = 0;
22         cin >> n >> m >> k;
23         mem(a,0);
24         for(int i =1; i <= n; i++)
25             cin >> a[i];
26         for(int i = 1; i < n; i++)
27         {
28             ll d,ne;
29             if(a[i+1] > a[i])
30             {
31                 d = a[i+1] - a[i];
32                 if(k >= a[i+1])
33                 {
34                     m += a[i];
35                 }
36                 else
37                 {
38                     if(d > k)
39                     {
40                         ne = a[i+1] - k - a[i];
41                         //   cout << "ne = " << ne << "m = " << m << " a[i] = " << a[i] << " a[i+1] = " << a[i+1] <<endl;
42                         if(m < ne)
43                         {
44                             flag = 1;
45                             break;
46                         }
47                         else
48                         {
49                             m -= ne;
50                         }
51                     }
52 
53                     else
54                     {
55                         ne = a[i] - (a[i+1] - k);
56                         m += ne;
57                     }
58                 }
59             }
60             else
61             {
62                 d = a[i] - a[i+1];
63                 if(k <= a[i+1])
64                 {
65                     ne = a[i] - (a[i+1] - k);
66                     m += ne;
67                 }
68                 else
69                 {
70                     ne = a[i] ;
71                     m += ne;
72                 }
73             }
74 
75         }
76 
77         if(flag == 1)
78             cout << "NO" <<endl;
79         else
80             cout << "YES" << endl;
81     }
82     return 0;
83 }

 

posted on 2019-08-16 09:12  一只小毛球  阅读(353)  评论(0编辑  收藏  举报

导航