CF#335 Board Game
2.5 seconds
256 megabytes
standard input
standard output
You are playing a board card game. In this game the player has two characteristics, x and y — the white magic skill and the black magic skill, respectively. There are n spell cards lying on the table, each of them has four characteristics, ai, bi, ci and di. In one move a player can pick one of the cards and cast the spell written on it, but only if first two of it's characteristics meet the requirement ai ≤ x and bi ≤ y, i.e. if the player has enough magic skill to cast this spell. However, after casting the spell the characteristics of a player change and become equal to x = ci and y = di.
At the beginning of the game both characteristics of a player are equal to zero. The goal of the game is to cast the n-th spell. Your task is to make it in as few moves as possible. You are allowed to use spell in any order and any number of times (for example, you may not use some spells at all).
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cards on the table.
Each of the next n lines contains four integers ai, bi, ci, di (0 ≤ ai, bi, ci, di ≤ 109) — the characteristics of the corresponding card.
In the first line print a single integer k — the minimum number of moves needed to cast the n-th spell and in the second line print knumbers — the indices of the cards in the order in which you should cast them. In case there are multiple possible solutions, print any of them.
If it is impossible to cast the n-th spell, print - 1.
4
0 0 3 4
2 2 5 3
4 1 1 7
5 3 8 8
3
1 2 4
2
0 0 4 6
5 1 1000000000 1000000000
-1
题意:给出n对点(ai,bi)、(ci, di),一开始在(0, 0),每一步可以从当前点(x, y)可以跳到(ci, di),条件是x>=ai且y>=bi。
问跳到(cn, dn)最少要多少步(这n对点是编号的,题目要求一定要跳到第n对点),要求方案。
分析:简单的BFS。
看似每次转移都要n次枚举,但是显然根据BFS的性质,每个点只可能转移一次。
转移之后删掉即可。
通过一定的链表之类的东西,按照x,y分别排序,然后在按照一定顺序扫描点,可以保证复杂度在O(n)
/**
Create By yzx - stupidboy
*/
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define mk make_pair inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = ;
struct DataType
{
int sx, sy, ex, ey, index;
inline void Read()
{
scanf("%d%d%d%d", &sx, &sy, &ex, &ey);
}
} arr[N];
struct StoreType
{
int key, index;
StoreType() {}
StoreType(int _key, int _index)
{
key = _key, index = _index;
}
inline bool operator <(const StoreType &t) const
{
return key < t.key;
}
} ;
vector<StoreType> store[N * ];
int n;
int m, next[N * ];
int dp[N], from[N], que[N];
int ans, start; inline void Input()
{
scanf("%d", &n);
for(int i = ; i < n; i++)
{
arr[i].Read();
arr[i].index = i;
}
} inline void Relabel(DataType *data, int n, int &m)
{
static int arr[N * ], len;
#define GetIndex(x) (lower_bound(arr, arr + len, x) - arr)
len = ;
for(int i = ; i < n; i++)
{
arr[len++] = data[i].sx;
arr[len++] = data[i].sy;
arr[len++] = data[i].ex;
arr[len++] = data[i].ey;
}
arr[len++] = ;
sort(arr, arr + len);
len = unique(arr, arr + len) - arr;
for(int i = ; i < n; i++)
{
data[i].sx = GetIndex(data[i].sx);
data[i].sy = GetIndex(data[i].sy);
data[i].ex = GetIndex(data[i].ex);
data[i].ey = GetIndex(data[i].ey);
}
m = len;
} inline int Find(int x)
{
static int path[N * ];
int len = ;
for(; next[x] != x; x = next[x]) path[len++] = x;
for(int i = ; i < len; i++)
next[path[i]] = x;
return x;
} inline void Bfs()
{
int head = , tail = ;
que[tail++] = n - ;
dp[n - ] = , from[n - ] = n;
start = -;
while(head < tail)
{
int idx = que[head++];
int x = arr[idx].sx, y = arr[idx].sy;
if(!x && !y)
{
start = idx;
return;
}
for(int tab = Find(x); tab < m; tab = Find(tab + ))
{
while(!store[tab].empty() && store[tab].back().key >= y)
{
int idy = store[tab].back().index;
que[tail++] = idy;
dp[idy] = dp[idx] + , from[idy] = idx;
store[tab].pob();
if(store[tab].empty()) next[tab] = tab + ;
}
}
}
} inline void Solve()
{
Relabel(arr, n, m);
// printf("relabel"); for(int i = ; i < n - ; i++)
store[arr[i].ex].pub(StoreType(arr[i].ey, i));
for(int i = ; i < m; i++)
if(store[i].empty()) next[i] = i + ;
else
{
next[i] = i;
sort(store[i].begin(), store[i].end());
}
next[m] = m;
// printf("initiazation"); Bfs();
// printf("bfs"); if(start == -)
{
puts("-1");
return;
}
printf("%d\n", dp[start]);
vector<int> ans;
for(int x = start; x < n; x = from[x]) ans.pub(x);
printf("%d", ans.front() + );
for(int i = ; i < dp[start]; i++) printf(" %d", ans[i] + );
puts("");
} int main()
{
freopen("a.in", "r", stdin);
Input();
Solve();
return ;
}
CF#335 Board Game的更多相关文章
- CF#335 Freelancer's Dreams
		
Freelancer's Dreams time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
 - CF#335 Intergalaxy Trips
		
Intergalaxy Trips time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
 - CF#335 Lazy Student
		
Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
 - CF#335 Sorting Railway Cars
		
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
 - CF #335 div1 A. Sorting Railway Cars
		
题目链接:http://codeforces.com/contest/605/problem/A 大意是对一个排列进行排序,每一次操作可以将一个数字从原来位置抽出放到开头或结尾,问最少需要操作多少次可 ...
 - CF 1477A. Nezzar and Board
		
传送门 思路: 从k = 2 * x - y ==> 2 * x = k + y ,可以看出x是k,y的中间值,则如果存在x1,x2,且x1 = x2 ± 1,则通过x1,x2可以得到所有整数, ...
 - CF Round#240题解
		
第一次参加CF的比赛,MSK19.30,四个小时的时差真心累,第一次CODE到这么夜-- 一开始做了A,C两题,后来做B题的时候我体力和精神集中度就很低了,导致一直WA在4-- 今天起床后再刷B,终于 ...
 - 【打CF,学算法——二星级】Codeforces Round #313 (Div. 2) B. Gerald is into Art(水题)
		
[CF简单介绍] 提交链接:http://codeforces.com/contest/560/problem/B 题面: B. Gerald is into Art time limit per t ...
 - 移植MarS Board代码到内核3.0.35
		
MarS Board提供的出厂Linux内核是3.0.15的.而Freescale的BSP都早已经更新到3.0.35.为了跟上节奏,我花了点时间把关于marsboard代码从3.0.15移植到了Fre ...
 
随机推荐
- “无法更新EntitySet“*****”,因为它有一个DefiningQuery,而元素中没有支持当前操作的元素”问题的解决方法
			
百思不得其解,最后发现 1:实体中的表必须有主键(数据库中的表必须有主键),如果没有,会有这样的提示 2:主键设置好后,运行还是会出现类似问题,那就一个郁闷 1):方法一:先从EF中删除刚设置主键的模 ...
 - C#冒泡排序
			
C#最简单的冒泡排序,需要的朋友可作参考: 思路: 使用两个for循环,就可以遍历数组,这样就可以确保每个数组元素都被使用 对比前后两个数,将小的数字和大的交换位置,引入一个临时变量temp来进行交换 ...
 - ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(四) 之 用户搜索(Elasticsearch),加好友流程(1)。
			
前面几篇基本已经实现了大部分即时通讯功能:聊天,群聊,发送文件,图片,消息.不过这些业务都是比较粗犷的.下面我们就把业务细化,之前用的是死数据,那我们就从加好友开始吧.加好友,首先你得知道你要加谁.L ...
 - 《Thinking in Java》十七章_容器深入研究_练习14(Page486)
			
练习14 Properties的继承树如下:
 - cache buffers
			
buffers缓冲,可以型象的理解为漏斗.如果有大量的数据要写入磁盘,由于数据量很大,磁盘不能一下子接收,所以这个时候,就有了buffer这个漏斗,先把数据放入这个漏斗里面,然后让它慢慢的注入磁盘,这 ...
 - 对Object类中方法的深入理解
			
看一下API中关于Object的介绍: 类 Object 是类层次结构的根类.每个类都使用 Object 作为超类.所有对象(包括数组)都实现这个类的方法. 那么Object中到底有哪些方法,各自有什 ...
 - Windows和Windows Phone应用终于可以使用FFmpeg了
			
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:曾经在Windows Phone上想开发一个支持多种格式的媒体播放器是比较困难的一件事 ...
 - win7 快捷键
			
F F1 显示辅助 F2 重命名选定项目 F3 搜索文件或文件夹 F4 在 Windows 资源管理器中显示地址栏列表 F5 刷新活动窗口 F6 在窗口中或桌面上循环切换屏幕元素 F10 激活活动程序 ...
 - SQLServer两张表筛选相同数据和不同数据
			
概述 项目中经常会对两张数据库表的数据进行比较,选出相同的数据或者不同的数据.在SQL SERVER 2000中只能用Exists来判断,到了SQL SERVER 2005以后可以采用EXCEPT和I ...
 - MapReduce中的分区方法Partitioner
			
在进行MapReduce计算时,有时候需要把最终的输出数据分到不同的文件中,比如按照省份划分的话,需要把同一省份的数据放到一个文件中:按照性别划分的话,需要把同一性别的数据放到一个文件中.我们知道最终 ...