codeforces B. Ping-Pong (Easy Version) 解题报告
题目链接:http://codeforces.com/problemset/problem/320/B
题目意思:有两种操作:"1 x y" (x < y) 和 "2 a b" (a ≠ b) 。 问对于的"2 a b" 询问,能否从第a行的区间到达第b行的区间(行的数量是以:"1 x y" 来统计的,不包括"2 a b"这些行),当然可以直达,也可以借助某些区间间接到达。假设给定一个区间为 (a, b) ,能到达区间 (c, d) 的条件需要满足 c < a < d 或者c < b < d 。以题目中的数据为例,
5
1 1 5
1 5 11
2 1 2
1 2 9
2 1 2
对于第3行的 2 1 2,即问从第1行:1 1 5 是否可以到达第2行的 1 5 11,这明显是不能的,因为 5 < 1 < 11 和 5 < 5 < 11 都不满足。而第5行的 2 1 2 则是可以的,因为可以借助1 2 9 这个桥梁:(1 5) ——> (2 9) ——> (5 11)。理由是 2 < 5 < 9 和 5 < 9 < 11。
其实这条题目的解决方法是搜索,以下用dfs来解决
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
int a[maxn], b[maxn], ans[maxn];
int n; void dfs(int k)
{
int i;
ans[k] = ; // 可以到达第k行的区间
for (i = ; i <= n; i++)
{
if (a[k] > a[i] && a[k] < b[i] && !ans[i]) // !ans[i]的使用防止下一次又重复访问第 i 行的区间,致使不停的无限循环
dfs(i);
else if (b[k] > a[i] && b[k] < b[i] && !ans[i])
dfs(i);
}
} int main()
{
int i, t1, t2, choice, len;
while (scanf("%d", &len) != EOF)
{
n = ;
for (i = ; i <= len; i++)
{
memset(ans, , sizeof(ans)); // 这个很重要,每次询问都要清空,询问之间是互相独立的
scanf("%d", &choice);
if (choice == )
{
n++;
scanf("%d%d", &a[n], &b[n]);
}
else if (choice == )
{
scanf("%d%d", &t1, &t2);
dfs(t1);
if (ans[t2]) // 如果可以到达第t2行的区间
puts("YES");
else
puts("NO");
}
}
}
return ;
}
codeforces B. Ping-Pong (Easy Version) 解题报告的更多相关文章
- 【LeetCode】278. First Bad Version 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 二分查找 日期 题目地址:https://leetcode.c ...
- codeforces C1. The Great Julya Calendar 解题报告
题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...
- codeforces B. Eugeny and Play List 解题报告
题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
- codeforces 510B. Fox And Two Dots 解题报告
题目链接:http://codeforces.com/problemset/problem/510/B 题目意思:给出 n 行 m 列只有大写字母组成的字符串.问具有相同字母的能否组成一个环. 很容易 ...
- codeforces 505A. Mr. Kitayuta's Gift 解题报告
题目链接:http://codeforces.com/problemset/problem/505/A 题目意思:给出一个长度不大于10的小写英文字符串 s,问是否能通过在字符串的某个位置插入一个字母 ...
- codeforces 499A.Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/499/A 题目意思:有两种按钮:1.如果当前观看的时间是 t,player 可以自动处理下一分钟,姑且理解 ...
- codeforces 374A Inna and Pink Pony 解题报告
题目链接:http://codeforces.com/problemset/problem/374/A 题目意思:给出一个 n 行 m 列 的棋盘,要将放置在坐标点为(i, j)的 candy 移动 ...
随机推荐
- Codeforces A. Bear and Big Brother
...不行.这题之后.不做1000分以下的了.很耻辱 A. Bear and Big Brother time limit per test 1 second memory limit per t ...
- win10下ubuntu虚拟机互传文件
前言 用过虚拟机的都知道在向linux传文件的时候总是会遇到各种问题 安装虚拟机的增强工具不好用,反正就是各种麻烦各种麻烦 准备-------方案一 使用专门的xshell可以直接链接到虚拟机,同时配 ...
- Java中PO、BO、VO、DTO、POJO、DAO概念及其作用和项目实例图(转)
PO(bean.entity等命名): Persistant Object持久对象,数据库表中的记录在java对象中的显示状态 最形象的理解就是一个PO就是数据库中的一条记录. 好处是可以把一条记录作 ...
- .net 哈希
using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptograph ...
- mybatis学习网站
http://www.mybatis.org/mybatis-3/zh/index.html
- 【Linxu】CentOS7下安装程序报错:
进入root用户,然后编辑 vi /usr/libexec/urlgrabber-ext-down 将首行换成 #!/usr/bin/python2.
- hdu5379||2015多校联合第7场1011 树形统计
pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...
- php性能监控扩展xhprof
XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开 关来控制是否进行profile.总体来说是个不错的工具 ...
- c++对象内存模型【内存布局】(转)
总结:1.按1继承顺序先排布基于每个父类结构.2.该结构包括:基于该父类的虚表.该父类的虚基类表.父类的父类的成员变量.父类的成员变量.3.多重继承且连续继承时,虚函数表按继承顺序排布函数与虚函数.4 ...
- C#自定义类型数组排序
在数组或者集合中对自定义类型进行排序分为两种方法. 1.如果这个自定义类型是自己定义编写的,那么我可以使它继承ICompareable<T>接口,实现其中的CompareTo(Object ...