您的位置:首页 > 其它

[二分图匹配 贪心] BZOJ 4276 [ONTAK2015]Bajtman i Okrągły Robin && BZOJ 2034 [2009国家集训队]最大收益

2016-08-05 14:47 537 查看
%%%dyh

按强盗从大到小排序,贪心选取每个强盗能不能抓。
判断一些强盗能不能抓完,可以按左端点排序,使用优先队列维护右端点。
贪心算法的正确性: 考虑匈牙利算法,从大到小一个一个匹配,一个点一旦在匹配中,那么一直在匹配里面。

也可以直接用拟阵证明 但我不会

堆的复杂度是n2logn 有线性的判断方法

我们从小到大扫描所有的位置,如果这个位置没有分配任务,那么直接分配就行了;否则我们根据贪心进行考虑:

如果新任务的终点位置<<当前位置分配任务的终点位置,那么让这个位置分配新任务,在这个位置后面去给这个位置原来的任务寻找匹配;

否则在后面的位置给新任务寻找匹配。

4276右端点记得减一

2034要离散化

4276

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;

inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}

inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=5005;

struct abcd{
int l,r,val;
bool operator < (const abcd &B) const{
return r>B.r;
}
}A
,S
,T
,tem;
bool cmpv(abcd A,abcd B){ return A.val>B.val; }
bool cmpl(abcd A,abcd B){ return A.l<B.l; }

int n,pnt,clk,ans;
priority_queue<abcd> Q;

inline bool check()
{
while (!Q.empty()) Q.pop();
memcpy(T,S,sizeof(S));
sort(T+1,T+pnt+1,cmpl);
int p=1;
for (int i=1;i<=clk;i++)
{
while (p<=pnt && T[p].l==i)
Q.push(T[p]),p++;
if (!Q.empty())
{
tem=Q.top();
if (tem.r<i) return 0;
Q.pop();
}
}
return Q.empty();
}

int boy
;
bool match(int x,int now,int ed){
if (now>ed || now>clk) return 0;
int t=boy[now];
if (!t)
return boy[now]=x,1;
if (A[x].r<A[t].r){
if (match(t,now+1,A[t].r))
return boy[now]=x,1;
else
return 0;
}
else
return match(x,now+1,ed);
}

int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n);
for (int i=1;i<=n;i++) read(A[i].l),read(A[i].r),read(A[i].val),A[i].r--,clk=max(clk,A[i].r);
sort(A+1,A+n+1,cmpv);
for(int i=1;i<=n;i++)
if (match(i,A[i].l,A[i].r))
ans+=A[i].val;
printf("%d\n",ans);
return 0;
}

2034

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;

inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}

inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=5005;

struct abcd{
int l,r,val;
bool operator < (const abcd &B) const{
return r>B.r;
}
}A
,S
,T
,tem;
bool cmpv(abcd A,abcd B){ return A.val>B.val; }
bool cmpl(abcd A,abcd B){ return A.l<B.l; }

int n,pnt; ll ans;

int icnt,sx
;

int boy
;
bool match(int x,int now,int ed){
if (now>ed || now>=icnt) return 0;
int t=boy[now];
if (!t)
return boy[now]=x,1;
if (A[x].r<A[t].r){
if (match(t,now+1,A[t].r))
return boy[now]=x,1;
else
return 0;
}
else
return match(x,now+1,ed);
}

int main()
{
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n);
for (int i=1;i<=n;i++) read(A[i].l),read(A[i].r),read(A[i].val);
for(int i=1;i<=n;i++) sx[++icnt]=A[i].l;
sort(sx+1,sx+icnt+1);
for(int i=2;i<=icnt;i++) sx[i]=max(sx[i-1]+1,sx[i]);
icnt=unique(sx+1,sx+icnt+1)-sx-1;
sx[++icnt]=1<<30;
for(int i=1;i<=n;i++){
A[i].l=lower_bound(sx+1,sx+icnt+1,A[i].l)-sx;
A[i].r=upper_bound(sx+1,sx+icnt+1,A[i].r)-sx-1;
}
sort(A+1,A+n+1,cmpv);
for(int i=1;i<=n;i++)
if (match(i,A[i].l,A[i].r))
ans+=A[i].val;
printf("%lld\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: