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 ...
随机推荐
- clustershell
.安装 yum install clustershell .配置ssh无密码登录 .配置/etc/hosts 在hosts中文件中将ip和主机名对应起来,使用比较方便 .配置关键文件 clusters ...
- 公共增删改查(MVC+三层架构)
1.建立数据访问层:新建一个项目,选择类库,命名为XXXDAL,然后把新生成的类删除,重新建一个类BaseDal,代码如下: public class BaseDal<T> where T ...
- poj1611(并查集)
题目链接:http://poj.org/problem?id=1611 题意: SARS(非典型肺炎)传播得非常厉害,其中最有效的办法是隔离那些患病.和患病者接触的人.现在有几个学习小组,每小组有几个 ...
- 使用python递归子目录处理日志文件
重要说明: (1)python使用4个空格进行层次缩进的(不是tab),在eclipse里面可以直接使用tab缩进,是因为eclipse会实时地将tab转成4个空格 (2)在eclipse中安装pyD ...
- .NET yield
.Net Yield 其实比较简单,手动yield,一学就会. public static class GalaxyClass { public static void ShowGalaxies() ...
- hdu 5291 dp+优化 ****
多校实在高能 题解链接 题意:有n中糖果,每种糖果有ai个.分给A,B两个人.两人的糖果要一样多,可以都是0,1......m个.同一种糖果没有区别. 问有几种分法. 定义dp[i]表示两人之间相差i ...
- [Tools] 使用work2013发布博客
参考园子里推荐的方式,觉得使用word发布挺好的,尝试了一下,还不错,记录下来备用 参考连接: http://www.cnblogs.com/liuxianan/archive/2013/04/1 ...
- 在Entity Framework 7中进行数据迁移
(此文章同时发表在本人微信公众号“dotNET每日精华文章”,欢迎右边二维码来关注.) 题记:虽然EF7重新设计了Entity Framework,不过也还是能够支持数据迁移的. Entity Fra ...
- 并发异步处理队列 .NET 4.5+
namespace Test { using System; using System.Threading; using System.Threading.Tasks; using Microshao ...
- pythonchallenge之C++学习篇-03
提示说一个小写字母两面精确地被大写字母包围,应该指的是周围没有四个而仅仅这两个像这样的:xXXXxXXXx的中间的那个应该是符合条件的 好了标题是re,提示该是使用正则表达式,网页源码里有待处理的字符 ...