给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