QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#114540#4438. MagicLiufAC ✓11ms4524kbC++201.5kb2023-06-22 15:03:412023-06-22 15:03:43

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-06-22 15:03:43]
  • 评测
  • 测评结果:AC
  • 用时:11ms
  • 内存:4524kb
  • [2023-06-22 15:03:41]
  • 提交

answer

#include<bits/stdc++.h>

#define endl '\n'
#define pii pair<int, int>
#define int long long

using namespace std;

void solve(){
    int n,k;
    cin>>n>>k;

    vector<vector<pii>>g(n+1);

    vector<int>p(n+1);
    for(int i=1;i<=n;i++){
        cin>>p[i];
        int st=max(0ll,i-k),ed=min(n,i+k-1);
        g[st].push_back({ed,p[i]});
    }

    for(int i=1;i<=n;i++){
        g[i-1].push_back({i,0});
    }

    int que;
    cin>>que;

    for(int i=1;i<=que;i++){
        int l,r,b;
        cin>>l>>r>>b;
        g[r].push_back({l-1,-b});
    }

    deque<int>q;
    q.push_back(0);
    vector<int>vis(n+1,0),dis(n+1,-1e18),cnt(n+1,0);
    dis[0]=0;

    while(q.size()){
        int u=q.front();q.pop_front();
        vis[u]=false;

        for(auto [v,w]:g[u]){
            if(dis[v]<dis[u]+w){
                dis[v]=dis[u]+w;
                cnt[v]=cnt[u]+1;
                if(cnt[v]>n){
                    cout<<-1<<endl;
                    return;
                }
                if(!vis[v]){
                    if(!q.empty()&&dis[v]<dis[q.front()]){
                        q.push_front(v);
                    } else{
                        q.push_back(v);
                    }
                    vis[v]=1;
                }
            }
        }
    }

    cout<<dis[n]<<endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin>>t;

    while(t--){
        solve();
    }

    
    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 11ms
memory: 4524kb

input:

15
5 2
1 2 2 2 1
1
2 4 1
5 2
2 2 0 10 3
1
1 5 11
5 2
2 2 0 10 3
1
2 3 0
3 2
3 7 3
1
2 2 0
3 2
3 0 6
2
1 1 0
3 3 0
4393 1769
280 628 847 751 431 684 746 842 975 487 294 339 803 123 492 662 340 590 844 431 635 831 459 470 287 133 668 865 722 829 653 734 417 452 959 343 900 355 214 627 791 811 891 537 ...

output:

-1
-1
12
7
6
1995
2227
-1
1997
10580
6929
2000
1999
2000
3810

result:

ok 15 lines