列表

详情


NC24060. [USACO 2015 Dec B]Fence Painting

描述

Several seasons of hot summers and cold winters have taken their toll on Farmer John's fence, and he decides it is time to repaint it, along with the help of his favorite cow, Bessie. Unfortunately, while Bessie is actually remarkably proficient at painting, she is not as good at understanding Farmer John's instructions.

If we regard the fence as a one-dimensional number line, Farmer John paints the interval between x=ax=a and x=bx=b. For example, if a=3a=3 and b=5b=5, then Farmer John paints an interval of length 2. Bessie, misunderstanding Farmer John's instructions, paints the interval from x=cx=c to x=dx=d, which may possibly overlap with part or all of Farmer John's interval. Please determine the total length of fence that is now covered with paint.

输入描述

The first line of the input contains the integers aa and bb, separated by a space (a<ba<b).

The second line contains integers cc and dd, separated by a space (c<dc<d).

The values of aa, bb, cc, and dd all lie in the range 0…1000…100, inclusive.

输出描述

Please output a single line containing the total length of the fence covered with paint.

示例1

输入:

7 10
4 8

输出:

6

说明:

Here, 6 total units of fence are covered with paint, from x=4x=4 all the way through x=10x=10.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 3ms, 内存消耗: 380K, 提交时间: 2020-12-07 18:58:01

#include <bits/stdc++.h>
using namespace std;
int a[5],m=1000000,mm=0,ans=100000;

int main(){
	for(int i=1;i<=4;i++){
		cin>>a[i];
		m=min(m,a[i]);
		mm=max(mm,a[i]);
	}
	ans=min(mm-m,a[4]+a[2]-a[1]-a[3]);
	cout<<ans<<endl;
	
    return 0;
}

pypy3(pypy3.6.1) 解法, 执行用时: 44ms, 内存消耗: 29940K, 提交时间: 2020-12-06 23:01:54

#!/usr/bin/env python3
#
# Fence Painting
#
import sys, os

a, b = list(map(int, input().split()))
c, d = list(map(int, input().split()))

if c > b or d < a:
	print(b - a + d - c)
else:
	print(max(b, d) - min(a, c))

上一题