CFR 596 C. Wilbur and Points ( Ad hoc )
Problem - C - Codeforces
The W array given in the input serves for constraining which points can be in those positions. For any ( x, y ), its special value is y - x, and it could only be in a position i, where W[ i ] == y - x. This means, we shall count and record the keys for each point and see if the W[] shows the exact spaces for them. For the same key, for example, ( x, y ) and ( x', y' ), where y - x == y' - x', and without losing generality x ≤ x', it is obvious that y ≤ y' also holds. So for each key we need a queue, storing positions for that key, and popping positions for the smallest ( x, y ) each time.
void solve(){ int N; cin >> N; vp P( N ); for( int i = 0; i < N; ++i ) cin >> P[ i ].first >> P[ i ].second; vi W( N ); map< int, queue< int > > svp_pos; map< int, int > cnt_spv; for( int i = 0; i < N; ++i ) cin >> W[ i ], svp_pos[ W[ i ] ].push( i ), ++cnt_spv[ W[ i ] ]; map< int, set< pii > > spv2p; for( int i = 0; i < N; ++i ){ int key = P[ i ].second - P[ i ].first; spv2p[ key ].insert( P[ i ] ); if( spv2p[ key ].size() > cnt_spv[ key ] ){ cout << "NO\n"; return; } } vp ans( N ); for( auto t : spv2p ){ for( pii p : t.second ) ans[ svp_pos[ t.first ].front() ] = p, svp_pos[ t.first ].pop(); } for( int i = 0; i + 1 < N; ++i ){ if( ans[ i ].first >= ans[ i + 1 ].first and ans[ i ].second >= ans[ i + 1 ].second ){ cout << "NO\n"; return; } } cout << "YES\n"; for( int i = 0; i < N; ++i ) cout << ans[ i ].first << " " << ans[ i ].second << "\n"; }