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 ...
随机推荐
- 【转】深入Windows内核——C++中的消息机制
上节讲了消息的相关概念,本文将进一步聊聊C++中的消息机制. 从简单例子探析核心原理 在讲之前,我们先看一个简单例子:创建一个窗口和两个按钮,用来控制窗口的背景颜色.其效果 图1.效果图 Win32 ...
- 苹果应用 Windows 申请 普通证书 和Push 证书 Hbuilder 个推(2)
s上一篇 讲述了android 如何打包,这一篇 看一下如何IOS下打包 在苹果上申请证书,及其麻烦,我写下来,有需要的直接拿走即可: 首先 苹果的证书分两种 一种是 development 证书,另 ...
- Linux 查看 删除进程
这东西,时间久不用总容易忘....记下来! 1. 在 LINUX 命令平台输入 1-2 个字符后按 Tab 键会自动补全后面的部分(前提是要有这个东西,例如在装了 tomcat 的前提下, 输入 to ...
- poj 3661 Running
题意:给你一个n,m,n表示有n分钟,每i分钟对应的是第i分钟能跑的距离,m代表最大疲劳度,每跑一分钟疲劳度+1,当疲劳度==m,必须休息,在任意时刻都可以选择休息,如果选择休息,那么必须休息到疲劳度 ...
- AFNetworking请求设置请求头
NSString *url = @"INPUT URL HERE"; AFHTTPRequestOperationManager *manager = [AFHTTPRequest ...
- Maven+druid+MyBatis+Spring+Oracle+Dubbo开发环境搭建
1.开发工具使用: MyEclipse或Eclipse,数据库使用Oracle.需要用到的软件有Zookeeper(注册中心),Tomcat(Web容器)和Maven(包管理). 2.初始环境配置: ...
- CSS3–1.css3 新增选择器
1.后代级别选择器 2.同辈级别选择器 3.伪类选择器 4.属性选择器 5.UI伪类选择器 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 T ...
- 无废话ExtJs 入门教程十二[下拉列表联动:Combobox_Two]
无废话ExtJs 入门教程十二[下拉列表联动:Combobox_Two] extjs技术交流,欢迎加群(201926085) 不管是几级下拉列表的联动实现本质上都是根据某个下拉列表的变化,去动态加载其 ...
- 7z命令行工具
7z (中文)是优秀开源的压缩解压缩软件(wiki: en 中文),有windows版本与linux版本,最新的9.32版本支持的格式包括: 压缩与解压缩均支持:7z, XZ, BZIP2, GZI ...
- [LeetCode] Word Break II (TLE)
Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each ...