#include <bits/stdc++.h>
#define fio ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define int long long
using namespace std ;
bool cmp ( pair<pair<int,int>,int> x , pair<pair<int,int>,int> y )
{
if ( x.first.second == y.first.second ) return x.first.first > y.first.first ;
return x.first.second > y.first.second ;
}
bool com ( pair<pair<int,int>,int> x , pair<pair<int,int>,int> y )
{
return x.second < y.second ;
}
int32_t main ()
{
fio
int n , ans = 0 ;
cin >> n ;
pair <pair<int,int>,int> a[n] ;
for ( int i = 0 ; i < n ; i++ )
{
cin >> a[i].first.first ;
cin >> a[i].first.second ;
a[i].second = i ;
}
sort ( a , a+n , cmp ) ;
int i = 0 , j = n-1 ;
while ( i < j )
{
int k = a[i].first.second - a[i].first.first ;
if ( k > a[j].first.first )
{
a[i].first.first += a[j].first.first ;
a[j].first.first = 0 ;
j-- ;
}
else if ( k < a[j].first.first )
{
a[i].first.first += k ;
i++ ;
a[j].first.first -= k ;
}
else if ( k == a[j].first.first )
{
a[i].first.first += k ;
i++ ;
a[j].first.first = 0 ;
j-- ;
}
}
for ( auto it : a ) if ( it.first.first == 0 ) ans++ ;
cout << ans << "\n" ;
sort ( a , a+n , com ) ;
for ( auto it : a ) cout << it.first.first << " " ;
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |