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
| #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#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, m;
cin >> n >> m;
vector<pair<pair<int,int>, int> > v;
rep(i, m) {
int a, b;
cin >> a >> b;
v.push_back(mp(mp(a, b), i));
}
sort(v.begin(), v.end());
vector<int> e;
vector<P> q;
vector<pair<int,pair<int,int> > > ans;
int t = 0;
rep(i, m) {
int cost = v[i].first.first;
int f = v[i].first.second;
int j = v[i].second;
if(f == 1) {
e.push_back(cost);
ans.push_back(mp(j, mp(t, t+1)));
t++;
}
else if(f == 0) {
q.push_back(mp(cost, j));
}
}
bool flag = true;
int from = 0, to = 1;
for(int i = 0; i < q.size(); i++) {
if(to == e.size()) {
flag = false;
break;
}
if(e[from] <= q[i].first && e[to] <= q[i].first) {
ans.push_back(mp(q[i].second, mp(from, to+1) ));
} else {
P p = q.back();
if(e[from] <= p.first && e[to] <= p.first) {
ans.push_back(mp(p.second, mp(from, to+1)));
q.pop_back();
}
i--;
}
if(from == to - 1) {
from = 0;
to++;
} else {
from++;
}
}
if(flag) {
sort(ans.begin(), ans.end());
rep(i, ans.size()) {
cout << ans[i].second.first+1 << " " << ans[i].second.second+1 << endl;
}
} else {
cout << -1 << endl;
}
return 0;
}
|