CCF 201809-1 买菜

问题描述

| 试题编号: | 201809-2 |

| 试题名称: | 买菜 |

| 时间限制: | 1.0s |

| 内存限制: | 256.0MB |

问题描述

小H和小W来到了一条街上,两人分开买菜,他们买菜的过程可以描述为,去店里买一些菜然后去旁边的一个广场把菜装上车,两人都要买n种菜,所以也都要装n次车。具体的,对于小H来说有n个不相交的时间段[a1,b1],[a2,b2]...[an,bn]在装车,对于小W来说有n个不相交的时间段[c1,d1],[c2,d2]...[cn,dn]在装车。其中,一个时间段[s, t]表示的是从时刻s到时刻t这段时间,时长为t-s。
由于他们是好朋友,他们都在广场上装车的时候会聊天,他们想知道他们可以聊多长时间。

输入格式

输入的第一行包含一个正整数n,表示时间段的数量。
接下来n行每行两个数ai,bi,描述小H的各个装车的时间段。

输出格式

输出一行,一个正整数,表示两人可以聊多长时间。

样例输入

4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14

样例输出

3

数据规模和约定

对于所有的评测用例,1 ≤ n ≤ 2000, a< b< ai+1,c< d< ci+1,对于所有的i(1 ≤ i ≤ n)有,1 ≤ ai, bi, ci, di ≤ 1000000。

题解

因为不清楚给的数据是否有序,所以先对左端点进行排序,此后分三种情况,一种是小H的时间段的右端相交于小W的时间段的内部,或者是小H的时间段的右端包含了小W的时间段;一种是小W的时间段的右端相交于小H的时间段的内部,或者是小W的时间段的右端包含了小H的时间段;最后一种是其中一个人的时间段相交于另一个人的两个不同时间段,例:小H的这一个时间段的右端落在小W的时间段的内部,小H的下一个时间段的左端落在小W的时间段的内部,就有两部分相交。n <= 2000 ,可以用两层循环解决第三种情况。代码如下:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <utility>
#define ll long long
#define ull_ unsigned long longusing namespace std ;const int maxx = 2005 ;
int n ;typedef struct{int left ;int right ;
}meassage ;meassage little_H[maxx] , little_W[maxx] ;void init(){for ( int i = 0 ; i < n ; i ++ ){cin >> little_H[i].left >> little_H[i].right ;}for ( int i = 0 ; i < n ; i ++ ){cin >> little_W[i].left >> little_W[i].right ;}return ;
}bool cmp( meassage x , meassage y ){return x.left < y.right ;
}void test(){for ( int i = 0 ; i < n ; i ++ ){cout << little_H[i].left << " " << little_H[i].right << endl ;}cout << endl ;for ( int i = 0 ; i < n ; i ++ ){cout << little_W[i].left << " " << little_W[i].right << endl ;}cout << endl ;return ;
}int main(){while ( cin >> n ){memset(little_H , 0 , sizeof(little_H)) ;memset(little_W , 0 , sizeof(little_W)) ;init() ;sort( little_H , little_H + n , cmp ) ;sort( little_W , little_W + n , cmp ) ;//    test() ;int ans = 0 ;for ( int i = 0 ; i < n ; i ++ ){for ( int j = 0 ; j < n ; j ++ ){if ( little_H[i].left <= little_W[j].left && little_H[i].right >= little_W[j].left ){if ( little_H[i].right <= little_W[j].right ){ans += little_H[i].right - little_W[j].left ;}else{ans += little_W[j].right - little_W[j].left ;}}else if ( little_W[j].left <= little_H[i].left && little_W[j].right >= little_H[i].left ){if ( little_W[j].right <= little_H[i].right ){ans += little_W[j].right - little_H[i].left ;}else{ans += little_H[i].right - little_H[i].left ;}}}}cout << ans << endl ;}return 0 ;
}

转载于:https://www.cnblogs.com/Cantredo/p/9839837.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388157.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

笔试题③

1.线程间通信 handler机制 2.AsyncTask 异步任务 3.HandlerThread 子线程中创建了一个 Looper对象 可以在子线程里使用消息机制 IntentService 带了HandlerThread 并且创建了一个子线程的handler 在服务中 创建子线程执行耗时操作 耗时操作执行结束之后服务退出 如果想在Serv…

Hadoop 2.0集群配置详细教程

Hadoop 2.0集群配置详细教程 前言 Hadoop2.0介绍 Hadoop是 apache 的开源 项目&#xff0c;开发的主要目的是为了构建可靠&#xff0c;可拓展 scalable &#xff0c;分布式的系 统&#xff0c; hadoop 是一系列的子工程的 总和&#xff0c;其中包含 1. hadoop common &#xff…

php如何减缓gc_管理信息传播-使用数据科学减缓错误信息的传播

php如何减缓gcWith more people now than ever relying on social media to stay updated on current events, there is an ethical responsibility for hosting companies to defend against false information. Disinformation, which is a type of misinformation that is i…

[UE4]删除UI:Remove from Parent

同时要将保存UI的变量清空&#xff0c;以释放占用的系统内存 转载于:https://www.cnblogs.com/timy/p/9842206.html

MySQL基础部分总结

MySQL 1、选择数据库 use dbnameshow databases;2、数据表 show tablesmysql> show columns from customers;mysql> desc customers;3、show 语句 show statusshow create databasesshow create tableshow grants4、select 检索 4.1.1版本后不再区分大小写&#xff0c;但…

BZOJ2503: 相框

Description P大的基础电路实验课是一个无聊至极的课。每次实验&#xff0c;T君总是提前完成&#xff0c;管理员却不让T君离开&#xff0c;T君只能干坐在那儿无所事事。先说说这个实验课&#xff0c;无非就是把几根导线和某些元器件&#xff08;电阻、电容、电感等&#xff09;…

泰坦尼克号 数据分析_第1部分:泰坦尼克号-数据分析基础

泰坦尼克号 数据分析My goal was to get a better understanding of how to work with tabular data so I challenged myself and started with the Titanic -project. I think this was an excellent way to learn the basics of data analysis with python.我的目标是更好地了…

Imperva开源域目录控制器,简化活动目录集成

Imperva已公开发布域目录控制器&#xff08;Domain Directory Controller&#xff0c;DDC&#xff09;的源代码&#xff0c;这是一个Java库&#xff0c;用于简化常见的Active Directory集成。 与Java的LdapContext不同&#xff0c;这个库构建在Apache Directory LDAP之上&#…

2018.10.24 NOIP模拟 小 C 的序列(链表+数论)

传送门 考虑到a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])a[l],gcd(a[l],a[l1]),gcd(a[l],a[l1],a[l2])....gcd(a[l]...a[r])是可以分成最多logloglog段且段内的数都是相同的。 那么我们用…

vba数组dim_NDArray — —一个基于Java的N-Dim数组工具包

vba数组dim介绍 (Introduction) Within many development languages, there is a popular paradigm of using N-Dimensional arrays. They allow you to write numerical code that would otherwise require many levels of nested loops in only a few simple operations. Bec…

Nodejs教程08:同时处理GET/POST请求

示例代码请访问我的GitHub&#xff1a; github.com/chencl1986/… 同时处理GET/POST请求 通常在开发过程中&#xff0c;同一台服务器需要接收多种类型的请求&#xff0c;并区分不同接口&#xff0c;向客户端返回数据。 最常用的方式&#xff0c;就是对请求的方法、url进行区分判…

关于position的四个标签

四个标签是static&#xff0c;relative&#xff0c;absolute&#xff0c;fixed。 static 该值是正常流&#xff0c;并且是默认值&#xff0c;因此你很少看到&#xff08;如果存在的话&#xff09;指定该值。 relative&#xff1a;框的位置能够相对于它在正常流中的位置有所偏移…

python算法和数据结构_Python中的数据结构和算法

python算法和数据结构To至 Leonardo da Vinci达芬奇(Leonardo da Vinci) 介绍 (Introduction) The purpose of this article is to give you a panorama of data structures and algorithms in Python. This topic is very important for a Data Scientist in order to help …

CSS:元素塌陷问题

2019独角兽企业重金招聘Python工程师标准>>> 描述&#xff1a; 在文档流中&#xff0c;父元素的高度默认是被子元素撑开的&#xff0c;也就是子元素多高&#xff0c;父元素就多高。但是当子元素设置浮动之后&#xff0c;子元素会完全脱离文档流&#xff0c;此时将会…

Celery介绍及常见错误

celery 情景&#xff1a;用户发起request&#xff0c;并等待response返回。在本些views中&#xff0c;可能需要执行一段耗时的程序&#xff0c;那么用户就会等待很长时间&#xff0c;造成不好的用户体验&#xff0c;比如发送邮件、手机验证码等。 使用celery后&#xff0c;情况…

python dash_Dash是Databricks Spark后端的理想基于Python的前端

python dash&#x1f4cc; Learn how to deliver AI for Big Data using Dash & Databricks this recorded webinar with Peter Kim of Plotly and Prasad Kona of Databricks.this通过Plotly的Peter Kim和Databricks的Prasad Kona的网络研讨会了解如何使用Dash&#xff06…

js里的数据类型转换

1、类型转换 转换为字符串 - String(x)- x.toString(x, 10)- x 转换为数字 - Number(x)- parseInt(x, 10) - parseFloat(x) - x - 0- x 转换为boolean - Boolean(x)- !!x 2、falsy值&#xff08;false&#xff09; - 0- NaN- - null- undefined 3、内存图 - object存储的是地址…

Eclipse 插件开发遇到问题心得总结

Eclipse 插件开发遇到问题心得总结 Posted on 2011-07-17 00:51 季枫 阅读(3997) 评论(0) 编辑 收藏1、Eclipse 中插件开发多语言的实现 为了使用 .properties 文件&#xff0c;需要在 META-INF/MANIFEST.MF 文件中定义&#xff1a; Bundle-Localization: plugin 这样就会…

/src/applicationContext.xml

<?xml version"1.0" encoding"UTF-8"?> <beans xmlns"http://www.springframework.org/schema/beans" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xmlns:context"http://www.springframework.org/schema…

在Python中查找子字符串索引的5种方法

在Python中查找字符串中子字符串索引的5种方法 (5 Ways to Find the Index of a Substring in Strings in Python) str.find() str.find() str.rfind() str.rfind() str.index() str.index() str.rindex() str.rindex() re.search() re.search() str.find() (str.find()) …