답안 #660901

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
660901 2022-11-23T13:58:50 Z danikoynov Palembang Bridges (APIO15_bridge) C++14
22 / 100
100 ms 5236 KB
#include <bits/stdc++.h>
#define endl '\n'

using namespace std;
typedef long long ll;

void speed()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
}

const int maxn = 1e5 + 10, smalln = 1010;

struct work
{
    ll x, y;
} w[maxn];

int k, n;
ll pref[smalln][smalln][2];

bool cmp(work w1, work w2)
{
    return w1.x + w1.y < w2.x + w2.y;
}

void solve()
{
    cin >> k >> n;
    ll ans = 0;
    vector < ll > v, sx;
    for (int i = 1; i <= n; i ++)
    {
        char c1, c2;
        cin >> c1 >> w[i].x >> c2 >> w[i].y;
        if (w[i].x > w[i].y)
            swap(w[i].x, w[i].y);
        if (c1 == c2)
        {
            ans = ans + w[i].y - w[i].x;
            i --;
            n --;
        }
        else
        {
            v.push_back(w[i].y);
            v.push_back(w[i].x);
            sx.push_back(w[i].x + w[i].y);
            ans ++;
        }
    }

    //for (int i = 1; i <= n; i ++)
      //  cout << w[i].x << " - " << w[i].y << endl;
    if (k == 1)
    {

        sort(v.begin(), v.end());
        for (int i = 0; i < v.size() / 2; i ++)
            ans = ans - (v[i]);
        for (int i = v.size() / 2; i < v.size(); i ++)
            ans = ans + v[i];

        cout << ans << endl;

    }
    else
    {
        ll best = 1e18;
        if (v.empty())
            best = 0;
        sort(w + 1, w + n + 1, cmp);
        sort(v.begin(), v.end());
        for (int i = 0; i < v.size(); i ++)
        {
            for (int j = 1; j <= n; j ++)
            {
                pref[i][j][0] = pref[i][j - 1][0] + abs(v[i] - w[j].x) + abs(v[i] - w[j].y);
            }
            for (int j = n; j > 0; j --)
            {
                pref[i][j][1] = pref[i][j + 1][1] + abs(v[i] - w[j].x) + abs(v[i] - w[j].y);
            }
        }

    for (int i = 1; i <= n; i ++)
    {
        ///cout << "pair " << w[i].x << " " << w[i].y << endl;
    }
        for (int i = 0; i < v.size(); i ++)
        {
            int pt = 1;
            for (int j = i + 1; j < v.size(); j ++)
            {
                while(pt <= n && w[pt].x + w[pt].y <= v[j])
                    pt ++;

                ll calc = 0;
                for (int k = 1; k <= pt; k ++)
                {
                    calc = calc + abs(v[i] - w[k].x) + abs(v[i] - w[k].y);
                }
                for (int k = pt + 1; k <= n; k ++)
                {
                    calc = calc + abs(v[j] - w[k].x) + abs(v[j] - w[k].y);
                }
                best = min(best, calc);
                ///cout << v[i] << " : " << v[j] << " : " << pt << " " << pref[i][pt][0] << " " << pref[j][pt][1] << endl;
                ///best = min(best, pref[i][pt - 1][0] + pref[j][pt][1]);
            }
        }
        cout << ans + best << endl;
    }


}

int main()
{
    solve();
    return 0;
}
/**
2 5
B 0 A 4
B 1 B 3
A 5 B 7
B 2 A 6
B 1 A 7
*/

Compilation message

bridge.cpp: In function 'void solve()':
bridge.cpp:61:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |         for (int i = 0; i < v.size() / 2; i ++)
      |                         ~~^~~~~~~~~~~~~~
bridge.cpp:63:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   63 |         for (int i = v.size() / 2; i < v.size(); i ++)
      |                                    ~~^~~~~~~~~~
bridge.cpp:76:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   76 |         for (int i = 0; i < v.size(); i ++)
      |                         ~~^~~~~~~~~~
bridge.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         for (int i = 0; i < v.size(); i ++)
      |                         ~~^~~~~~~~~~
bridge.cpp:95:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   95 |             for (int j = i + 1; j < v.size(); j ++)
      |                                 ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 2 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 2 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 464 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 360 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 47 ms 5228 KB Output is correct
13 Correct 100 ms 5152 KB Output is correct
14 Correct 65 ms 4756 KB Output is correct
15 Correct 60 ms 3012 KB Output is correct
16 Correct 72 ms 5236 KB Output is correct
17 Correct 89 ms 5196 KB Output is correct
18 Correct 83 ms 5160 KB Output is correct
19 Correct 97 ms 5180 KB Output is correct
20 Correct 82 ms 5144 KB Output is correct
21 Correct 87 ms 5220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 3 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 3 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Incorrect 3 ms 1236 KB Output isn't correct
4 Halted 0 ms 0 KB -