-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
80 lines (77 loc) · 1.4 KB
/
solution.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
#include <bits/stdc++.h>
using namespace std;
int R[10 << 10];
bool check(vector<int>& a, vector<int>& b, vector<int>& zr, vector<int>& nz, int cnt)
{
int pos = b[0] + cnt;
int value = a[0]*pos;
int n = a.size();
int p_zr = zr.size() - 1, p_nz = 0;
for(int i = 1; i <= n; ++i)
{
if(i == pos)
{
if(p_nz >= nz.size() || nz[p_nz] != 0)
return false;
R[nz[p_nz]] = i;
++p_nz;
continue;
}
if(p_nz < nz.size() && nz[p_nz] != 0 && a[nz[p_nz]]*i > value)
{
R[nz[p_nz]] = i;
++p_nz;
continue;
}
if(p_zr >= 0 && a[zr[p_zr]]*i > value)
{
R[zr[p_zr]] = i;
--p_zr;
continue;
}
return false;
}
return true;
}
int main()
{
int n;
cin >> n;
vector<int> a(n), b(n);
for(int i = 0; i < n; ++i)
cin >> a[i] >> b[i];
if(b[0] == 0)
{
for(int i = 0; i < n; ++i)
if(b[i] != 0)
++b[i];
b[0] = 1;
}
vector<pair<pair<int, int>, int>> w(n);
for(int i = 0; i < n; ++i)
w[i] = make_pair(pair<int, int>(b[i], a[i]), i);
sort(w.begin(), w.end());
vector<int> zr, nz;
for(int i = 0; i < n; ++i)
if(w[i].first.first == 0)
zr.push_back(w[i].second);
else
nz.push_back(w[i].second);
int cnt = 0;
for(int i = 0; i < n; ++i)
if(b[i] == 0)
++cnt;
bool res = false;
for(int i = 0; i <= cnt; ++i)
if(check(a, b, zr, nz, i))
{
res = true;
break;
}
if(!res)
cout << -1 << endl;
else
for(int i = 0; i < n; ++i)
cout << R[i] << endl;
return 0;
}