ZOJ 3197

简介: Google BookTime Limit: 1 Second      Memory Limit: 32768 KBYou, the best hacker in the world, want to download the books published on Google Book.
Google Book
Time Limit: 1 Second        Memory Limit: 32768 KB

You, the best hacker in the world, want to download the books published on Google Book. After some investigation, you found that the address of each page consists of two parts. The first part is the page number, the second part is the signature which is unique for each page. To get the signature, you can send the query to the server. The query has one parameter, which indicates the page number. The server will return the signature of the required page, and it may also return the signature of some adjacent pages.

To minimize the bytes downloaded from the internet, and also make the server adminstrator hard to notice your "hack", you'd like to minimize the number of queries

Input

The input has multiple cases. The first line of the input is a single integer T which is the number of test cases. Then T consecutive test cases follow. In each test case, the first line is a number N (1<=N<=5000), indicating the number of pages of the book. Then n lines follows. On the i-th line, there will be two integers ai and bi (ai<=i<=bi). They indicate that the query for the i-th page will return the signatures from page ai to page bi (inclusive)

Output

Results should be directed to standard output. The output of each test case should be a single integer, which is the minimum number of queries to get all the signatures.

Sample Input

 

2
3
1 1
2 2
3 3
3
1 1
1 3
3 3

 

Sample Output

 

3
1

 


Author: FAN, Xiang
Source: ZOJ Monthly, May 2009

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdlib>
 4 using namespace std;
 5 
 6 const int N = 5010;
 7 typedef struct Node 
 8 {
 9     int start,end;
10 };
11 Node node[N];
12 
13 int cmp(const void *a, const void *b)
14 {
15     Node *c = (Node *)a;
16     Node *d = (Node *)b;
17     if(c->start != d->start)
18         return c->start > d->start;
19     return c->end > d->end;
20 }
21 bool comp(Node s1, Node s2)
22 {
23     if(s1.start != s2.start)
24         return s1.start < s2.start;
25     return s1.end < s2.end;
26 }
27 
28 int main()
29 {
30     int i,j,k;
31     int T;
32     int n;
33     cin>>T;
34     int from, to, cnt;
35     while(T--)
36     {
37         cin>>n;
38        // memset(node,0,sizeof(node));
39         for(i=0; i<n; i++)
40         {
41             cin>>node[i].start>>node[i].end;
42             if(node[i].end>n)
43                 node[i].end = n;
44         }
45         //qsort排序失败了不知道为啥子, 
46         //qsort(node,n,sizeof(Node[0]),cmp);
47         sort(node, node +n,comp);
48         /*
49         for(i=0; i<n; i++)
50         {
51             cout<<node[i].start<<"  "<<node[i].end<<endl;
52         }
53         */
54         
55         //排过序了,最小点一定是1 ,实际上对end排序没有意义 
56         from = node[0].start, to = node[0].end;
57         i = 0;
58         while(i<n && from==node[i].start)
59         {
60             if(to<node[i].end)
61                 to = node[i].end;
62             i++;
63         }
64         
65         cnt = 1;
66         while(to<n)
67         {
68             int temp = -1;
69             from = to + 1;
70             for(j=i; j<n; j++)
71             {
72                 if(node[j].start<=from)
73                 {
74                     if(node[j].end>temp)
75                         temp = node[j].end;
76                 }
77                 else
78                 {
79                     i = j;
80                     break;
81                 } 
82             }
83             /*
84             必须在for外判断,判断是必须要的,因为可能前一个区间完全包含选择好的最大区间
85             比如
86             前一个:(1,9)
87              后:(2,3)(2,6)(4,7),则选择了(4,7) 不过7还是小于9的,不用增加下载次数,卡在这了一直TLE 
88             */
89             if(temp>to)
90             {
91                 cnt++;
92                 to = temp;
93             }
94         }
95         cout<<cnt<<endl;   
96     }
97     return 0;
98 }

 省赛时遇到一道类似的,直接AC了,哈哈,貌似是最早提交的……

目录
相关文章
|
6天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
17天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1326 7
|
5天前
|
存储 人工智能 Java
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
本文讲解 Prompt 基本概念与 10 个优化技巧,结合学术分析 AI 应用的需求分析、设计方案,介绍 Spring AI 中 ChatClient 及 Advisors 的使用。
298 129
AI 超级智能体全栈项目阶段二:Prompt 优化技巧与学术分析 AI 应用开发实现上下文联系多轮对话
|
4天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
16天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1394 87
|
4天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
5天前
|
人工智能 Java API
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
本文介绍AI大模型的核心概念、分类及开发者学习路径,重点讲解如何选择与接入大模型。项目基于Spring Boot,使用阿里云灵积模型(Qwen-Plus),对比SDK、HTTP、Spring AI和LangChain4j四种接入方式,助力开发者高效构建AI应用。
284 122
AI 超级智能体全栈项目阶段一:AI大模型概述、选型、项目初始化以及基于阿里云灵积模型 Qwen-Plus实现模型接入四种方式(SDK/HTTP/SpringAI/langchain4j)
|
5天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
232 82
2025年阿里云域名备案流程(新手图文详细流程)