四连测(二)

我来水第二篇题解,let's go;

测量温度(temperature)

题目描述

见洛谷([POI2011]TEM-Temperature)

题目理解

考场思维:

题目描述什么乱七八糟的,第一题肯定不难,按常用的区间思维(s[i-1].r>=s[i].l),没想到老师连面子都不给,直接出难题。

下来之后发现:

我把题意改成了有可能平衡。于是题目应该是(s[i-1].l<=s[i].r),保持l的单调下降即可。不多讲,多思考就懂了。

代码

#include<cstdio>
#define M  1000000 + 5
inline void read(int &x){
    x = 0;
    int f = 1;
    char s = getchar();
    while (s < '0' || s > '9'){
        if (s == '-')
            f = -1;
        s = getchar();
    }
    while (s >= '0' && s<='9'){
        x = x * 10 + s - '0';
        s = getchar();
    }
    x *= f;
}
struct node{
    int l,r,pos;
}t;
node queue[M];
int tail=0,front=1,n,ans;
int main()
{
    read(n);
    for(t.pos=1;t.pos<=n;t.pos++)
    {
        read(t.l),read(t.r);
        while(front<=tail&&queue[front].l>t.r)//注意:题意是可能不下降,我排除了一定下降的。
            front++;
        if(ans<queue[tail].pos-queue[front-1].pos+1)
            ans=queue[tail].pos-queue[front-1].pos+1;
        while(front<=tail&&queue[tail].l<=t.l)
            tail--;
        queue[++tail]=t;
    }
    printf("%d",ans);
}

奶牛慢跑(cowjog)

题目描述

见洛谷(P4873 [USACO14DEC] Cow Jog_Gold 牛慢跑(金) )

题目理解

我最近沉迷单调队列优化dp,以至于什么都想着单调队列,这道就不行,之前用过的跑道可以重复利用,也就是导弹拦截,更是最长上升序列,由于复杂度小,n*log(n)可以过,于是二分辅助贪心即可,我当场估计智障了。真没什么好解释的,如果没见过,可以补一下基础知识。

代码

#include<cstdio>
int n,t,len;
long long dp[100005];
int main()
{
    scanf("%d%d",&n,&t);
    dp[0]=1ll<<60;
    for(int i=1;i<=n;i++)
    {
        int x,y;
        scanf("%d%d",&x,&y);
        long long p=x+1ll*y*t;
        if(p<=dp[len]) {dp[++len]=p;continue;}
        int l=0,r=len,mid;
        while(l<r)
        {
            mid=(l+r+1)/2;
            if(p>dp[mid]) r=mid-1;
            else l=mid;
        }
        dp[l+1]=p;
    }
    printf("%d",len);
}

路径规划(path)

题目描述

有n个点,m条无向边,有A,B两个人,初始时刻A在点1,B在点2,他们要走到点n去。A每走一条边,要消耗B单位能量,B每走一条边,要消耗E单位能量。如果A,B相伴走,则只消耗P单位的能量。请问A,B走到点n,最少要消耗多少能量?

输入数据保证1和n,2和n连通。

 

输入

第一行包含整数B,E,P,N和M,所有的整数都不超过40000,N>=3.

接下来M行,每行两个整数,表示该无向边连接的两个顶点。

 

输出

一个整数,表示最小要消耗的能量。

题目理解

如果分开走,好算。一起走,总有个集合点,既然集合了(不要有错觉,假定的是已经在一起了),剩下的的最短路就是到N点的最短距离了,再有从1和2两点到集合点的最短距离就行了,所以三次SPFA(假的SPFA,由于权值相等),然后O(n)枚举集合点就好了。

代码

#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
#define M  40000 + 5
inline void read(int &x){
    x = 0;
    int f = 1;
    char s = getchar();
    while (s < '0' || s > '9'){
        if (s == '-')
            f = -1;
        s = getchar();
    }
    while (s >= '0' && s<='9'){
        x = x * 10 + s - '0';
        s = getchar();
    }
    x *= f;
}
vector<int>G[M];
bool vis[M];
int dis[4][M],n,m,w1,w2,w3,ans=0x3f3f3f3f;
void SPFA(int x,int o)
{
    int queue[M],tail=0,front=1;
    queue[++tail]=o;
    memset(dis[x],0x3f,sizeof(dis[x]));
    memset(vis,0,sizeof(vis));
    dis[x][o]=0;
    vis[o]=1;
    while(front<=tail)
    {
        vis[queue[front]]=0;
        for(int i=0;i<G[queue[front]].size();i++)
            if(!vis[G[queue[front]][i]]&&dis[x][queue[front]]+1<dis[x][G[queue[front]][i]])
            {
                vis[G[queue[front]][i]]=1;
                dis[x][G[queue[front]][i]]=dis[x][queue[front]]+1;
                queue[++tail]=G[queue[front]][i];
            }
        front++;
    }
}
int main()
{
    read(w1),read(w2),read(w3);
    read(n),read(m);
    for(int i=1;i<=m;i++)
    {
        int x,y;
        read(x),read(y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    SPFA(1,1);
    SPFA(2,2);
    SPFA(3,n);
    for(int i=1;i<=n;i++)
        if(dis[1][i]*w1+dis[2][i]*w2+dis[3][i]*w3<ans)
            ans=dis[1][i]*w1+dis[2][i]*w2+dis[3][i]*w3;
    printf("%d",ans);   
}

奶牛飞盘

题目描述

[USACO14DEC]后卫马克Guard Mark(抄袭都不用原名)

题目理解

我这次考试像中了邪,每道题都失误(这是我总分第8的大原因)。我像瞎了眼一样看不见n<=20,搜索都能过(也确实是搜索),先数论证明,如果想s[1~i-1].重力的和减s[i].承受力大就用s[i-1].重力+s[i-1].承受力<s[i].重力+s[i].承受力,再dfs,用之前所用的重的和判断这头牛可不可以用,再分别别用与不用。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
inline void read(LL &x) { 
    x=0;LL f=1;char c=getchar(); 
    while(c<'0'||c>'9') { if(c=='-') f=0-f; c=getchar(); } 
    while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar(); } 
    x*=f; return ; 
} 
struct node{
    LL w,h,v; 
}s[21]; 
bool cmp(node a,node b) { 
    return a.w+a.v<b.w+b.v; 
} 
LL n,H,ans=-1;
void dfs(LL x,LL sumh,LL sumv,LL op) {
    if(x==n+1)
    {
        if(sumh>=H&&ans<op)
            ans=op;
        return ;
    }
    if(sumv<s[x].v)
        dfs(x+1,sumh+s[x].h,sumv+s[x].w,min(op,s[x].v-sumv));
    dfs(x+1,sumh,sumv,op);
    return ;
} 
int main() { 
    read(n); 
    read(H); 
    for(int i=1;i<=n;i++) 
        read(s[i].h),read(s[i].w),read(s[i].v); 
    sort(s+1,s+1+n,cmp);
    dfs(1,0,0,0x3f3f3f3f);
    if(ans>-1) 
        printf("%lld",ans);
    else
        printf("Mark is too tall"); 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值