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, ai?< bi?< ai+1,ci?< di?< 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.pswp.cn/news/388157.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/388157.shtml
英文地址,請注明出處:http://en.pswp.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()) …