Submission #45530


ソースコード

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "bits/stdc++.h" // {{{
using namespace std; typedef long long ll; typedef pair<int,int> pii;
#define val const auto
#define eb emplace_back
#define emp emplace
#define fi first
#define se second
#define outl(x) cout << (x) << '\n'
#define rep(i,n) for(int i=0; i < (int)(n); ++i)
#define repr(i,h,l) for(int i=(h); i >= (int)(l); --i)
#define reps(i,s,t) for(int i=(s); i <= (int)(t); ++i)
#define ALL(x) begin(x), end(x)
#define TMPLT(T,U) template<class T, class U>
#define ten(p) ((long long)(1e##p))
#define FILL(a,v) memset((a), (v), sizeof(a))
#ifdef DEBUG
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define show(x) clog << #x << " \t\t= " << (x) << '\n'
#define show2(x,y) clog << '(' << #x << ',' << #y << ")\t\t= " << '(' << (x) << "\t, " << (y) << ")\n"
#define LN() fputs("\n--------------------------------\n", stderr)
#else
#define debug(...)
#define show(x)
#define show2(x,y)
#define LN()
#endif
#define def(op) inline bool operator op (const T &that) const { return comp(that) op 0; }
template<class T> struct Ordered
{ virtual int comp(const T &that) const = 0; def(==); def(!=); def(<); def(<=); def(>); def(>=); };
#undef def
namespace {
constexpr int INF = 0x3f3f3f3f; constexpr ll LINF = 0x3f3f3f3f3f3f3f3fLL; ostringstream _oss;
TMPLT(T,U) inline constexpr common_type_t<T,U> gcd(T x, U y)
{ return (x<y)? gcd(y,x) : (y <= 0)? x : gcd(y, x % y); }
TMPLT(T,U) inline bool chmax(T &a, const U &b){return b>a ? a=b,1 : 0;}
TMPLT(T,U) inline bool chmin(T &a, const U &b){return b<a ? a=b,1 : 0;}
TMPLT(T,U) inline constexpr ll lcm(T x, U y) { return (ll)x/gcd(x,y) * y; }
template<class T> string toString(const T &x){ _oss.str(""); _oss << x; return _oss.str(); }
template<class Itr> string mkString(Itr begin, Itr end, const char *sp = " ") {
_oss.str(""); for(Itr i=begin; i != end; ++i) { if(i != begin)_oss << sp; _oss << *i; }
return _oss.str();
}
// }}}
constexpr int MX = ten(5) + 10;
int N, M;
int a[MX];
int sum[22][MX];
// [s, t)
inline int calcSum(int k, int s, int t) { return sum[k][t-1] - sum[k][s-1]; }
int dp[1 << 21];
int slv(int bit, int tail)
{
if(bit == (1 << M)-1) { return 0; }
int &ret = dp[bit];
if (~ret) { return ret; }
ret = INF;
rep(k, M) {
if ((bit >> k) & 1) continue;
val n = sum[k][N]; // 種類 k のぬいぐるみの総数
val nxtTail = tail + n;
val ncos = (n - calcSum(k, tail, nxtTail));
chmin(ret, slv( bit | (1<<k), nxtTail) + ncos);
}
return ret;
}
void Xx_main_xX(const vector<char*> &args)
{
cin >> N >> M;
reps(i, 1, N) {
cin >> a[i];
--a[i];
++sum[a[i]][i];
}
rep(k, M) {
reps(i, 1, N) { sum[k][i] += sum[k][i-1]; }
}
FILL(dp, -1);
int ans = slv(0, 1);
outl(ans);
}
} // {{{
signed main(signed argc, char *argv[]) {
#ifndef DEBUG
ios::sync_with_stdio(false); cin.tie(nullptr);
#endif
cout << fixed << setprecision(9); Xx_main_xX(vector<char*>(argv, argv+argc)); return 0;
} // }}}

ステータス

項目 データ
問題 0595 - ぬいぐるみの整理 (Plush Toys)
ユーザー名 Arumakan_ei1727
投稿日時 2018-12-06 23:18:29
言語 C++14
状態 Accepted
得点 100
ソースコード長 3158 Byte
最大実行時間 136 ms
最大メモリ使用量 17292 KB

セット

セット 得点 Cases
1 set1 20 / 20 *t4*1.txt
2 set2 20 / 20 *t4*2.txt
3 set3 20 / 20 *t4*3.txt
4 set4 20 / 20 *t4*4.txt
5 set5 20 / 20 *t4*5.txt

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
2017-yo-t4-in1.txt AC 28 ms 10844 KB
1
2017-yo-t4-in2.txt AC 26 ms 12976 KB
2
2017-yo-t4-in3.txt AC 31 ms 13304 KB
3
2017-yo-t4-in4.txt AC 69 ms 16972 KB
4
2017-yo-t4-in5.txt AC 136 ms 17292 KB
5