#拼多多集团-PDD笔试#我是菜鸡。4道题一道都没有全对,0.95  0.4  0.975  0.95  与大厂无缘了
不愧是拼都督,笔试都能感觉到卷了
--------
第二题的屎山代码
import java.util.*;
import java.io.*;
public class Main{
    public static void main(String[] args) throws InterruptedException {
        Scanner sc=new Scanner(new BufferedInputStream(System.in));
        PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
        int L=sc.nextInt(),C=sc.nextInt(),n=sc.nextInt();
        int[]ds=new int[n+1];
        int[]ps=new int[n+1];
        for(int i=0;i<n;i++){
            ds[i]=sc.nextInt();
            ps[i]=sc.nextInt();
        }
        ds[n]=L;
        ArrayDeque<Integer>w=new ArrayDeque<>();
        int next=C;
        int start=-1;
        int startC=C;
        long cost=0;
        boolean stop=false;
        for(int i=0;i<=n;i++){
            if(next>=ds[i]){
                while(!w.isEmpty()&&ps[w.getLast()]>=ps[i])w.removeLast();
                w.addLast(i);
            }else{
                int need=ds[i]-next;
                while(!w.isEmpty()&&need>0){
                    int idx=w.getFirst();
                    int space=(C-startC)+(ds[idx]-(start==-1?0:ds[start]));
                    // 可以加的油=邮箱中剩余的空间=起点时邮箱不满的空间+从起点走到这里花的油
                    if(space<=need){
                        need-=space;
                        next+=space;
                        cost+=(long)space*ps[idx];
                        startC=C;
                        start=idx;
                        w.removeFirst();
                    }else{
                        start=idx;
                        startC=C-space+need;
                        next+=need;
                        cost+=(long)need*ps[idx];
                        need=0;
                    }
                }
                if(need>0){
                    stop=true;
                    break;
                }
                i--; // 这时候反悔加了油,但是当前的i处的加油站还没有加进来,再来一轮
            }
        }
        if(stop)out.println(-1);
        else out.println(cost);
        out.flush();
        out.close();
        sc.close();
    }
}

只记得样例1了
20 10 3
4 5
9 2
15 6
输出为24
全部评论
不知道你们怎么做的,我前两道题都自定义了一个类然后实现了comparable,一写出这个就感觉特别清晰了,前两题全ac,后面两道题感觉是贪心,二分,前缀和的经典运用
1 回复 分享
发布于 昨天 18:46 重庆
你这不是稳了吗?
1 回复 分享
发布于 昨天 17:20 浙江
同学我前几天投的简历,这个笔试一般是怎么约的啊?我现在只做了一个性格测试,但是一直没有笔试邀请发过来,流程显示在笔试中😭
点赞 回复 分享
发布于 昨天 21:50 湖南
兄弟你这已经相当于a了三道半了,真不是凡尔赛吗
点赞 回复 分享
发布于 昨天 17:05 天津

相关推荐

评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务