-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path860C.cpp
127 lines (127 loc) · 4.1 KB
/
860C.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define FOR0(i,n) for(int i=0, _##i=(n); i<_##i; ++i)
#define FOR(i,l,r) for(int i=(l), _##i=(r); i<_##i; ++i)
#define FORD(i,l,r) for(int i=(r), _##i=(l); --i>=_##i; )
#define repi(i,a) for(__typeof((a).begin()) i=(a).begin(), _##i=(a).end(); i!=_##i; ++i)
#define dwni(i,a) for(__typeof((a).rbegin()) i=(a).rbegin(), _##i=(a).rend(); i!=_##i; ++i)
#define SZ(a) ((int)((a).size()))
#define printCase() "Case #" << caseNum << ": "
#define pb push_back
#define mp make_pair
#define EPS (1e-9)
#define PI 3.1415926535
#define INF ((int)1e9)
#define SYNC std::ios::sync_with_stdio(false); cin.tie(NULL);
#define ff first
#define ss second
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define N 100005
int toNum(string& s) {
int ret = 0;
FOR0(i, s.length()) {
if(s[i] < '0' || s[i] > '9') return -1;
ret = ret*10 + s[i] - '0';
if(!ret) return -1;
}
return ret;
}
int main() {
SYNC
int n, e = 0, type[N], num[N];
string s[n];
bitset<N> done, occ;
cin >> n;
FOR0(i,n) {
cin >> s[i] >> type[i];
e += type[i];
num[i] = toNum(s[i]);
}
set<int> avail[2];
set<ii> other;
vector<pair<string, string>> out;
FOR0(i, n) {
if(type[i]) {
if(num[i] == -1 || num[i] > n) continue;
if(num[i] <= e) done[num[i]] = 1;
else {
other.insert(mp(1, i));
occ[num[i]] = 1;
}
} else {
if(num[i] == -1 || num[i] > n) continue;
if(num[i] > e) done[num[i]] = 1;
else {
other.insert(mp(0, i));
occ[num[i]] = 1;
}
}
}
FOR(i,1,e+1) if(!done[i] && !occ[i]) avail[1].insert(i);
FOR(i,e+1,n+1) if(!done[i] && !occ[i]) avail[0].insert(i);
int change = true;
while(other.size() && change) {
change = false;
ii cur;
cur = *(other.begin());
if(avail[!cur.ff].size()) {
out.pb(mp(to_string(num[cur.ss]),to_string(*avail[!cur.ff].begin())));
change = true;
done[*avail[!cur.ff].begin()] = 1;
avail[!cur.ff].erase(avail[!cur.ff].begin());
other.erase(other.begin());
}
}
change = true;
while(other.size() && change) {
change = false;
ii cur;
cur = *(other.rbegin());
if(avail[!cur.ff].size()) {
out.pb(mp(to_string(num[cur.ss]),to_string(*avail[!cur.ff].begin())));
change = true;
done[*avail[!cur.ff].begin()] = 1;
avail[!cur.ff].erase(avail[!cur.ff].begin());
other.erase(*other.rbegin());
}
}
if(other.size()) {
while(other.size() && change) {
while(other.size() && change) {
change = false;
ii cur;
cur = *(other.begin());
if(avail[!cur.ff].size()) {
out.pb(mp(to_string(num[cur.ss]),to_string(*avail[!cur.ff].begin())));
change = true;
done[*avail[!cur.ff].begin()] = 1;
avail[!cur.ff].erase(avail[!cur.ff].begin());
other.erase(other.begin());
}
}
if(change) continue;
change = true;
while(other.size() && change) {
change = false;
ii cur;
cur = *(other.rbegin());
if(avail[!cur.ff].size()) {
out.pb(mp(to_string(num[cur.ss]),to_string(*avail[!cur.ff].begin())));
change = true;
done[*avail[!cur.ff].begin()] = 1;
avail[!cur.ff].erase(avail[!cur.ff].begin());
other.erase(*other.rbegin());
}
}
}
}
cout << out.size() << '\n';
for(auto& cur : out) cout << "move " << cur.ff << " " << cur.ss << '\n';
}