QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#471513#8572. Passing Gameucup-team2307#TL 0ms10052kbC++203.3kb2024-07-10 21:42:222024-07-10 21:42:22

Judging History

你现在查看的是最新测评结果

  • [2024-07-10 21:42:22]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:10052kb
  • [2024-07-10 21:42:22]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize("O3")

#define fi first
#define se second
#define pb push_back

typedef long long ll;
//#define int ll

using namespace std;

const int N = 3e5+100;
const int K = 20;

vector<pair<int, ll> > g[2*N];

pair<int, int> x[N];
int x0[N];
int n, k;

int index(int x, int r)
{
    return x*2+r;
}

void add_edge(int x1, int r1, int x2, int r2)
{
    g[index(x1, r1)].pb({index(x2, r2), 2ll * x[x1].se * abs(x[x1].fi - x[x2].fi) + (r1 != r2)});
}

void add_edge(int x1, int x2)
{
    int r2 = (x[x1].fi > x[x2].fi);
    for (int r1=0; r1<2; r1++)
        add_edge(x1, r1, x2, r2);
}

ll dijkstra(int a, int b, int k)
{
    static ll cur[2*N],nxt[2*N];
    const ll INF=1e18;
    for(int i=0;i<=a;i++)
        cur[i]=nxt[i]=INF;
    cur[a]=0;
    for(int level=0;level<k;level++) {
        priority_queue<pair<ll, int>> pq;
        for(int i=0;i<=a;i++)
            cur[i]=min(cur[i],nxt[i]),
            pq.emplace(-cur[i], i),
            nxt[i]=INF;
        while (!pq.empty()) {
            auto [d, v] = pq.top();
            pq.pop();
            d = -d;
            if (d != cur[v])
                continue;
            for (auto [to, w]: g[v])
                if(w%2)
                    nxt[to]=min(nxt[to],d+w/2);
                else if (cur[to] > d + w/2)
                    cur[to] = d + w/2,
                    pq.emplace(-cur[to], to);
        }
//        for(int i=0;i<=a;i++)
//            cout<<level<<" "<<i<<" -> "<<cur[i]<<"\n";
    }
    return min(cur[2*b],cur[2*b+1]);
}

mt19937 rng(time(0));
void solve()
{
    cin>>n>>k;
    k = min(k+1, K);
    for (int i=0; i<=2*n; i++)
        g[i].clear();
    vector<pair<int, int> > v;
    for (int i=0; i<n; i++)
        cin>>x[i].fi;
    for (int i=0; i<n; i++)
        cin>>x[i].se;
//    for (int i=0; i<n; i++)
//    {
//        x[i].fi = i;
//        x[i].se = rng()%100 + 1;
//    }

    for (int i=0; i<n; i++)
        x0[i]=x[i].fi;
    sort(x, x+n);
    int a=-1, b=-1;
    for (int i=0; i<n; i++)
        if (x[i].fi == x0[0])
            a = i;
        else if (x[i].fi == x0[n-1])
            b = i;
    for (int i=0; i<n; i++)
        v.pb({x[i].se, i});

    sort(v.begin(), v.end());
    set<pair<int, int> > xs;
    for (auto [s, i] : v)
    {
//        if (i!=a)
        {
            auto it = xs.lower_bound({x[i].fi, 0});
            if (it != xs.end())
                if (it->se != b)
                {
//                    cout<<"F "<<i<<" "<<it->se<<"\n";
                    add_edge(i,  it->se);
                }
            if (it != xs.begin())
                if (prev(it)->se != b)
                {
//                    cout<<"B "<<i<<" "<<prev(it)->se<<"\n";
                    add_edge(i, prev(it)->se);
                }
        }
        xs.insert({x[i].fi, i});
    }
//    for (int i=0; i<n; i++)
//        if (i != a) add_edge(a, i);
    for (int i=0; i<n; i++)
        if (i != b) add_edge(i, b);
    g[2*n].pb({index(a, 0), 0});
    g[2*n].pb({index(a, 1), 0});

    cout<<dijkstra(2*n, b, k)<<"\n";
}

main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));

    int t;
    cin>>t;
    while (t--)
        solve();

}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 10052kb

input:

2
4 2
3 2 1 6
3 1 1 3
2 0
1 2
1 2

output:

7
1

result:

ok 2 number(s): "7 1"

Test #2:

score: -100
Time Limit Exceeded

input:

1
300000 204334
809492393 304618667 173130445 377106790 364888630 949045125 622060683 557772818 216607577 848817467 862855568 507840723 120816645 639713488 741781998 682531787 685261161 601686403 355792373 162819930 710057718 234560726 998604853 678957602 485413982 855985802 109303681 979706626 4822...

output:


result: