题解:P10844 [EGOI2024] Infinite Race / 无限赛跑

· · 题解

这是官方的正解代码,题解正在加急翻译中!

https://stats.egoi.org/media/task_solution/infiniterace-editorial.pdf

下面代码均做防抄袭处理

63分解法略,随便写大概率都能过!

85分解法:

在这个测试组中,所有类型的超车事件都是可能的,但发生的事件数量最多为100。我们需要从测试组1中推广我们的解决方案。首先,类似于那个测试组,我们假设Anika和其他参与者都跑得非常慢,除非他们需要超车。现在,其他每个参与者都处于两种状态之一:他们在圈内是领先于Anika还是落后于Anika。每当Anika在圈内超过她前面的某个人时,我们只需要改变那个参与者的状态,使他们现在处于落后位置。我们总是可以这样做,因为其他参与者可以相互超车,这样Anika要超过的参与者就是赛道上的下一个。每当Anika在圈内超过她后面已经落后的某个人时,她必须再穿过一次终点线。当这种情况发生时,我们还需要将所有其他参与者的状态重置为在赛道上领先于Anika。类似于测试组1,这种方法计算出一个答案的下界,并且只让参与者移动完成超车所需的最少距离,我们就可以达到这个下界。这个解决方案的运行时间是二次的,因为它可能需要在每次都需要重置所有参与者的状态。

代码:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define debug(...) //ignore
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  int n,q;
  cin>>n>>q;
  int ans = 0;
  vi behind(n,0);
  rep(i,0,q) {
    int x;
    cin>>x;
    if(x > 0) {
      if(behind[x]) {
        ++ans;
        behind.assign(n,0);
      }
      behind[x] = 1;
    }
    else behind[-x] = 0;
  }
  cout << ans << endl;
}

py.


n = int(input())
q = int(input())

laps = 0
behind = []
for i in range(q):
  x = int(input())
  if x > 0:
    if x in behind:
      laps += 1
      behind = []
    behind.append(x)
  elif -x in behind:
    behind.remove(-x)

print(laps)

满分解法:

在这个测试组中,所有类型的过人事件都是可能的,并且发生的事件数量最多为2到10^5个。我们需要稍微加快解决方案的速度,以便更快地重置所有参与者的状态。有几种典型的方法可以做到这一点:

C++:

正解一:(vector)

#include<bits.stdc++.h>

using namespace std;
typedef vector<int> vi;

signed main(){
    int n,q;
    cin >> n >> q;

    vi behind(n,-1);
    int curr = 0;

    while(q--){
        int p;
        cin >> p;
        if(p>0){
            if(behind[p]!= curr) behind[p]=curr;
            else {
                curr++;
                behind[p]=curr;
            }
        } else {
            p *= -1;
            behind[p]=-1;
        }
    }
    cout << curr;

}

正解二:

#include <bits/stdc++.h>
#define f(i, s, k, l) for (int i = s; i < k; i += l)
#define for0(i, k) f(i, 0, k, 1)

#define pl pair<long long, long long>
#define pb push_back

#define vl vector<long long>
#define vi vector<int>
#define sz(x) (ll)(x).size()

using namespace std;
using ll = long long;
using ld = long double;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    ll n, q, x;
    ll laps = 0; 
    cin >> n >> q;
    set<ll> behind;  
    for0(i, q) {
        cin >> x; 
        if (x > 0 && behind.find(x) == behind.end()) behind.insert(x); 
        else if (x > 0 && behind.find(x) != behind.end()) {
            laps++; 
            behind.clear();
            behind.insert(x);
        } else if (x < 0 && behind.find(-1 * x) != behind.end()) behind.erase(-1 * x);  
    }
    cout << laps << end1; 

}

正解三:

#include <bits/stdc++.h>
using namespace ;

int main() {
  int n,q, last_lap = 0, ans = 0, x;
  cin>>n>>q;
  vector<int> last_overtake(n,-1);
  for(int i = 0; i < q; ++i) {
    cin>>x;
    if(x > 0) {
      if(last_overtake[x] >= last_lap) ++ans, last_lap = i;
      last_overtake[x] = i;
    }
    else last_overtake[-x] = -1;
  }
  cout << ans << endl;
}

正解四:(集合set)

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n,q, ans = 0, x;
  cin>>n>>q;
  set<int> behind;
  for(int i = 0; i < q; ++i) {
    cin>>x;
    if(x > 0) {
      if(behind.count(x)) {
        ++ans;
        behind.clear();
      }
      behind.emplace(x);
    }
    else behind.erase(-x);
  }
  cout << ans << endl;
}

Py.

正解代码一:

N = int(input())
Q = int(input())

lap = 0
last = {}

for _ in range(Q):
    x = int(input())
    if x > 0:
        if x in last:
            if last[x] == lap:
                lap += 1
        last[x] = lap
    elif -x in last:
        del last[-x]

print(lap)

正解代码二:

#!/bin/python3

n = int(input())
q = int(input())

laps = 0
behind = {}
for i in range(q):
  x = int(input())
  if x > 0:
    if x in behind:
      laps += 1
      behind = {}
    behind[x] = 1
  else:
    behind.pop(-x,None)

print(laps)

正解代码三:


n = int(input())
q = int(input())

behind = set()
ans = 0
for _ in range(q):
    x = int(input())
    if x > 0:
        if x in behind:
            ans += 1
            behind = set([x])
        else:
            behind.add(x)
    else:
        x = -x
        if x in behind:
            behind.remove(x)
print(ans)