题解 P7189 【[COCI2007-2008#6] PARKING】

· · 题解

题意

停车场对卡车收费方式如下:

当然,不停车当然不会收费。

共有 3 辆卡车停在该停车场内,题目给出了每辆车停车的起始时间和结束时间,求最后的总费用。

注意:结束时间不收费。

思路

纯模拟,从第一分钟开始遍历每一分钟,判断该分钟停在停车场的卡车的数量,在总费用中加上该分钟的费用,直至所有卡车离开停车场,结束循环,输出总费用。

代码实现

\texttt{code:}
/* light_chaser */
  #include<bits/stdc++.h>
  using namespace std;
  int a,b,c;
  int l1,r1,l2,r2,l3,r3;//3辆车的停车时间区间
  int maxr=0;
  int ans=0;
  int total;
  int main(){
      cin>>a>>b>>c;
      cin>>l1>>r1>>l2>>r2>>l3>>r3;
      maxr=max(max(r1,r2),r3);//最后一辆卡车的离开时间;注意:要用2次max函数,不然会wa最后一个测试点
      for(int i=1; i<=maxr; i++){
          total=0;//每次循环先把停车场内卡车数量清零
          if( i>=l1 && i<r1 ){
              total++;
          }//第一辆车在停车场
          if( i>=l2 && i<r2 ){
              total++;
          }//第二辆车在停车场
          if( i>=l3 && i<r3 ){
              total++;
          }//第三辆车在停车场
          if(total==3){
              ans+=c*3;
          }//有3辆卡车
          else if(total==2){
              ans+=b*2;
          }//有2辆卡车
          else if(total==1){
              ans+=a;
          }//有1辆卡车
      }
      cout<<ans<<"\n";
      return 0;
  }

后记

感谢管理员的辛勤付出!

蒟蒻的第6篇题解,望通过。

求赞