Submission #400263

# Submission time Handle Problem Language Result Execution time Memory
400263 2021-05-07T19:05:51 Z ly20 Towns (IOI15_towns) C++17
39 / 100
27 ms 984 KB
#include "towns.h"
#include <bits/stdc++.h>
using namespace std;


const int MAXN = 120;
/*
int ds[MAXN][MAXN];
int getDistance(int a, int b) {
    return ds[a][b];
}
*/
int dist[MAXN][MAXN];
int gd(int a, int b) {
    if(dist[a][b] == 0) {
        dist[a][b] = getDistance(a, b);
        dist[b][a] = dist[a][b];
    }
    return dist[a][b];
}
int hubDistance(int n, int sub) {
	//int resp = 0;
	for(int i = 0; i < MAXN; i++) {
        for(int j = 0; j < MAXN; j++) {
            dist[i][j] = 0;
        }
	}
	int mx1 = 1, mx = 0;
	int a, b;
	for(int i = 1; i < n; i++) {
        int d = gd(1, i);
        if(d > mx) {
            mx = d; mx1 = i;
        }
	}
    a = mx1; mx = 0;
    for(int i = 0; i < n; i++) {
        if(i == a) continue;
        int d = gd(a, i);
        if(d > mx) {
            mx = d; mx1 = i;
        }
    }
    b = mx1;
    int dim = mx;
    //set <int> di;
    int resp = dim;
    vector <int> ns;
    for(int i = 0; i < n; i++) {
        if(i == a || i == b) continue;
        int d = gd(a, i) - (gd(a, i) + gd(b, i) - dim) / 2;
        resp = min(resp, max(d, dim - d));
        ns.push_back(d);
    }
    resp = resp * -1;
    int esq1 = -1, esq2 = -1;
    for(int i = 0; i < n; i++) {
        int d = gd(a, i) - (gd(a, i) + gd(b, i) - dim)/ 2;
        //printf("%d %d %d\n", i, d, dim - d);
        if(max(d, dim - d) != -resp) continue;
        //printf("oi\n");
        if(d == esq1 || d == esq2) continue;
        //printf("%d\n", i);
        if(esq1 == -1) esq1 = d;
        else esq2 = d;
        vector <int> cur;
        int ce = 0, cd = 0;
        for(int j = 0; j < n; j++) {
            int dt = gd(a, j) - (gd(a, j) + gd(b, j) - dim) / 2;
            if(dt < d) {
                ce++;
            }
            else if(dt > d) {
                cd++;
            }
            else cur.push_back(j);
        }
        if(ce > n / 2 || cd > n / 2) continue;
        if(cur.size() <= n / 2) return -resp;
        //printf("%d\n", cur.size());
        stack <int> pilha;
        for(int j = 0; j < cur.size(); j++) {
            int id = cur[j];
            if(pilha.empty()) {
                pilha.push(id);
            }
            else {
                int k = pilha.top(), l = cur[j];
                int d1 = gd(a, k) + gd(b, k) - gd(a, b), d2 = gd(a, l) + gd(b, l) - gd(a, b), db = gd(k, l);
                if(d1 + d2 != 2 * db) pilha.push(l);
                else pilha.pop();
            }
        }
        if(pilha.empty()) return -resp;
        int bs = pilha.top();
        int qt = 1;
        for(int j = 0; j < cur.size(); j++) {
            int id = cur[j];
            if(id == bs) continue;
            int k = bs, l = id;
            int d1 = gd(a, k) + gd(b, k) - gd(a, b), d2 = gd(a, l) + gd(b, l) - gd(a, b), db = gd(k, l);
            if(d1 + d2 != 2 * db) qt++;
        }
        if(qt <= n / 2) return -resp;
    }
	return resp;
}
/*
int main() {
    int n;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            scanf("%d", &ds[i][j]);
        }
    }
    printf("%d\n", hubDistance(n, 0));
    return 0;
}

*/

Compilation message

towns.cpp: In function 'int hubDistance(int, int)':
towns.cpp:79:23: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   79 |         if(cur.size() <= n / 2) return -resp;
      |            ~~~~~~~~~~~^~~~~~~~
towns.cpp:82:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for(int j = 0; j < cur.size(); j++) {
      |                        ~~^~~~~~~~~~~~
towns.cpp:97:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   97 |         for(int j = 0; j < cur.size(); j++) {
      |                        ~~^~~~~~~~~~~~
towns.cpp:21:28: warning: unused parameter 'sub' [-Wunused-parameter]
   21 | int hubDistance(int n, int sub) {
      |                        ~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 19 ms 984 KB Output is correct
2 Correct 16 ms 860 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 22 ms 900 KB Output is correct
5 Correct 27 ms 892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 304 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 17 ms 844 KB Output is correct
2 Correct 22 ms 900 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 22 ms 884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 844 KB Output is correct
2 Correct 24 ms 972 KB Output is correct
3 Correct 22 ms 892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -