Board Game
time limit per test

2.5 seconds

memory limit per test

256 megabytes

input

standard input

output

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, aibici 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).

Input

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 aibicidi (0 ≤ ai, bi, ci, di ≤ 109) — the characteristics of the corresponding card.

Output

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.

Sample test(s)
input
4
0 0 3 4
2 2 5 3
4 1 1 7
5 3 8 8
output
3
1 2 4
input
2
0 0 4 6
5 1 1000000000 1000000000
output
-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的更多相关文章

  1. CF#335 Freelancer's Dreams

    Freelancer's Dreams time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  2. CF#335 Intergalaxy Trips

     Intergalaxy Trips time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. CF#335 Lazy Student

    Lazy Student time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  4. CF#335 Sorting Railway Cars

    Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. CF #335 div1 A. Sorting Railway Cars

    题目链接:http://codeforces.com/contest/605/problem/A 大意是对一个排列进行排序,每一次操作可以将一个数字从原来位置抽出放到开头或结尾,问最少需要操作多少次可 ...

  6. CF 1477A. Nezzar and Board

    传送门 思路: 从k = 2 * x - y ==> 2 * x = k + y ,可以看出x是k,y的中间值,则如果存在x1,x2,且x1 = x2 ± 1,则通过x1,x2可以得到所有整数, ...

  7. CF Round#240题解

    第一次参加CF的比赛,MSK19.30,四个小时的时差真心累,第一次CODE到这么夜-- 一开始做了A,C两题,后来做B题的时候我体力和精神集中度就很低了,导致一直WA在4-- 今天起床后再刷B,终于 ...

  8. 【打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 ...

  9. 移植MarS Board代码到内核3.0.35

    MarS Board提供的出厂Linux内核是3.0.15的.而Freescale的BSP都早已经更新到3.0.35.为了跟上节奏,我花了点时间把关于marsboard代码从3.0.15移植到了Fre ...

随机推荐

  1. NYOJ题目273字母小游戏

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAswAAAIZCAIAAAA9QP7RAAAgAElEQVR4nO3dPXKjzBqG4bMJ5VqIU2

  2. 第一个JAVA创建

    1.file-new-java project  创建项目文件夹 2.在项目文件夹new-class 3.java对大小写比较敏感 输入代码 public class HELLOWORD { publ ...

  3. CSS学习笔记----CSS3自定义字体图标

    响应式网页字体图标 作者:大漠 日期:2014-01-28 点击:3220 @font-face Responsive 本文由大漠根据Jason的<Responsive Webfont Icon ...

  4. SQLServer基本查询

    条件查询 --1.比较运算符 --2.确定集合谓词 --3.确定范围谓词 , ) --4.字符匹配谓词 select * from dbo.DepartMent where dName like 'C ...

  5. Linux下pipe使用注意事项

    转自:http://blog.yufeng.info/archives/1485 Linux下的pipe使用非常广泛, shell本身就大量用pipe来粘合生产者和消费者的. 我们的服务器程序通常会用 ...

  6. SQL小纸条--一些方便平时参考的SQL语句--随用随查

    SQL 语句 语句 语法 AND / OR SELECT column_name(s)FROM table_nameWHERE conditionAND|OR condition ALTER TABL ...

  7. Shell编程基础教程6--shell函数

    6.shell函数    6.1.定义函数        简介:            shell允许将一组命令集或语句形成一个可用块,这些块成为shell函数        定义函数的格式      ...

  8. EChart使用简单介绍

    Echart是百度研发团队开发的一款报表视图JS插件,功能十分强大,使用内容做简单记录:(EChart下载地址 http://echarts.baidu.com/download.html) 1.ti ...

  9. mysql5.7.11安装配置

    1.下载安装包. mysql-5.7.11版本: http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.11-winx64.zip 2.拷贝到任意盘: ...

  10. go-martini 简单分析之二

    martini.go 对路由采用正则表达式处理,最终转化成正则表达式. 添加route对应的调用栈 按照生成,验证,添加的步骤 route := newRoute(method, pattern, h ...