题目:meeting
题意:给你一棵树,再给你这棵树上的一些点,需要找到这些点能可以在一个点聚会的最短时间。
思路:随便找到一个点,搜索这个点能够到达最远的点,然后再用最远的点,搜索能够到达最远的距离。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int nex[maxn<<1], to[maxn<<1], h[maxn<<1];
int is[maxn];
int n, m, mx, point, tot = 0;
void add(int x, int y){
to[tot] = y;
nex[tot] = h[x];
h[x] = tot++;
}
void dfs(int x, int fa, int dis){
if(dis > mx && is[x]){
mx = dis;
point = x;
}
for(int i = h[x]; ~i; i = nex[i]){
int t = to[i];
if(t == fa)continue;
dfs(t, x, dis+1);
}
}
int main(){
scanf("%d %d", &n, &m);
memset(h, -1, sizeof h);
for(int i = 1; i < n; i++){
int a, b;
scanf("%d %d",&a, &b);
add(a, b);
add(b, a);
}
int x;
for(int i = 1; i <= m; i++){
scanf("%d", &x);
is[x] = 1;
}
mx = 0;
dfs(x, 0, 0);
dfs(point, 0, 0);
printf("%d\n", (mx+1)/2);
return 0;
}
题目:free
题意:给你一个图,从S点到T点,你最多可以有k条边的路可以设置为0,问你从S点到T点最少花费。
思路:本题就是一个分层最短路的裸体。
代码:
#include<bits/stdc++.h>
using namespace std;
#define pa pair<int, pair<int, int> >
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+10;
int dis[maxn][maxn], h[maxn<<1], to[maxn<<1], val[maxn<<1], nex[maxn];
int tot = 0, vis[maxn][maxn];
inline int read()
{
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9')
{
if(ch=='-')f=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*f;
}
void add(int x, int y, int v){
val[tot] = v;
to[tot] = y;
nex[tot] = h[x];
h[x] = tot++;
}
int n, m, k, s, t;
void Dijkstra(){
priority_queue<pa, vector<pa>, greater<pa> >q;
memset(dis, 0x3f, sizeof dis);
q.push(make_pair(0,make_pair(s, 0)));
dis[s][0] = 0;
while(!q.empty()){
int x = q.top().second.first,kx = q.top().second.second;
q.pop();
if(vis[x][kx])continue;
vis[x][kx] = 1;
for(int i = h[x]; ~i; i = nex[i]){
int y = to[i], v = val[i];
if(dis[x][kx] + v < dis[y][kx]){
dis[y][kx] = dis[x][kx] + v;
q.push(make_pair(dis[y][kx], make_pair(y, kx)));
}
if(kx + 1 <= k && dis[x][kx] < dis[y][kx+1]){
dis[y][kx+1] = dis[x][kx];
q.push(make_pair(dis[y][kx+1],make_pair(y, kx+1)));
}
}
}
}
int main(){
n = read();m = read();s = read();t = read();k = read();
memset(h, -1, sizeof h);
for(int i = 1; i <= m; i++){
int a, b, v;
a = read(), b = read(), v = read();
if(a == b)continue;
add(a, b, v);
add(b, a, v);
}
Dijkstra();
int ans = INF;
for(int i = 0; i <= k; i++){
ans = min(ans, dis[t][i]);
//cout<<dis[t][i]<<endl;
}
printf("%d\n", ans);
return 0;
}
题目:sequence
题意:题意就是给你两个数组a和b,然后问你找一个区间[l, r]使a数组的最小值乘以b数组的和最大。
思路:我们分开讨论a, b数组,先讨论a数组的区间最小值,我们可以把每个数看做最小值,然后通过单调栈去寻找他的左右区间。那么b数组怎么维护呢,我们可以用线段树维护前缀和最大的和最小值,通过前缀和求区间的和。
#include<bits/stdc++.h>
using namespace std;
#define LL long long
const int MAXN=3e6+100;
LL l[MAXN], r[MAXN];
LL st[MAXN<<2], ss[MAXN<<2];
LL a[MAXN];
LL sum[MAXN];
void pushup(int o)
{
st[o]=max(st[o<<1], st[(o<<1)|1]);
ss[o]=min(ss[o<<1], ss[o<<1|1]);
}
void build(int o,int l,int r)
{
if(l==r){
st[o]=ss[o]=sum[l];
return;
}
else
{
int m=l+((r-l)>>1);
build(o<<1,l,m);
build((o<<1)|1,m+1,r);
pushup(o);
}
}
LL query(int o,int l,int r,int ql,int qr, int type)
{
if(ql<=l&&qr>=r)
{
if(type)return st[o];
else return ss[o];
}
int m=l+((r-l)>>1);
LL ans;
if(type)
{
ans = -1e13;
if(ql<=m)ans=max(ans,query(o<<1,l,m,ql,qr,type));
if(qr>=m+1)ans=max(ans,query(o<<1|1,m+1,r,ql,qr,type));
return ans;
}
else
{
ans = 1e13;
if(ql<=m)ans=min(ans,query(o<<1,l,m,ql,qr,type));
if(qr>=m+1)ans=min(ans,query(o<<1|1,m+1,r,ql,qr,type));
return ans;
}
}
int main()
{
int n, m;
scanf("%d", &n);
for(int i = 1; i <= n; i++)scanf("%lld", &a[i]);
for(int i = 2; i <= n+1; i++)
{
scanf("%lld", &sum[i]);
sum[i] += sum[i-1];
}
build(1, 1, n+1);
a[0] = a[n+1] = -1e13;
stack<LL>s;
s.push(0);
for(int i = 1; i <= n; i++)
{
while(a[s.top()] >= a[i])s.pop();
l[i] = s.top();
s.push(i);
}
s.push(n+1);
for(int i = n; i >= 1; i--)
{
while(a[s.top()] >= a[i])s.pop();
r[i] = s.top();
s.push(i);
}
LL ans = -1e13;
for(int i = 1; i <= n; i++)
{
int x = l[i], y = r[i];
if(a[i] < 0)
{
ans = max(ans, a[i]*(query(1, 1, n, i+1, y, 0)-query(1,1, n, x+1, i,1)));
}
else
{
ans = max(ans, a[i]*(query(1, 1, n, i+1, y, 1)-query(1,1, n,x+1, i,0)));
}
}
printf("%lld\n", ans);
return 0;
}