1
+ // problem:
2
+ // https://s3-us-west-2.amazonaws.com/prod-runcodes-files/exercisefiles/14552/p10131.pdf?X-Amz-Content-Sha256=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAI3OUHGVPPSQMGO5Q%2F20200517%2Fus-west-2%2Fs3%2Faws4_request&X-Amz-Date=20200517T182414Z&X-Amz-SignedHeaders=Host&X-Amz-Expires=120&X-Amz-Signature=ec4ec7d41dde38e46f3cb6a81112e5a8fca7a722dbe9ba4602681a0bb5712fb8
3
+ #include < iostream>
4
+ #include < vector>
5
+ #include < map>
6
+ #include < queue>
7
+ #include < string>
8
+ #include < string.h> // memset
9
+ #include < algorithm> // lower_bound
10
+ #include < bitset>
11
+
12
+ using namespace std ;
13
+
14
+ #define fs first
15
+ #define sd second
16
+ #define pb push_back
17
+ // #define mp make_pair
18
+ #define MAXN 100001
19
+
20
+ const int INF = 0x3f3f3f3f ;
21
+ // priority_queue<pair<int, vector<pair<int, int>>>> elem;
22
+ #define M 10001
23
+
24
+ struct Elephant {
25
+ int id,
26
+ w,
27
+ s;
28
+ };
29
+
30
+ bool mySort (const Elephant& a, const Elephant& b){
31
+ return (a.w != b.w )? (a.w < b.w ) : (a.s > b.s );
32
+ }
33
+
34
+ bitset<MAXN> dp;
35
+ Elephant elem[MAXN];
36
+
37
+
38
+ int lis (int n) {
39
+ vector<pair<int , int >> v;
40
+ vector<int > topo, // QI
41
+ ans; // subsequence of elephantes
42
+ vector<int >::iterator it;
43
+ int ultimoTopo;
44
+
45
+ /* ************************/
46
+ // primeira iteração, inicializa topo
47
+
48
+ // cout << v[0].first << ' ' << v[0].second << endl;
49
+
50
+ // armazeno as tres informações ordenadas pelo maior peso
51
+ topo.pb (elem[0 ].s );
52
+ ultimoTopo = topo.back ();
53
+ /* ************************/
54
+
55
+ for (int i = n - 1 ; i >= 0 ; --i) {
56
+ // procuro a maior subsequencia crescente em QI
57
+ it = lower_bound (topo.begin (), topo.end (), elem[i].s );
58
+ // se o peso ainda nao foi escolhido
59
+ if (!dp[elem[i].w ]) {
60
+ // crio uma nova pilha se o elemnto eh maior que o topo das anteriores
61
+ if (it == topo.end ()) {
62
+ dp[elem[i].w ] = 1 ; // marco o peso como escolhido
63
+ ans.pb (topo.back ()); // o topo da ultima pilha faz parte da subsequencia
64
+ topo.pb (elem[i].s ); // topo da nova pilha
65
+ ultimoTopo = elem[i].s ; // o ultimo elemento da sequencia
66
+ } else {
67
+ *it = elem[i].s ; // atualizo topo
68
+ }
69
+ }
70
+
71
+ }
72
+
73
+ // ultima iteração, atualiza resposta
74
+ ans.pb (ultimoTopo);
75
+
76
+ // reverse(ans.begin(), ans.end());
77
+
78
+ // o tamanho da sequencia é
79
+ cout << (int )topo.size () << endl;
80
+
81
+ // a sequencia é
82
+ int i = 0 ;
83
+ while (ans.size ()) {
84
+ int qi = ans.back ();
85
+ while (i < n) {
86
+ if (elem[i].s == qi) {
87
+ cout << elem[i].id << endl;
88
+ break ;
89
+ }
90
+ ++i;
91
+ }
92
+ ans.pop_back ();
93
+ }
94
+
95
+ return 0 ;
96
+ }
97
+
98
+ int main ()
99
+ {
100
+ ios_base::sync_with_stdio (false );
101
+ cin.tie (NULL );
102
+
103
+ int n = 0 , a, b, i = 0 ;
104
+ while (cin >> elem[n].w >> elem[n].s ) {
105
+ elem[n].id = n + 1 ;
106
+ ++n;
107
+ }
108
+
109
+ lis (n);
110
+
111
+ return 0 ;
112
+ }
0 commit comments