2013年8月8日

UVa 394 - Mapmaker

#include <cstdio>
#include <string>
#include <vector>
#include <map>
#define PB push_back
#define MP make_pair
using namespace std;

typedef pair<int, int> Bound;

struct Array {
  Array(string name, int base, int size, vector<Bound> & bounds)
    : name(name), base(base), size(size), bounds(bounds) {}
  string name;
  int base, size;
  vector<Bound> bounds;
};

int main() {
  int N, R;
  scanf("%d%d", &N, &R);
  vector<Array> arrays;
  map<string, int> indexOf;
  while (N--) {
    char name[99];
    int base, size, dim;
    scanf("%s%d%d%d", name, &base, &size, &dim);
    vector<Bound> v;
    for (int i = 0; i < dim; i++) {
      int l, u;
      scanf("%d%d", &l, &u);
      v.PB(MP(l, u));
    }
    arrays.PB(Array(name, base, size, v));
    indexOf[name] = arrays.size() - 1;
  }
  while (R--) {
    char name[99];
    scanf("%s", name);
    int choose = indexOf[name];
    int dim = arrays[choose].bounds.size(), size = arrays[choose].size, ans = arrays[choose].base;
    printf("%s[", name);
    for (int i = 0, index, others; i < dim; i++) {
      scanf("%d", &index);
      printf("%d%s", index, i == dim - 1 ? "" : ", ");
      others = 1;
      for (int j = i + 1; j < dim; j++) {
        others *= (arrays[choose].bounds[j].second - arrays[choose].bounds[j].first + 1);
      }
      ans += (index - arrays[choose].bounds[i].first) * others * size;
    }
    printf("] = %d\n", ans);
  }

  return 0;
}

沒有留言:

張貼留言