結果

問題 No.953 席
ユーザー chocorusk
提出日時 2019-12-16 02:03:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 3,162 bytes
コンパイル時間 1,410 ms
コンパイル使用メモリ 133,716 KB
実行使用メモリ 16,156 KB
最終ジャッジ日時 2024-07-02 18:51:45
合計ジャッジ時間 9,402 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
#define int long long
using namespace std;
//typedef long long int ll;
typedef pair<int, int> P;
signed main()
{
	int n, k1, k2;
  cin>>n>>k1>>k2;
  int q;
  cin>>q;
  int a[100010], b[100010];
  using Pi=pair<P, int>;
  priority_queue<Pi, vector<Pi>, greater<Pi>> v;
  vector<Pi> vec;
  for(int i=0; i<q; i++){
    cin>>a[i]>>b[i];
  }
  queue<int> que;
  int cnt=0;
  priority_queue<int, vector<int>, greater<int>> pque;
  for(int i=0; i<q; i++){
    while(!pque.empty()){
      int x=pque.top();
      if(x>a[i]) break;
      cnt--;
      pque.pop();
      if(!que.empty()){
        int j=que.front();
        que.pop();
        a[j]=x;
        cnt++;
        pque.push(a[j]+b[j]);
      }
    }
    if(cnt<n){
      cnt++;
      pque.push(a[i]+b[i]);
    }else{
      que.push(i);
    }
  }
  while(!pque.empty()){
      int x=pque.top();
      cnt--;
      pque.pop();
      if(!que.empty()){
        int j=que.front();
        que.pop();
        a[j]=x;
        cnt++;
        pque.push(a[j]+b[j]);
      }
  }
  for(int i=0; i<q; i++){
    //cout<<a[i]<<endl;
    v.push({{a[i], i+q}, 0});
    v.push({{a[i]+b[i], i}, 1});
  }
  set<int> st1, st2;
  bool used[100010]={};
  for(int i=0; i<=n+1; i++){
    st1.insert(i);
    st2.insert(i);
  }
  queue<int> myon;
  int ans[100010];
  while(!v.empty()){
    auto p=v.top(); v.pop();
    int t=p.second;
    int i=p.first.second;
    if(t==0){
      i-=q;
      auto itr=st1.lower_bound(max(k1, k2));
      int x=*itr;
      itr--;
      int y=*itr;
      if(x==n+1 && y==0){
        auto itr2=st2.lower_bound(max(k1, k2));
        int x2=*itr2;
        itr2--;
        int y2=*itr2;
        if(x2==n+1 && y2==0){
          //cout<<i<<" "<<p.first.first<<endl;
          assert(0);
        }else if(x2==n+1){
          x=y2;
        }else if(y2==0){
          x=x2;
        }else{
          if(x2-max(k1, k2)>min(k1, k2)-y2){
            x2=y2;
          }else if(k1<k2 && x2-k2==k1-y2){
            x2=y2;
          }
          x=x2;
        }
      }else if(x==n+1){
        x=y;
      }else if(y==0){
        
      }else{
        if(x-max(k1, k2)>min(k1, k2)-y){
          x=y;
        }else if(k1<k2 && x-k2==k1-y){
          x=y;
        }
      }
      ans[i]=x;
      st2.erase(x);
      st1.erase(x);
      if(x>1) st1.erase(x-1);
      if(x<n) st1.erase(x+1);
      used[x]=1;
    }else{
      int x=ans[i];
      used[x]=0;
      st2.insert(x);
      if(x-1>0){
        if(!used[x-1] && !used[x-2]) st1.insert(x-1);
      }
      if(x+1<=n){
        if(!used[x+1] && !used[x+2]) st1.insert(x+1);
      }
      if(!used[x-1] && !used[x+1]) st1.insert(x);
    }
  }
  for(int i=0; i<q; i++){
    cout<<ans[i]<<endl;
  }
	return 0;
}
0