一道挺水的题,好好读题理解题意就可以做了
题目链接
代码如下:
#include <iostream>
using namespace std;
const int MAX =1e3+5;
struct Node
{
int t;
int c;
}node[MAX];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
node[i].t=1e7;
node[i].c=0;
}
int l,r,t,c;
for(int i=0;i<m;i++){
cin>>l>>r>>t>>c;
for(int j=l;j<=r;j++){
if(t < node[j].t) //新的运动员跑这段时间短
node[j].c = c,node[j].t=t; //那么得替换成新的
}
}
int total=0;
for(int i=1;i<=n;i++){
total+=node[i].c;
}
cout<<total<<endl;;
return 0;
}
第二题有点烦人,就选择语句的情况要好好考虑一下,仔细点就能过了。
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int n,m;
cin>>n>>m;
int s,f,t;
for(int i=0;i<n;i++){
cin>>s>>f>>t;
int flag = t/(m-1); // 如果 flag为偶数说明正在向上开,如果不是偶数 说明正在向下开
int yushu = t%(m-1);
int floor;
if(flag&1){
floor = m-yushu;
}
else floor = 1+yushu;
int totaltime=0;
if(s>floor) // 他在当前楼层的上面,并且电梯往上开
{
if(flag%2==0)
totaltime = s-floor;
else totaltime = floor-1+s-1,flag=0; //这个时候往上开啦 往下开然后变成往上
floor = s;
}
else if(s<floor){
if(flag%2==0) //如果往上开
{
totaltime = (m-floor)+(m-s);
flag = 1; // 要改成往下开
}
else totaltime = floor-s;
floor = s;
}
//接到人了,然后就是判断坐电梯上去还是下去了
if(s==f){
totaltime = t;
}
else{
if(floor > f) // 如果电梯的位置在要去的下面
{
if(flag%2==0){
totaltime += (m-floor+m-f);
}
else totaltime += floor-f;
}
else if(floor<f){
if(flag%2==0)
totaltime += f-floor;
else totaltime += floor-1+f-1;
}
totaltime+=t;
}
cout<<totaltime<<endl;
}
return 0;
}