Submission #1078395

# Submission time Handle Problem Language Result Execution time Memory
1078395 2024-08-27T16:39:30 Z aykhn Closing Time (IOI23_closing) C++17
0 / 100
1000 ms 37672 KB
#include <bits/stdc++.h>
#include "closing.h"
 
using namespace std;

#define int long long
 
struct BIT
{
  int n;
  vector<long long> ft;
  void init(int N)
  {
    n = N + 5;
    ft.assign(n + 5, 0);
  }
  void add(int pos, long long val)
  {
    for (pos = pos + 1; pos <= n; pos += pos & -pos) ft[pos] += val;
  }
  long long get(int pos, long long res = 0)
  {
    for (pos = pos + 1; pos > 0; pos -= pos & -pos) res += ft[pos];
    return res;
  }
};
 
const int MXN = 2e5 + 5;
 
int n, x, y;
long long k;
vector<array<long long, 2>> adj[MXN];
long long d[2][MXN], p[3][MXN];
vector<int> pt, PT;
int in[MXN];

void dfs(int a, int p, int t)
{
  PT.push_back(a);
  if (a != p && a == y) pt = PT;
  for (const array<long long, 2> &v : adj[a])
  {
    if (v[0] == p) continue;
    d[t][v[0]] = d[t][a] + v[1];
    dfs(v[0], a, t);
  }
  PT.pop_back();
}
void getcomp(int a, int p, vector<int> &comp)
{
  for (array<long long, 2> &v : adj[a])
  {
    if (v[0] == p || in[v[0]]) continue;
    getcomp(v[0], a, comp);
  }
  comp.push_back(a);
}
#undef int long long
int max_score(int N, int X, int Y, long long K, vector<int> U, vector<int> V, vector<int> W)
{
  #define int long long
  n = N, x = X, y = Y, k = K;
  for (int i = 0; i < n; i++)
  {
    d[0][i] = d[1][i] = 0;
    adj[i].clear();
    in[i] = 0;
  }
  int f = 1;
  for (int i = 0; i < n - 1; i++)
  {
    adj[U[i]].push_back({V[i], W[i]});
    adj[V[i]].push_back({U[i], W[i]});
    f &= U[i] == i && V[i] == i + 1;
  }
  dfs(x, x, 0);
  dfs(y, y, 1);
  int ans = 0;
  {
    vector<long long> v;
    for (int i = 0; i < n; i++) v.push_back(min(d[0][i], d[1][i]));
    sort(v.begin(), v.end());
    long long k1 = k;
    for (int i = 0; i < n; i++)
    {
      k1 -= v[i];
      if (k1 < 0) break;
      ans++;
    }
  }
  for (int &i : pt) in[i] = 1;
  vector<int> cc[pt.size()];
  for (int i = 0; i < pt.size(); i++) 
  {
    getcomp(pt[i], pt[i], cc[i]);
    cc[i].pop_back();
  }
  for (int i = 0; i < pt.size(); i++)
  {
    for (int j = i; j < pt.size(); j++)
    {
      int cost = 0, res = pt.size() + (j - i + 1);
      for (int k = 0; k < pt.size(); k++)
      {
        if (k < i) cost += d[0][pt[k]];
        else if (k <= j) cost += max(d[0][pt[k]], d[1][pt[k]]);
        else cost += d[1][pt[k]];
      }
      if (cost > k) continue;
      vector<long long> v[2];
      for (int k = 0; k < pt.size(); k++)
      {
        if (k < i) for (int &l : cc[k]) v[0].push_back(d[0][l]);
        else if (k <= j) for (int &l : cc[k]) v[1].push_back(max(d[0][l], d[1][l]));
        else for (int &l : cc[k]) v[0].push_back(d[1][l]);
      }
      sort(v[0].begin(), v[0].end());
      sort(v[1].begin(), v[1].end());
      int l = 0, r = 0;
      while (l + 1 < v[0].size() && r < v[1].size())
      {
        int A = v[0][l] + v[0][l + 1];
        int B = v[1][r];
        if (B <= A && cost + B <= k)
        {
          cost += B;
          res += 2;
          r++;
        } 
        else if (A <= B && cost + v[0][l] <= k)
        {
          cost += v[0][l];
          res++;
          l++;
        }
        else break;
      }
      if (r == v[1].size())
      {
        for (; l < v[0].size(); l++)
        {
          if (cost + v[0][l] <= k) 
          {
            cost += v[0][l];
            res++;
          }
          else break;
        }
      }
      else
      {
        int res1 = res, res2 = res;
        int cost1 = cost, cost2 = cost;
        int r1 = r;
        for (; r < v[1].size(); r++)
        {
          if (cost1 + v[1][r] <= k)
          {
            cost1 += v[1][r];
            res1 += 2;
          }
          else break;
        }
        if (l < v[0].size()) cost2 += v[0][l], res2++;
        r = r1;
        for (; r < v[1].size(); r++)
        {
          if (cost1 + v[1][r] <= k)
          {
            cost1 += v[1][r];
            res1 += 2;
          }
          else break;
        }
        res = max({res, res1, res2});
      }
      ans = max(ans, res);
    }
  }
  return ans;
  #undef int
}

Compilation message

closing.cpp:58:12: warning: extra tokens at end of #undef directive
   58 | #undef int long long
      |            ^~~~
closing.cpp: In function 'int max_score(int, int, int, long long int, std::vector<int>, std::vector<int>, std::vector<int>)':
closing.cpp:93:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |   for (int i = 0; i < pt.size(); i++)
      |                   ~~^~~~~~~~~~~
closing.cpp:98:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   98 |   for (int i = 0; i < pt.size(); i++)
      |                   ~~^~~~~~~~~~~
closing.cpp:100:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     for (int j = i; j < pt.size(); j++)
      |                     ~~^~~~~~~~~~~
closing.cpp:103:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  103 |       for (int k = 0; k < pt.size(); k++)
      |                       ~~^~~~~~~~~~~
closing.cpp:111:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  111 |       for (int k = 0; k < pt.size(); k++)
      |                       ~~^~~~~~~~~~~
closing.cpp:120:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |       while (l + 1 < v[0].size() && r < v[1].size())
      |              ~~~~~~^~~~~~~~~~~~~
closing.cpp:120:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |       while (l + 1 < v[0].size() && r < v[1].size())
      |                                     ~~^~~~~~~~~~~~~
closing.cpp:138:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  138 |       if (r == v[1].size())
      |           ~~^~~~~~~~~~~~~~
closing.cpp:140:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  140 |         for (; l < v[0].size(); l++)
      |                ~~^~~~~~~~~~~~~
closing.cpp:155:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |         for (; r < v[1].size(); r++)
      |                ~~^~~~~~~~~~~~~
closing.cpp:164:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  164 |         if (l < v[0].size()) cost2 += v[0][l], res2++;
      |             ~~^~~~~~~~~~~~~
closing.cpp:166:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  166 |         for (; r < v[1].size(); r++)
      |                ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1071 ms 37672 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4952 KB Output is correct
3 Incorrect 2 ms 4956 KB 1st lines differ - on the 1st token, expected: '30', found: '36'
4 Halted 0 ms 0 KB -