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 移动 ...
随机推荐
- BZOJ2002弹飞绵羊
动态树LCT模板题 #include<cstdio> #include<cctype> #include<algorithm> using namespace st ...
- iOS- Exception Type: 00000020:什么是看门狗机制(转)
1.前言 前几天我们项目闪退之后遇到的一个Crash,之后逛了许多论坛,博客都没有找到满意的回复 在自己做了深入的研究之后,对iOS的看门狗机制有了一个基本的了解 而有很多奇怪的Crash可 ...
- 终极报错解决方案:Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed with
遇到这个报错的时候,不要慌 Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger fail ...
- mac apache配置虚拟主机
设置虚拟主机 在终端运行“sudo vi /etc/apache2/httpd.conf”,打开Apche的配置文件 在httpd.conf中找到“#Include /private/etc/apac ...
- ZOJ 3626 Treasure Hunt I(树形dp)
Treasure Hunt I Time Limit: 2 Seconds Memory Limit: 65536 KB Akiba is a dangerous country since ...
- 直接返回list不封装的结果集
直接返回list不封装的结果集,在Jsp访问方式: 1.封装成map访问 2.用jstl: <c:forEach var="images" items="${lis ...
- vue2.0 vue-router
一.SPA中路由的简单实现 main.js import Vue from 'vue' import App from './App' import VueRouter from 'vue-route ...
- HDU ACM 1073 Online Judge ->字符串水题
分析:水题. #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read ...
- who命令
who1.c #include <stdio.h>#include <utmp.h>#include <fcntl.h>#include <unistd.h& ...
- cocosStudio中使用PageView,ListView和ScrollView
晚上吃东西好像吃坏肚子了,.但是技术还要继续研究.最近工作中要使用CocosStudio做界面,好吧,不管对他有什么偏见,学习一下吧.这里主要记录一下三个控件的使用和说明.就是ScrollView,L ...