code chef - Divide the Tangerine 橘子分块算法题解

橘子分堆算法
本文介绍了一种解决橘子分堆问题的算法。该算法旨在判断是否可以从已混乱的橘子堆中,通过进一步分割而得到最初期望的橘子分堆方式。文中详细解释了问题背景、输入输出格式及解决方案。

Once Chef decided to divide the tangerine into several parts. At first, he numbered tangerine's segments from1 to n in the clockwise order starting from some segment. Then he intended to divide the fruit into several parts. In order to do it he planned to separate the neighbouring segments in k places, so that he could get kparts: the 1st - from segment l1 to segment r1 (inclusive), the 2nd - from l2 to r2, ..., the kth - from lk to rk (in all cases in the clockwise order). Suddenly, when Chef was absent, one naughty boy came and divided the tangerine into p parts (also by separating the neighbouring segments one from another): the 1st - from segment a1 to segment b1, the 2nd - from a2 to b2, ..., the pth - from ap to bp (in all cases in the clockwise order). Chef became very angry about it! But maybe little boy haven't done anything wrong, maybe everything is OK? Please, help Chef to determine whether he is able to obtain the parts he wanted to have (in order to do it he can divide p current parts, but, of course, he can't join several parts into one).

Please, note that parts are not cyclic. That means that even if the tangerine division consists of only one part, but that part include more than one segment, there are two segments which were neighbouring in the initial tangerine but are not neighbouring in the division. See the explanation of example case 2 to ensure you understood that clarification.

Input

The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains three space separated integers nkp, denoting the number of tangerine's segments and number of parts in each of the two divisions. The next k lines contain pairs of space-separated integers li and ri. The next p lines contain pairs of space-separated integers ai and bi.

It is guaranteed that each tangerine's segment is contained in exactly one of the first k parts and in exactly one of the next p parts.

Output

For each test case, output a single line containing either "Yes" or "No" (without the quotes), denoting whether Chef is able to obtain the parts he wanted to have.

 

Constraints

  • 1 ≤ T ≤ 100
  • 1 ≤ n ≤ 5 * 107
  • 1 ≤ k ≤ min(500, n)
  • 1 ≤ p ≤ min(500, n)
  • 1 ≤ liriaibi ≤ n

 

Example

Input:
2
10 3 2
1 4
5 5
6 10
1 5
6 10
10 3 1
2 5
10 1
6 9
1 10

Output:
Yes
No

 

Explanation

Example case 1: To achieve his goal Chef should divide the first part (1-5) in two by separating segments 4 and 5 one from another.

Example case 2: The boy didn't left the tangerine as it was (though you may thought that way), he separated segments 1 and 10 one from another. But segments 1 and 10 are in one part in Chef's division, so he is unable to achieve his goal.


好长的题目,本题目的难度就是如何读懂题目了。

题目解析:

1 把一个橘子分块,然后分堆,所有堆中有的快的序号必须是顺时针数起

2 分堆已经乱了

3 是否在乱了的堆中分出原来想要分的堆?

额外隐藏条件: 1 所有堆都需要分出来 2 不能合并堆,只能分开堆

读懂题目就能出答案了,尤其是隐藏条件

注意:

1 输出格式Yes不是YES  

2 vector容器每次都需要清零

这个程序的时间效率有点复杂,是:O(lg(k)*max(k, p))

  1. #include <iostream>  
  2. #include <vector>  
  3. #include <string>  
  4. #include <algorithm>  
  5.   
  6. using namespace std;  
  7.   
  8. struct Tangerine  
  9. {  
  10.     int left, right;  
  11.     bool operator<(const Tangerine &t) const  
  12.     {  
  13.         return left < t.left;  
  14.     }  
  15. };  
  16.   
  17. bool biDiv(vector<Tangerine> &vk, int low, int up, int left)  
  18. {  
  19.     if (low > up) return false;  
  20.     int mid = low + ((up-low)>>1);  
  21.     if (vk[mid].left < left) return biDiv(vk, mid+1, up, left);  
  22.     if (left < vk[mid].left) return biDiv(vk, low, mid-1, left);  
  23.     return true;  
  24. }  
  25.   
  26. string divTangerine(vector<Tangerine> &vk, vector<Tangerine> &vp)  
  27. {  
  28.     sort(vk.begin(), vk.end());  
  29.     for (unsigned i = 0; i < vp.size(); i++)  
  30.     {  
  31.         if (!biDiv(vk, 0, vk.size()-1, vp[i].left)) return "No";  
  32.     }  
  33.     return "Yes";  
  34. }  
  35.   
  36. void DivideTheTangerine()  
  37. {  
  38.     int n = 0, k = 0, p = 0;  
  39.     Tangerine tang;  
  40.     vector<Tangerine> vk;  
  41.     vector<Tangerine> vp;  
  42.   
  43.     int T = 0;  
  44.     cin>>T;  
  45.     while (T--)  
  46.     {  
  47.         cin>>n>>k>>p;  
  48.         for (int i = 0; i < k; i++)  
  49.         {  
  50.             cin>>tang.left>>tang.right;  
  51.             vk.push_back(tang);  
  52.         }  
  53.         for (int i = 0; i < p; i++)  
  54.         {  
  55.             cin>>tang.left>>tang.right;  
  56.             vp.push_back(tang);  
  57.         }  
  58.           
  59.         cout<<divTangerine(vk, vp)<<endl;  
  60.   
  61.         vk.clear(), vp.clear();  
  62.     }  
标题SpringBoot智能在线预约挂号系统研究AI更换标题第1章引言介绍智能在线预约挂号系统的研究背景、意义、国内外研究现状及论文创新点。1.1研究背景与意义阐述智能在线预约挂号系统对提升医疗服务效率的重要性。1.2国内外研究现状析国内外智能在线预约挂号系统的研究与应用情况。1.3研究方法及创新点概述本文采用的技术路线、研究方法及主要创新点。第2章相关理论总结智能在线预约挂号系统相关理论,包括系统架构、开发技术等。2.1系统架构设计理论介绍系统架构设计的基本原则和常用方法。2.2SpringBoot开发框架理论阐述SpringBoot框架的特点、优势及其在系统开发中的应用。2.3数据库设计与管理理论介绍数据库设计原则、数据模型及数据库管理系统。2.4网络安全与数据保护理论讨论网络安全威胁、数据保护技术及其在系统中的应用。第3章SpringBoot智能在线预约挂号系统设计详细介绍系统的设计方案,包括功能模块划、数据库设计等。3.1系统功能模块设计划系统功能模块,如用户管理、挂号管理、医生排班等。3.2数据库设计与实现设计数据库表结构,确定字段类型、主键及外键关系。3.3用户界面设计设计用户友好的界面,提升用户体验。3.4系统安全设计阐述系统安全策略,包括用户认证、数据加密等。第4章系统实现与测试介绍系统的实现过程,包括编码、测试及优化等。4.1系统编码实现采用SpringBoot框架进行系统编码实现。4.2系统测试方法介绍系统测试的方法、步骤及测试用例设计。4.3系统性能测试与析对系统进行性能测试,析测试结果并提出优化建议。4.4系统优化与改进根据测试结果对系统进行优化和改进,提升系统性能。第5章研究结果呈现系统实现后的效果,包括功能实现、性能提升等。5.1系统功能实现效果展示系统各功能模块的实现效果,如挂号成功界面等。5.2系统性能提升效果对比优化前后的系统性能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值