原题连接
https://codeforces.com/problemset/problem/1875/D
本来想去练一下dp,然后找了一道纯dp的1600的做。
由于本人完全没学过dp。
做着做着发现不对劲,显示想了个子集枚举的暴力,枚举有哪些数字被选择删除
复杂度2^5000
当场吓傻。
然后定睛一看,有点像图论。
这不是就MEX到0的最短路吗
卧槽
写了个dijkstra秒了。
时间复杂度nlogn
官方题解是O(n^2)的
燃起来了,看来刷了十几道最短路还是有用的。
厉害
respect bro
虽然看起来简单,但是我还是想了挺久的
#include<bits/stdc++.h> using namespace std; #define int long long #define PII pair<int,int> #define INF 1e9 const int N=1e6; void slove(){ int n; cin>>n; map<int,int> m; int a; for(int i=0;i<n;i++){ cin>>a; m[a]++; } int x=0; while(m.count(x))x++; vector<int> dist(n+1,INF); vector<bool> flag(n+1); dist[x]=0; priority_queue<PII,vector<PII>,greater<PII>> q; q.push({0,x}); while(q.size()){ int u=q.top().second; q.pop(); if(flag[u])continue; flag[u]=1; for(int i=u-1;i>=0;i--){ if(dist[i]>dist[u]+m[i]*u){ dist[i]=dist[u]+m[i]*u; q.push({dist[i],i}); } } } cout<<dist[0]-x<<endl; } signed main(){ int t; cin>>t; while(t--){ slove(); } }