Submission #827797

# Submission time Handle Problem Language Result Execution time Memory
827797 2023-08-16T18:58:23 Z I_Love_EliskaM_ Towns (IOI15_towns) C++17
48 / 100
13 ms 596 KB
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;
#define forn(i,n) for(int i=0;i<n;++i)
#define pb push_back
#define all(x) x.begin(), x.end()
#define pi pair<int,int>
#define f first
#define s second
const int inf=1e9+9;
     
const int _N=555;
int d[_N][_N];
 
int p1(int n) {
  
  int mx=0; 
  int f,s;
  forn(i,n) {
    if (!i) continue;
    int x=getDistance(0,i);
    if (x>mx) {
      mx=x;
      f=i;
    }
  }
 
  vector<int> a(n), b(n);
  mx=0;
  forn(i,n) {
    if (i==f) continue;
    int x=getDistance(f,i);
    a[i]=x;
    if (x>mx) {
      mx=x;
      s=i;
    }
  }
  forn(i,n) {
    if (i==s) continue;
    int x=getDistance(s,i);
    b[i]=x;
  }
 
  int ans=inf;
 
  map<int,int> cnt;
 
  set<int> s1,s2;
 
  forn(i,n) {
    if (i==f || i==s) continue;
    int x=a[i], y=b[i], z=a[s];
    int A=(x-y+z)/2;
    int B=z-A;
    int C=x-A;
    ans=min(ans,max(A,B));
    cnt[A]++;
    s1.insert(A), s2.insert(B);
  }
  vector<int> x,y;
  for(auto&t:s1) x.pb(t);
  for(auto&t:s2) y.pb(t);
  reverse(all(y));
  int left=1;
  forn(i,s1.size()) {
    if (max(x[i],y[i])==ans) {
      int a=left, b=cnt[x[i]];
      int c=n-a-b;
      if (max({a,b,c})<=n/2) return ans;
    }
    left+=cnt[x[i]];
  }
  return -ans;
 
}
 
struct DSU {
  vector<int> p,sz;
  DSU(int n) {forn(i,n)p.pb(i),sz.pb(1);}
  int get(int u) {return p[u]==u?u:get(p[u]);}
  void uni(int u, int v){
    u=get(u),v=get(v); if (u==v)return; if (sz[u]<sz[v]) swap(u,v);
    p[v]=u;sz[u]+=sz[v];
  }
};
 
int p3(int n) {
 
  forn(i,n) {
    forn(j,i) {
      int x=getDistance(i,j);
      d[i][j]=d[j][i]=x;
    }
  }
 
  int f=0,s=0;
  forn(i,n) if (d[0][i]>d[0][f]) f=i;
  forn(i,n) if (d[f][i]>d[f][s]) s=i;
 
  int ans=inf;
  map<int,int> cnt;
  map<int,vector<int>> q;
  vector<int> ok(n);
  set<int> s1,s2;
  forn(i,n) {
    if (i==f || i==s) continue;
    int x=d[f][i], y=d[s][i], z=d[f][s];
    int A=(x-y+z)/2;
    int B=z-A;
    int C=x-A;
    ans=min(ans,max(A,B));
    ok[i]=C;
    cnt[A]++;
    q[A].pb(i);
    s1.insert(A), s2.insert(B);
  }
 
  int left=1;
  vector<int> x,y;
  for(auto&t:s1)x.pb(t);
  for(auto&t:s2)y.pb(t);
  reverse(all(y));
  forn(i,s1.size()) {
    if (max(x[i],y[i])==ans) {
      int right=n-left-cnt[x[i]];
      int mx=max(left,right);
      DSU dsu(n);
      for(auto&u:q[x[i]]) {
        for(auto&v:q[x[i]]) {
          int dist = d[u][v];
          if (dist < ok[u]+ok[v]) dsu.uni(u,v);
        }
      }
      forn(j,n) mx=max(mx,dsu.sz[j]);
      if (mx<=n/2) return ans;
    }
    left+=cnt[x[i]];
  }
 
  return -ans;
}


int p5(int n) {

  vector<int> a(n), b(n), c(n);
  int f=0;
  forn(i,n) {
    a[i]=getDistance(0,i);
    if (a[i]>a[f]) f=i;
  }
  int s=0;
  forn(i,n) {
    b[i]=getDistance(f,i);
    if (b[i]>b[s]) s=i;
  }
  forn(i,n) {
    c[i]=getDistance(s,i);
  }

  set<int> st;

  vector<int> d(n);
  map<int,vector<int>> ok;

  forn(i,n) {
    if (i==f || i==s) continue;
    int x=b[i], y=c[i], z=b[s];
    int A=(x-y+z)/2;
    d[i]=x-A;
    st.insert(A);
    ok[A].pb(i);
  }

  int ans=inf;
  for(auto&x:st) {
    int y=b[s]-x;
    ans=min(ans,max(x,y));
  }
  int l=1;
  for(auto&x:st) {
    int y=b[s]-x;
    if (max(x,y)!=ans) {
      l+=ok[x].size();
      continue;
    }
    int r=n-ok[x].size()-l;
    if (max(l,r)>n/2) continue;
    int t=ok[x].size();
    if (max({l,r,t})<=n/2) return ans;

    vector<int> a,b;
    a = ok[x];
    while (a.size()>1) {
      int k = a.size();
      if (k&1) b.pb(a[0]);
      for (int i=k&1; i<a.size(); i+=2) {
        int z = getDistance(a[i],a[i+1]);
        int x = d[a[i]], y = d[a[i+1]];
        if (z==x+y) continue;
        b.pb(a[i]);
      }
      a=b;
      b.clear();
    }
    if (!a.size()) return ans;
    int u=a[0];
    int sz=0;
    for(auto&v:ok[x]) {
      int z = getDistance(u,v);
      int x = c[u], y = c[v];
      if (z<x+y) ++sz;
    }
    if (sz<=n/2) return ans;
  }

  return -ans;

}

int hubDistance(int n, int sub) {
  if (sub<=2 || sub==4) return p1(n);
  if (sub==3) return p3(n);
  if (sub==5) return p5(n);
  return 0;
}

Compilation message

towns.cpp: In function 'int p1(int)':
towns.cpp:56:9: warning: unused variable 'C' [-Wunused-variable]
   56 |     int C=x-A;
      |         ^
towns.cpp:4:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for(int i=0;i<n;++i)
......
   66 |   forn(i,s1.size()) {
      |        ~~~~~~~~~~~              
towns.cpp:66:3: note: in expansion of macro 'forn'
   66 |   forn(i,s1.size()) {
      |   ^~~~
towns.cpp:68:11: warning: declaration of 'a' shadows a previous local [-Wshadow]
   68 |       int a=left, b=cnt[x[i]];
      |           ^
towns.cpp:28:15: note: shadowed declaration is here
   28 |   vector<int> a(n), b(n);
      |               ^
towns.cpp:68:19: warning: declaration of 'b' shadows a previous local [-Wshadow]
   68 |       int a=left, b=cnt[x[i]];
      |                   ^
towns.cpp:28:21: note: shadowed declaration is here
   28 |   vector<int> a(n), b(n);
      |                     ^
towns.cpp: In function 'int p3(int)':
towns.cpp:4:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::set<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    4 | #define forn(i,n) for(int i=0;i<n;++i)
......
  124 |   forn(i,s1.size()) {
      |        ~~~~~~~~~~~              
towns.cpp:124:3: note: in expansion of macro 'forn'
  124 |   forn(i,s1.size()) {
      |   ^~~~
towns.cpp: In function 'int p5(int)':
towns.cpp:164:15: warning: declaration of 'd' shadows a global declaration [-Wshadow]
  164 |   vector<int> d(n);
      |               ^
towns.cpp:13:5: note: shadowed declaration is here
   13 | int d[_N][_N];
      |     ^
towns.cpp:185:21: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  185 |       l+=ok[x].size();
      |                     ^
towns.cpp:188:25: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  188 |     int r=n-ok[x].size()-l;
      |           ~~~~~~~~~~~~~~^~
towns.cpp:190:21: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  190 |     int t=ok[x].size();
      |           ~~~~~~~~~~^~
towns.cpp:193:17: warning: declaration of 'a' shadows a previous local [-Wshadow]
  193 |     vector<int> a,b;
      |                 ^
towns.cpp:147:15: note: shadowed declaration is here
  147 |   vector<int> a(n), b(n), c(n);
      |               ^
towns.cpp:193:19: warning: declaration of 'b' shadows a previous local [-Wshadow]
  193 |     vector<int> a,b;
      |                   ^
towns.cpp:147:21: note: shadowed declaration is here
  147 |   vector<int> a(n), b(n), c(n);
      |                     ^
towns.cpp:196:21: warning: conversion from 'std::vector<int>::size_type' {aka 'long unsigned int'} to 'int' may change value [-Wconversion]
  196 |       int k = a.size();
      |               ~~~~~~^~
towns.cpp:198:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  198 |       for (int i=k&1; i<a.size(); i+=2) {
      |                       ~^~~~~~~~~
towns.cpp:200:13: warning: declaration of 'x' shadows a previous local [-Wshadow]
  200 |         int x = d[a[i]], y = d[a[i+1]];
      |             ^
towns.cpp:182:12: note: shadowed declaration is here
  182 |   for(auto&x:st) {
      |            ^
towns.cpp:200:26: warning: declaration of 'y' shadows a previous local [-Wshadow]
  200 |         int x = d[a[i]], y = d[a[i+1]];
      |                          ^
towns.cpp:183:9: note: shadowed declaration is here
  183 |     int y=b[s]-x;
      |         ^
towns.cpp:212:11: warning: declaration of 'x' shadows a previous local [-Wshadow]
  212 |       int x = c[u], y = c[v];
      |           ^
towns.cpp:182:12: note: shadowed declaration is here
  182 |   for(auto&x:st) {
      |            ^
towns.cpp:212:21: warning: declaration of 'y' shadows a previous local [-Wshadow]
  212 |       int x = c[u], y = c[v];
      |                     ^
towns.cpp:183:9: note: shadowed declaration is here
  183 |     int y=b[s]-x;
      |         ^
towns.cpp: In function 'int p1(int)':
towns.cpp:52:18: warning: 'second' may be used uninitialized in this function [-Wmaybe-uninitialized]
   52 |     if (i==f || i==s) continue;
      |                  ^
towns.cpp:52:10: warning: 'first' may be used uninitialized in this function [-Wmaybe-uninitialized]
   52 |     if (i==f || i==s) continue;
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 10 ms 360 KB Output is correct
2 Correct 8 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 12 ms 356 KB Output is correct
5 Correct 12 ms 364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 360 KB Output is correct
2 Correct 9 ms 340 KB Output is correct
3 Correct 12 ms 352 KB Output is correct
4 Correct 12 ms 356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 596 KB Output is correct
2 Correct 13 ms 572 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 13 ms 572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -