当前位置:网站首页>Haut OJ 1221: a tired day

Haut OJ 1221: a tired day

2022-07-05 05:17:00 hunziHang

Problem description :

    It's early in the morning , Playing outside all day cds, Now I'm tired both physically and mentally , But it is still a whole kilometer away from the gate of the school , So he checked whether there was a bus to take
Walk as little as possible , Sure enough , Although it is very late now , But there are still two buses in operation , So here comes the question ,cds How can I make the shortest way to go ?
Let's assume that the path is set as a straight number axis , There are integer points on the number axis (0,1,2,3,4,……), There is a distance between every two points 1m. It is known that cds On the number axis 0 spot ,
And the school is at 1000 On , The starting and ending points of the two buses are x1, y1, x2, y2, You can't get off halfway , Take a car or not , And ensure that you can wait for the bus ( We assume that cds A lot of time ).

Input :

4 A positive integer x1, y1, x2, y2, Data assurance x1<y1, x2<y2, And all the int Within the scope of ,

Output :

An integer , Occupy a line , Express cds The shortest distance to walk

The sample input :

300 1000 50 301

Sample output :

51

Cause analysis :

1. because for It's from 1 At the beginning , therefore sort The first item should be a+1 Similarly, the last bit a+n+1

2. situation  

1. Take a car (2 Kind of )   2. Take two cars      3. No car

When making two cars, we should discuss the situation at the same time Three situations , The first includes the second , The first and second cars have a crossover , No cross at all


Solution :


#include<bits/stdc++.h>
using namespace std;
#define endl"\n"
typedef struct pos{
   int x,y;
}p;
bool cmp(p a,p b)
{
    return a.x<b.x;
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
   int n1,n2,n3,result,i;
     p a[4];
  for(i=1;i<=2;i++)
    cin>>a[i].x>>a[i].y;
  sort(a+1,a+3,cmp);
  n1=a[1].x+abs(1000-a[1].y);
  n3=a[2].x+abs(1000-a[2].y);
  if(a[2].y>a[1].y)
      n2=a[1].x+abs(a[1].y-a[2].x)+abs(1000-a[2].y);
  else
    n2=n1;
  result=min(n1,1000);
  result=min(result,n2);
  cout<<min(result,n3)<<endl;

  return 0;

}

原网站

版权声明
本文为[hunziHang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140622509154.html