【题解】 Codeforces Round #503 A
目录
题目描述
New Building for SIS
time limit per test 1 second
memory limit per test 256 megabytes
input
standard input
output
standard output
You are looking at the floor plan of the Summer Informatics School's new building. You were tasked with SIS logistics, so you really care about travel time between different locations: it is important to know how long it would take to get from the lecture room to the canteen, or from the gym to the server room.
The building consists of n towers, h floors each, where the towers are labeled from 1 to n, the floors are labeled from 1 to h. There is a passage between any two adjacent towers (two towers i and i + 1 for all i: 1 ≤ i ≤ n - 1) on every floor x, where a ≤ x ≤ b. It takes exactly one minute to walk between any two adjacent floors of a tower, as well as between any two adjacent towers, provided that there is a passage on that floor. It is not permitted to leave the building.
The picture illustrates the first example.
You have given k pairs of locations (ta, fa), (tb, fb): floor fa of tower ta and floor fb of tower tb. For each pair you need to determine the minimum walking time between these locations.
Input
The first line of the input contains following integers:
- n: the number of towers in the building (1 ≤ n ≤ 108),
- h: the number of floors in each tower (1 ≤ h ≤ 108),
- a and b: the lowest and highest floor where it's possible to move between adjacent towers (1 ≤ a ≤ b ≤ h),
- k: total number of queries (1 ≤ k ≤ 104).
Next k lines contain description of the queries. Each description consists of four integers ta, fa, tb, fb (1 ≤ ta, tb ≤ n, 1 ≤ fa, fb ≤ h). This corresponds to a query to find the minimum travel time between fa-th floor of the ta-th tower and fb-th floor of the tb-th tower.
Output
For each query print a single integer: the minimum walking time between the locations in minutes.
Example
input
3 6 2 3 3 1 2 1 3 1 4 3 4 1 2 2 3
output
1 4 2
题意分析
题意:给你一系列查询,问从楼层A到楼层B最少要多久时间,横跨竖行一格都只要1min
但是要横跨楼层的话,就必须在所给范围内才可跨越。
一开始少考虑了一个条件WA了一发,首先是楼层不同,判断一下是否在所给的范围内
在外面逼近限定就好,然后就是在同一楼层,直接抵达即可 (WA点)。
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#define MAXN 10005
using namespace std;
int main()
{
long long n,h,a,b,k; // a 最低 b 最高
long long sum=0;
long long q,w,e,r;
int i;
while(cin>>n>>h>>a>>b>>k)
{
for(i=0;i<k;i++)
{
sum=0;
cin>>q>>w>>e>>r;
if(q==e)
{
sum += abs(w-r);
}
else
{
sum += abs(q-e);
if(w>b)
{
sum += abs(w-b);
w=b;
}
if(w<a)
{
sum += abs(w-a);
w=a;
}
if(r>b)
{
sum += abs(r-b);
r=b;
}
if(r<a)
{
sum += abs(r-a);
r=a;
}
sum += abs(w-r);
}
cout<<sum<<endl;
}
}
return 0;
}
上一篇: 秒杀---使用乐观锁实现或cache实现
下一篇: Java多线程——volatile详解
推荐阅读
-
Codeforces Round #252 (Div. 2)-C,D_html/css_WEB-ITnose
-
Codeforces Round #277.5 (Div. 2)-D_html/css_WEB-ITnose
-
Codeforces Round #225 (Div. 1) C 树状数组 || 线段树_html/css_WEB-ITnose
-
Codeforces Round #275 (Div. 1)D(树形DP)_html/css_WEB-ITnose
-
Codeforces Round #264 (Div. 2)
-
Codeforces Round #281 (Div. 2)_html/css_WEB-ITnose
-
codeforces Round #260(div2) E解题报告
-
Codeforces Round #259 (Div. 2) A B C 三连发_html/css_WEB-ITnose
-
Codeforces Round #281 (Div. 2) (A、B、C、D题)_html/css_WEB-ITnose
-
Codeforces Round #222 (Div. 2)