-
Notifications
You must be signed in to change notification settings - Fork 1
/
OIG1_SKL.cpp
executable file
·178 lines (125 loc) · 2.98 KB
/
OIG1_SKL.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// #include <iostream>
// #include <map>
// #include <queue>
// #include <stdint.h>
// #define FOR(i,a,b) for(int i = a ; i < b ; ++i )
// using namespace std;
// int main(){
// int t;
// int n;
// int k;
// int result = 0;
// vector <int> que;
// map <int32_t,int32_t> towary;
// ios_base::sync_with_stdio();
// scanf("%d\n",&t);
// FOR( i, 0 , t ){
// scanf("%d%d",&n,&k);
// if ( towary[ n ] == 0 ){
// result ++;
// que.push_back(n);
// towary[ n ] = k;
// }
// else{
// towary[ n ] += k;
// }
// }
// printf("%d\n",result);
// for( vector<int>::iterator it = que.begin(); it != que.end() ; it++){
// printf("%d %d\n", *it ,towary[(*it)]);
// }
// return 0 ;
// }
// MAIN.EDU.PL Solution
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <map>
#include <queue>
#include <cstdio>
#include <stdint.h>
#define FOR(i,a,b) for(int i = a ; i < b ; ++i )
using namespace std;
struct temp{ int towar; short int ilosc; int pozycja; };
void quicksort( temp tab[],int start, int stop);
int partition( temp tab[],int start, int stop);
temp tab[1000001];
int main(){
srand(time(NULL));
int t;
int n;
short int k;
int result = 0;
vector < pair<int,int> > que;
ios_base::sync_with_stdio();
scanf("%d\n",&t);
FOR( i, 0 , t ){
scanf("%d%hd",&n,&k);
pair<int,int> elem;
elem.first = n;
elem.second = 0;
que.push_back(elem);
tab[i].pozycja = i ;
tab[i].towar = n ;
tab[i].ilosc = k ;
}
tab[t].towar = -1;
tab[t].pozycja = -1;
tab[t].ilosc = -1;
quicksort(tab,0,t-1);
int sum = 0;
int min = 0;
FOR(i,0, t+1){
if( i == 0 ){
sum = tab[i].ilosc;
min = tab[ i ].pozycja;
}
else if( tab[ i ].towar == tab[ i - 1 ].towar){
if( tab[ i ].pozycja < min )
min = tab[ i ].pozycja;
sum += tab[ i ].ilosc;
}
else{
result++;
que[ min ].second = sum;
min = tab[ i ].pozycja;
sum = tab[ i ].ilosc;
}
}
printf("%d\n",result);
FOR( i , 0 , t )
if( que[ i ].second != 0 )
printf("%d %d\n", que[i].first,que[i].second);
return 0 ;
}
void quicksort( temp tab[],int start, int stop){
if( start >= stop )
return ;
int r = rand()%(stop-start)+start;
swap( tab[ r ],tab[ start ]);
int first = tab[start].towar;
int f = start;
int s = stop;
int i = start+1;
while( i <= s){
if( tab[ i ].towar < first ){
f++;
swap(tab[ i ],tab[ f ]);
i++;
}
else if( tab[ i ].towar > first ){
swap(tab[ i ], tab[ s ]);
s--;
}
else{
i++;
}
}
swap(tab[start],tab[ f ]);
quicksort( tab , start, f-1);
quicksort( tab, s+1, stop );
}
int partition( temp tab[],int start, int stop){
int pos = 0;
return pos;
}