codeforce 310 div2 D题 Case of Fugitive

题目

给n个岛屿和m座桥,问桥是否足够把两两相邻的岛屿连起来,当一座桥的长度大于等于相邻两个岛屿上的点的距离最小值并且小于等于相邻两个岛屿上的点的距离最大值的时候,就可以把他们连起来。

用贪心的方法,把岛屿按照距离的最小值(即l[i+1]-r[i])排序,把桥按照长度排序,显然距离最小的岛屿应该用长度最小的桥来接,这是第一个贪心。第二个贪心是当前岛屿可以连接的所有桥中应该选距离最大值最小的。仔细想想这两个贪心思路是对的。最后看是否所有的岛屿都被连起来就行了。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/
struct Node
{
    long long int l,r;
    int id;
    bool operator<( Node b)const
    {
        return r<b.r;
    }
    bool operator>( Node b)const
    {
        return r>b.r;
    }
};
Node isl[200000+5];
struct A
{
    long long int  num;
    int id;
};
A a[200000+5];
int ans[200000+5];
bool cmp(const Node& a,const Node &b)
{
    return a.l<b.l;
}
bool cmp1(const A&a,const A&b)
{
    return a.num<b.num;
}
priority_queue<Node,vector<Node>,greater<Node> >q;
int main()
{int n,m;
RII(n,m);
long long int l1,l2,r1,r2;
scanf("%I64d%I64d",&l1,&r1);
REP(i,1,n)
{scanf("%I64d%I64d",&l2,&r2);
isl[i].l=l2-r1;
isl[i].r=r2-l1;
l1=l2;
r1=r2;
isl[i].id=i;
}
REP(i,1,m+1)
{
   scanf("%I64d",&a[i].num);
   a[i].id=i;
}
sort(isl+1,isl+n,cmp);
sort(a+1,a+m+1,cmp1);
int i,j;
int flag=0;
for(i=1,j=1;i<=m;i++)
{
    while(j<n&&isl[j].l<=a[i].num)
    {
        q.push(isl[j]);
        j++;
    }
    if(q.empty())
        continue;
    Node tt=q.top();
    q.pop();
    if(tt.r>=a[i].num)
    {ans[tt.id]=a[i].id;

    }
    else
    {
        flag=1;
        break;
    }
}
if(flag||j<n||!q.empty())
{
    printf("No\n");
}
else
{
    printf("Yes\n");
    REP(i,1,n-1)
    {printf("%d ",ans[i]);

    }
    printf("%d\n",ans[n-1]);
}

        return 0;
}
时间: 2024-10-15 04:07:50

codeforce 310 div2 D题 Case of Fugitive的相关文章

Round 310(Div.1) B. Case of Fugitive

Round 310(Div.1) B. Case of Fugitive Andrewid the Android is a galaxy-famous detective. He is now chasing a criminal hiding on the planet Oxa-5, the planet almost fully covered with water. The only dry land there is an archipelago of n narrow islands

#310 (div.2) D. Case of Fugitive

1.题目描述:点击打开链接 2.解题思路:本题利用贪心法+优先队列解决.不过本题的贪心策略的选取是关键,有些看似正确的贪心策略实际上暗含危险.先说说正确的贪心策略:将所有的岛按照顺序求出第i个岛和i+1个岛之间桥的最小最大长度,并按照L从小到大排序,若相同则按照R从小到大排序.然后对桥由小到大排序,将所有的桥扫描一遍,枚举第i个桥时,将L值小于等于当前桥的区间按照(R,id)放入优先队列,R小的在队首,大的在队尾,每次看队首的R是否大于等于len,若满足,则记录答案,break,若不存在,则无解

贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

题目传送门 1 /* 2 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 3 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0),先从1开始找到已经套好的娃娃层数, 4 其他是2次操作,还要减去k-1个娃娃是只要套上就可以 5 详细解释:http://blog.csdn.net/firstlucker/article/details/46671251 6 */ 7 #include <cstdio> 8 #i

codeforce 285 div2 D 题解

codeforce 285 div2 D 题解 说明 这道题目是看了思路分析才知道的,关键问题在于数据量过大,需要快速检索的同时不能辅助空间过大. 因此利用了下面3种方法结合解决该问题 康拓展开与逆康拓展开 树状数组 二分查找 代码 /** * @brief Codeforces Round #285 (Div. 2) d * @file d.cpp * @author mianma * @created 2015/01/27 18:18 * @edited 2015/01/29 22:45 *

Codeforces 556D - Case of Fugitive

556D - Case of Fugitive 思路:将桥长度放进二叉搜索树中(multiset),相邻两岛距离按上限排序,然后二分查找桥长度匹配并删除. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long const int N=2e5+5; struct node { ll x; ll y; int id; bool operator <(const node &a)const { ret

找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

题目传送门 1 /* 2 找规律/贪心:ans = n - 01匹配的总数,水 3 */ 4 #include <cstdio> 5 #include <iostream> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 using namespace std; 10 11 const int MAXN = 2e5 + 10; 12 const int INF =

构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

题目传送门 1 /* 2 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 3 构造:先求出使第1个指向0要多少步,按照这个次数之后的能否满足要求 4 题目读的好累:( 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #i

codeforces round#259 div2 B题(KMP)

先上链接:http://codeforces.com/contest/454/problem/B B. Little Pony and Sort by Shift time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output One day, Twilight Sparkle is interested in how to sort a se

codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元组[ll,rr]排序(ll相同时再rr),长度x排序(升序).一个全局优先队列pq(rr小的顶部).for循环,对每个x,将ll比它小的放入优先队列pq,如果pq仍为空,说明这块桥用不上,不为空,看top的rr是否大于x,如果大于,这块桥就能用上,并且给当前的top一定是可行的. 乱码: #pragma co