D
预处理,ans[i]记录一a[i]后面第一个与a[i]相等的值(如果没有,则需要特殊判断或者初始化成一个极大值)
每次询问时,可以直接看ans[l]是否大于r,如果大于则不存在,如果小于则存在
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>using namespace std;
typedef long long LL;
#define int long long
typedef pair<int,int> PII;
const int N=1000010;
int a[N];
int n,m;void solve(){int k;cin>>n;queue<int>q;std::vector<int>ans(n+1);for(int i=1;i<=n;i++){cin>>a[i];while(q.size() && a[q.front()]!=a[i]){int t=q.front();q.pop();ans[t]=i;}q.push(i);} cin>>k;while(k--){int l,r;cin>>l>>r;if(ans[l]>r || ans[l]==0){cout<<-1<<" "<<-1<<endl;}else{cout<<l<<" "<<ans[l]<<endl;}}cout<<endl;}
signed main(){int t=1;cin>>t;while(t--){solve();}
}
E
构造
一眼看上去没有头绪,实际上手玩一下样例即可
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>using namespace std;
typedef long long LL;
#define int long long
typedef pair<int,int> PII;
const int N=1000010;
int a[N],ans[N];
int n,m;void solve(){int k;cin>>n>>k;int res1=n,res2=1;for(int i=1;i<=k;i++){if(i&1){int now=res1;for(int j=0;i+j*k<=n;j++){ans[i+j*k]=now--;}res1=now;}else{int now=res2;for(int j=0;i+j*k<=n;j++){ans[i+j*k]=now++;}res2=now;}}for(int i=1;i<=n;i++) cout<<ans[i]<<" ";cout<<endl;}
signed main(){int t=1;cin>>t;while(t--){solve();}
}
F
类似于最小生成树的求法
将边的从大向小排序
用并查集判断是否成环
如果成环记录一下此时的起点start和终点end以及边长w
然后bfs一下,求出起点到各个点的最短距离,根据终点反推出整个路径
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<array>
using namespace std;
typedef long long LL;
#define int long long
typedef pair<int,int> PII;
const int N=1000010;
array<int,3> a[N];
std::vector<int> v[N];
int dist[N];
int f[N];
int find(int x){if(f[x]!=x) return f[x]=find(f[x]);return f[x];
}
void solve(){int n,m;cin>>n>>m;for(int i=1;i<=n;i++) f[i]=i,v[i].clear(),dist[i]=1e18;for(int i=1;i<=m;i++){cin>>a[i][0]>>a[i][1]>>a[i][2];v[a[i][1]].push_back(a[i][0]);v[a[i][0]].push_back(a[i][1]);}sort(a+1,a+1+m,[&](array<int,3> x,array<int,3> y){return x[2]>y[2];});int ans=1e18;int start=-1,end=-1;for(int i=1;i<=m;i++){int xx=find(a[i][0]);int yy=find(a[i][1]);if(xx==yy && a[i][2]<ans){ans=a[i][2];start=a[i][0];end=a[i][1];}f[xx]=yy;}queue<int>q;q.push(start);dist[start]=0;while(q.size()){int t=q.front();q.pop();for(auto c:v[t]){if(t==start && c==end) continue;if(c==start) continue;if(dist[c]>dist[t]+1){dist[c]=dist[t]+1;q.push(c); }}}int dis=dist[end];int now=end;vector<int>arr;while(dis){arr.push_back(now);for(auto c:v[now]){if(dist[c]==dis-1){now=c;dis--;break;}}}arr.push_back(start);cout<<ans<<" "<<arr.size()<<endl;for(auto c:arr){cout<<c<<" ";}cout<<endl;}
signed main(){int t=1;cin>>t;while(t--){solve();}
}