zoukankan      html  css  js  c++  java
  • P1016 旅行家的预算 模拟 贪心

      

    题目描述

    墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会向你发布如下指令:

    1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。

    2、 R P Col 把第P支画笔替换为颜色Col。

    为了满足墨墨的要求,你知道你需要干什么了吗?

    输入输出格式

    输入格式:

    第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。

    第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。

    第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

    输出格式:

    对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

    输入输出样例

    输入样例#1: 复制
    6 5
    1 2 3 4 5 5
    Q 1 4
    Q 2 6
    R 1 2
    Q 1 4
    Q 2 6
    输出样例#1: 复制
    4
    4
    3
    4


    比较麻烦的一道模拟题:
    贪心原则:
    如果在当前油站的范围内有比当前油价更小的加油站 维护当前油量为恰好到那个更小的油站所用油量即可
    如果没有 则加满油
    //#include<bits/stdc++.h>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<map>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define CLR(A,v)  memset(A,v,sizeof A)
    #define inf 0x3f3f3f3f
    #define lson l,m,pos<<1
    #define rson m+1,r,pos<<1|1
    //////////////////////////////////////
    const int N=3e6+5;
    struct node
    {
        double p,d;
    }s[N];
    double D,C,money,D2,p1,nowoil;
    int nowpos,n;
    
    int main()
    {
        cin>>D>>C>>D2>>p1>>n;
        s[0].d=0;s[0].p=p1;s[n+1].d=D;
    
        rep(i,1,n)
        scanf("%lf%lf",&s[i].d,&s[i].p);
    
        double maxx=0;
        rep(i,1,n+1)maxx=max(maxx,s[i].d-s[i-1].d);
        if(C*D2<maxx){cout<<"No Solution"<<endl;return 0;}
    
    
        while(1)
        {
            int flag=-1;
            rep(i,nowpos+1,n)
            {
                if(s[i].d-s[nowpos].d>C*D2)break;
                if(s[nowpos].p>s[i].p){flag=i;break;}
            }
    
            if(flag!=-1){ double dis=s[flag].d-s[nowpos].d;double x=dis/D2-nowoil;if(x<0){nowoil-=dis/D2;nowpos=flag;continue;} money+=x*s[nowpos].p; nowoil=0;nowpos=flag;continue;}
    
            if(s[n+1].d-s[nowpos].d<=C*D2){ double dis=s[n+1].d-s[nowpos].d; double x=dis/D2-nowoil; money+=x*s[nowpos].p;break;  }//如果能跳到终点
    
            double x=C-nowoil;  money+=s[nowpos].p*x;  nowoil=C-(s[nowpos+1].d-s[nowpos].d)/D2;  nowpos++;//如果当前位置的油价是最小的
        }
    
        printf("%.2lf",money);
    
        return 0;
    }
    View Code










  • 相关阅读:
    面试题目以及注意事项
    jQuery Ajax 实例 ($.ajax、$.post、$.get)
    前端知识大全
    jquery实现2级联动
    [转]那些年我们一起清除过的浮动
    使用kubeadm在CentOS上搭建Kubernetes1.14.3集群
    企业优秀运维人员20道必会iptables面试题
    通过nginx日志利用shell统计日pv和uv
    php访问mysql接口pdo-mysql安装
    何查看已经安装的nginx、apache、mysql和php的编译参数
  • 原文地址:https://www.cnblogs.com/bxd123/p/10922608.html
Copyright © 2011-2022 走看看