列表

详情


NC229258. It Rains Again

描述

Suppose that in a Cartesian coordinate system on an infinite plane, the x-axis represents the ground and there are n rainscreens (We don't consider their thickness.) in the sky.

Every rainscreen can be described as a segment which starts from the (x_1, y_1) and ends at (x_2, y_2). Now if it starts to rain from the infinite high sky, I want you to tell me how many units of the x-axis will not get rained on.

To simplify the problem,the rain can only move vertically whenever it falls.

Note that two rainscreens can overlap and intersect with each other, and there is no rainscreen which is placed vertically.

输入描述

The first line contains one positive integer .
Each of the next n lines contains four positive integers , representing a rainscreen which starts from the(x_1, y_1) and ends at (x_2, y_2).

输出描述

The only one integer - the number of units of the x-axis which will not get rained on.

示例1

输入:

5
1 2 2 1
1 1 2 2
3 3 4 3
5 1 6 3
6 3 7 2

输出:

4

原站题解

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

C 解法, 执行用时: 552ms, 内存消耗: 708K, 提交时间: 2022-03-22 16:17:51

#include <stdio.h>
int main(){
	int i,n,u,t,x,m[100000];
	scanf("%d",&n);
	for(i=0;i<=100000;i++)	m[i]=0;
	for(;n>0;n--){
		scanf("%d%d%d%d",&i,&u,&t,&x);
		for(u=i-1;u<t-1;u++)	m[u]=1;
	}
	x=0;
	for(i=0;i<100000;i++)	x+=m[i];
	printf("%d",x);
	return 0;
}

C++ 解法, 执行用时: 1614ms, 内存消耗: 792K, 提交时间: 2021-10-26 10:43:19

#include<stdio.h>
int a[100005];
int main()
{
	int t,x1,y1,x2,y2;
	scanf("%d",&t);
	int cnt=0;
	while(t--)
	{
		scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
		for(int i=x1;i<x2;++i)
		{
			if(a[i]==0)
			{
				a[i]=1;
				++cnt;
			}
		}
	}
	printf("%d",cnt);
}

Python3 解法, 执行用时: 575ms, 内存消耗: 5364K, 提交时间: 2022-10-17 19:54:20

a=int(input())
l=[0]*100005
for i in range(a):
    a,b,c,d=map(int,input().split())
    l[a+1]+=1
    l[c+1]-=1
ans=0
k=0
for i in range(len(l)):
    k+=l[i]
    if k>0:
        ans+=1
print(ans)
    

上一题