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. 素数环(dfs+回溯)

    题目描述: 输入正整数n,把整数1,2...n组成一个环,使得相邻两个数和为素数.输出时从整数1开始逆时针排列并且不能重复: 例样输入: 6 例样输出: 1 4 3 2 5 6 1 6 5 2 3 4 ...

  2. NYOJ之猴子吃桃问题

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAIMCAIAAACIcqa9AAAgAElEQVR4nO3dO3Li3BaG4TMJcgbi1A

  3. Java中常见数据结构:list与map -底层如何实现

    1:集合 2 Collection(单列集合) 3 List(有序,可重复) 4 ArrayList 5 底层数据结构是数组,查询快,增删慢 6 线程不安全,效率高 7 Vector 8 底层数据结构 ...

  4. CLR via C#(11)-无参属性、有参数属性(索引器)

    一. 无参属性 1. 定义属性 无参属性就是我们最常见的属性方式,在赋值时可以加入一定的逻辑判断.属性的定义其实不复杂,先看个直观的例子: 说明: 属性要定义名称和类型,且类型不能是void. 属性是 ...

  5. SVM 最大间隔目标优化函数(NG课件2)

        目标是优化几何边距, 通过函数边距来表示需要限制||w|| = 1     还是优化几何边距,St去掉||w||=1限制转为普通函数边距     更进一步的,可以固定函数边距为1,调节||w| ...

  6. Ext Js【Hello World】 ——4.1 beta 1

    准备:vs+ExtJs4.1Beta1 ExtJS 4.1  xiazai_ https://yunpan.cn/cqv6bdBwtRjAj (提取码:2733) 引用,cs文件,js主入口,zh—c ...

  7. jQuery Mobile学习之grid、等待显示的ajax效果、页面跳转、页面跳转传递参数等(二)

    Index.cshtml <!-- Start of second page --> <section data-role="page" id="bar ...

  8. 【131031】rel 属性 -- link标签中的rel属性,定义了文档与链接的关系

    此属性通常出现在a,link标签中 属性值 Alternate -- 定义交替出现的链接 Alternate 属性值 -- alternate是LinkTypes的一个值,网页设计者可以通过此值,设计 ...

  9. .Net Ioc Unity

    Unity 的接口IUnityContainer public interface IUnityContainer : IDisposable IUnityContainer RegisterType ...

  10. [Tips] JavaScript 使用hash 对象传参

    转自Web 前端开发修炼之道. 在JavaScript 中funciton 包含多个参数的时候,我们想要实现可选参数的功能,传很多个null 其实是个很讨厌的事情,这个时候就可以使用这个技巧. 具体见 ...