SRM334 D1E EncodedSum

TopCoder Statistics - Problem Statement

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

$A \sim J$には,各々のアルファベットに$0 \sim 9$が対応する.全ての和の最大値を答える.まずは各アルファベットを1にして考えて,アルファベットごとの和を出す.この和が大きいものから数字を大きい順に当てはめていく.もし0で始まってしまう場合があったらそこはswapする.

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
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <set>

#define REP(i,k,n) for(int i=k;i<int(n);i++)
#define rep(i,n) for(int i=0;i<int(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define INF 1<<30
#define mp make_pair

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

class EncodedSum {

    public:

    long long maximumSum(vector <string> numbers) {
      vector<vector<int> > s(numbers.size());
      rep(i, numbers.size()) {
          s[i].resize(numbers[i].size());
          rep(j, numbers[i].size()) {
              s[i][j] = int(numbers[i][j] - 'A');
          }
      }

      bool flag[10];
      memset(flag, 0, sizeof(flag));

      vector<ll> x(10);
      rep(i, s.size()) {
          ll t = 1;
          flag[ s[i][0] ] = true;
          for(int j = s[i].size()-1; j >= 0; j--) {
              x[ s[i][j] ] += t;
              t *= 10;
          }
      }

      vector<P> v;
      vector<int> a(10);
      rep(i, 10) {
          v.push_back(P(x[i], i));
          a[i] = 9 - i;
      }

      sort(v.begin(), v.end(), greater<P>());

      ll ans = 0;
      for(int i = v.size()-1; i >= 0; i--) {
          if(flag[ v[i].second ] && a[i] == 0) {
              swap(a[i], a[i-1]);
          }
          ans += v[i].first * a[i];
      }

      return ans;
    }
};
Feb 3rd, 2017