답안 #842339

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
842339 2023-09-02T18:43:08 Z flashmt 봉쇄 시간 (IOI23_closing) C++17
17 / 100
109 ms 23664 KB
#include "closing.h"
#ifdef LOCAL
#include "Debug.h"
#else
#define debug(...) 42
#endif
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const long long oo = 1LL << 62;

int n;
vector<pair<int, int>> a[N];

vector<long long> bfs(int s)
{
  vector<long long> dist(n, -1);
  queue<int> q;
  dist[s] = 0;
  q.push(s);
  while (!empty(q))
  {
    int x = q.front();
    q.pop();
    for (auto [y, w] : a[x])
      if (dist[y] < 0)
      {
        dist[y] = dist[x] + w;
        q.push(y);
      }
  }
  return dist;
}

int isLinear()
{
  for (int i = 0; i + 1 < n; i++)
  {
    int isGood = 0;
    for (auto [j, _] : a[i])
      if (j == i + 1)
        isGood = 1;
    if (!isGood)
      return 0;
  }
  return 1;
}

int max_score(int N, int A, int B, long long budget, vector<int> U, vector<int> V, vector<int> W)
{
  n = N;
  for (int i = 0; i < n; i++)
    a[i].clear();
  for (int i = 0; i < size(U); i++)
  {
    a[U[i]].push_back({V[i], W[i]});
    a[V[i]].push_back({U[i], W[i]});
  }

  auto distA = bfs(A);
  auto distB = bfs(B);
  auto distAB = distA[B];

  if (distAB > budget * 2)
  {
    vector<long long> allDists = distA;
    for (auto d : distB)
      allDists.push_back(d);
    sort(begin(allDists), end(allDists));
    int ans = 0;
    for (auto d : allDists)
      if (d <= budget)
      {
        ans++;
        budget -= d;
      }
    return ans;
  }

  if (isLinear())
  {
    vector<long long> sumA(n), sumB(n), sumAB(n);
    for (int i = A; i <= B; i++)
    {
      sumA[i] = distA[i];
      sumAB[i] = max(distA[i], distB[i]);
      if (i)
      {
        sumA[i] += sumA[i - 1];
        sumAB[i] += sumAB[i - 1];
      }
    }
    for (int i = B; i >= A; i--)
    {
      sumB[i] = distB[i];
      if (i + 1 < n)
        sumB[i] += sumB[i + 1];
    }

    vector<long long> ext;
    for (int i = 0; i < n; i++)
      if (i < A) ext.push_back(distA[i]);
      else if (i > B) ext.push_back(distB[i]);

    vector<long long> costFor(n * 2, oo);
    costFor[0] = 0;
    sort(begin(ext), end(ext));
    for (int i = 0; i < size(ext); i++)
      if (ext[i] < distAB) costFor[i + 1] = costFor[i] + ext[i];
      else
      {
        int cur = i;
        for (int j = 0; j < i; j++)
        {
          cur++;
          costFor[cur] = costFor[cur - 1] + distAB;
        }

        for (int j = i; j < size(ext); j++)
        {
          cur++;
          costFor[cur] = costFor[cur - 1] + ext[j];
          cur++;
          costFor[cur] = costFor[cur - 1] + distAB;
        }

        break;
      }

    int ans = 0;
    for (int l = A; l <= B; l++)
      for (int r = A; r <= B; r++)
      {
        long long cost = 0;
        int curAns = l - A + 1 + B - r + 1;
        if (l < r) cost = sumA[l] + sumB[r];
        else
        {
          cost = sumAB[l];
          if (r)
          {
            cost -= sumAB[r - 1];
            cost += sumA[r - 1];
          }
          if (l + 1 < n)
            cost += sumB[l + 1];
        }

        if (cost > budget)
          continue;

        int u = upper_bound(begin(costFor), end(costFor), budget - cost) - begin(costFor);
        curAns += u - 1;
        ans = max(ans, curAns);
      }

    return ans;
  }

  return 0;
}

Compilation message

closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:54:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   54 |   for (int i = 0; i < size(U); i++)
      |                   ~~^~~~~~~~~
closing.cpp:108:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  108 |     for (int i = 0; i < size(ext); i++)
      |                     ~~^~~~~~~~~~~
closing.cpp:119:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  119 |         for (int j = i; j < size(ext); j++)
      |                         ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '6', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 106 ms 23408 KB Output is correct
2 Correct 109 ms 23664 KB Output is correct
3 Correct 64 ms 7608 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4960 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4960 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Incorrect 1 ms 4956 KB 1st lines differ - on the 1st token, expected: '171', found: '147'
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4956 KB Output is correct
2 Correct 1 ms 4956 KB Output is correct
3 Correct 1 ms 4956 KB Output is correct
4 Correct 1 ms 4956 KB Output is correct
5 Correct 1 ms 4956 KB Output is correct
6 Correct 1 ms 4956 KB Output is correct
7 Correct 1 ms 4960 KB Output is correct
8 Correct 1 ms 4956 KB Output is correct
9 Correct 1 ms 4956 KB Output is correct
10 Correct 1 ms 4956 KB Output is correct
11 Correct 1 ms 4956 KB Output is correct
12 Correct 1 ms 4956 KB Output is correct
13 Correct 1 ms 4956 KB Output is correct
14 Incorrect 1 ms 4956 KB 1st lines differ - on the 1st token, expected: '171', found: '147'
15 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '6', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '6', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '6', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '6', found: '0'
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 4952 KB 1st lines differ - on the 1st token, expected: '6', found: '0'
2 Halted 0 ms 0 KB -