제출 #1011206

#제출 시각아이디문제언어결과실행 시간메모리
1011206huutuanSki 2 (JOI24_ski2)C++14
0 / 100
1901 ms472 KiB
#include<bits/stdc++.h>

using namespace std;

#define int long long

mt19937 rng(69420);

int rand(int l, int r){
   return uniform_int_distribution<int>(l, r)(rng);
}

double rand_double(int l, int r){
   return uniform_real_distribution<double>(l, r)(rng);
}

const int N=310, inf=LLONG_MAX;
int n, k, h[N], c[N];

struct State{
   int add[N];
   int value;
   void init_random(){
      for (int i=1; i<=n; ++i) add[i]=rand(0, n);
   }
   void evaluate(){
      value=accumulate(add+1, add+n+1, 0ll)*k;
      vector<pair<int, int>> v;
      for (int i=1; i<=n; ++i) v.emplace_back(h[i]+add[i], i);
      sort(v.begin(), v.end());
      if (v[0].first==v[1].first){
         value=inf;
         return;
      }
      priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
      pq.emplace(0, v[0].second);
      for (int i=1; i<n; ++i){
         int j=i;
         while (j+1<n && v[j+1].first==v[i].first) ++j;
         for (int l=i; l<=j; ++l){
            value+=pq.top().first;
            if (pq.top().first==0){
               int id=pq.top().second; pq.pop();
               pq.emplace(c[id], id);
            }
         }
         for (int l=i; l<=j; ++l) pq.emplace(0, v[l].second);
         i=j;
      }
   }
   State (){
      memset(add, 0, sizeof add);
      value=0;
   }
};

int32_t main(){
   ios_base::sync_with_stdio(false);
   cin.tie(nullptr);
   cin >> n >> k;
   if (n==1){
      cout << 0;
      return 0;
   }
   for (int i=1; i<=n; ++i) cin >> h[i] >> c[i];
   auto time=clock();
   State state, best;
   best.evaluate();
   int ans=inf;
   double temp=1e5;
   state.init_random(); state.evaluate();
   while ((clock()-time)*1000/CLOCKS_PER_SEC<1900){
      int i=rand(1, n);
      int j=0;
      if (state.add[i]==0) j=1;
      else if (state.add[i]==n) j=-1;
      else j=rand(0, 1), j-=!j;
      int old_score=state.value;
      state.add[i]+=j; state.evaluate();
      int new_score=state.value;
      ans=min(ans, new_score);
      if (new_score<old_score);
      else if (exp((old_score-new_score)/temp)>rand_double(0, 1));
      else state.add[i]-=j, state.value=old_score;
      temp*=0.999;
   }
   cout << ans;
   return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...