AOJ2176 for the Peace

For the Peace

This is a story of a world somewhere far from the earth. In this world, the land is parted into a number of countries ruled by empires. This world is not very peaceful: they have been involved in army race. They are competing in production of missiles in particular.

それぞれの国の はミサイルの威力の合計で決まる.ミサイルはそれぞれの国と差が 以下なら捨てることが出来る.全ての国はミサイルを捨てることが出来るか?

何かを捨てたことによって,本来捨てられるべきものが捨てられなかった,という場合がないので,捨てられるものから捨てていった.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#include <stack>

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair

using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main() {
  int n, d;
  while(cin >> n >> d) {
      if(n == 0 && d == 0) break;

      stack<int> st[n];
      vector<int> sum(n);
      rep(i, n) {
          int m;
          cin >> m;

          rep(j, m) {
              int x;
              cin >> x;

              st[i].push(x);
              sum[i] += x;
          }
      }

      int id = 0, cnt = 0;
      while(cnt != n) {
          if(st[id].size() == 0) {
              id = (id + 1) % n;
              cnt++;
              continue;
          }

          int p = st[id].top();
          sum[id] -= p;

          int vmin = INF, vmax = 0;
          rep(i, n) {
              vmin = min(vmin, sum[i]);
              vmax = max(vmax, sum[i]);
          }

          if(vmax - vmin <= d) {
              st[id].pop();
              cnt = 0;
          } else {
              sum[id] += p;
              cnt++;
          }

          id = (id + 1) % n;
      }

      bool flag = true;
      rep(i, n) {
          if(sum[i] == 0) continue;
          flag  = false;
      }

      if(flag) cout << "Yes" << endl;
      else cout << "No" << endl;
  }
  return 0;
}
May 29th, 2016