被虐爆了。。。贪心这种玄学东西还可以证吗??除了范围缩放算是可以想想比较经典(倍增第一题?)。。。

poj3614:这道题想了很久,并没有想到是把minSPF按大到小排序,一直的思想是小的就小到大排序。后来仔细分析下,这题做法是n^2的,假如排序是要控制一个值的单调性,再用一个for来贪心,假如是小到大排序,选取时应该取大的还是应该取小的?直观上想选小的,因为后面的下界大,但是,假如当前位置区间很大而后一个很小就出问题了。假如是大到小排序,相当于逆过来,此时尽量取大,是基于当前的上界的,对于下界已经没有必要关注了,因为是单调递减的。考虑到这题没有权值,也可以看作全为1,当前的最大和后面的匹配无法得到更优解。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; struct node
{
int x,y;
}a[],b[];
bool cmp(node n1,node n2){return n1.x>n2.x;}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d%d",&a[i].x,&a[i].y);
for(int i=;i<=m;i++)scanf("%d%d",&b[i].x,&b[i].y);
sort(a+,a+n+,cmp);
sort(b+,b+m+,cmp); int ans=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
if(a[i].x<=b[j].x&&b[j].x<=a[i].y&&b[j].y>)
{
b[j].y--;ans++;
break;
}
}
printf("%d\n",ans);
return ;
}

poj3614

poj3190:这题倒是没什么。。。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std; struct node
{
int x,y,z;
friend bool operator>(node q1,node q2){return q1.y>q2.y;}
}a[];priority_queue< int,vector<node>,greater<node> >q;
bool cmp(node n1,node n2){return n1.x<n2.x;} int bel[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y), a[i].z=i;
sort(a+,a+n+,cmp); int ans=;q.push(a[]);bel[a[].z]=;
for(int i=;i<=n;i++)
{
node t=q.top();
if(t.y<a[i].x)q.pop(),bel[a[i].z]=bel[t.z];
else ans++,bel[a[i].z]=ans;
q.push(a[i]);
}
printf("%d\n",ans);
for(int i=;i<=n;i++)printf("%d\n",bel[i]);
return ;
}

poj3190

poj1328:也算是小小的套路题吧。在越后放越好,“决策包容性”虽然运用的不是挺灵活,但是还蛮好想(所以这题难度在转换问题变成在n个区间里都要有至少一个点??)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; struct node
{
double l,r;
}a[];
bool cmp(node n1,node n2)
{
if(fabs(n1.l-n2.l)<1e-)return n1.r<n2.r;
return n1.l<n2.l;
} bool v[];
int main()
{
int n,T_T=;double R;
while(scanf("%d%lf",&n,&R)!=EOF)
{
if(n==&&R==)break;
T_T++; double x,y;bool bk=true;
for(int i=;i<=n;i++)
{
scanf("%lf%lf",&x,&y);
if(y>R)bk=false;
double dis=(sqrt((R*R)-(y*y)));
a[i].l=x-dis;a[i].r=x+dis;
}
if(bk==false){printf("Case %d: -1\n",T_T);continue;}
sort(a+,a+n+,cmp); int ans=;
memset(v,false,sizeof(v));
for(int i=;i<=n;i++)
{
if(v[i]==false)
{
ans++;v[i]=true;double pos=a[i].r;
for(int j=i+;j<=n;j++)
if(a[j].l<=pos)v[j]=true,pos=min(pos,a[j].r);
else break;
}
}
printf("Case %d: %d\n",T_T,ans);
}
return ;
}

poj1328

upd:我之前写的实在是太不优秀了。

把区间弄出来以后,相当于用最小的点覆盖所有区间,那么对于区间相互包含,不用管,对于区间分开没有交集,直接新开

那么就要处理覆盖一部分的情况。按L排序,决策包容,能选就选,L区间单调增,维护个R单调减,非法就新开

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std; struct node{double L,R;}a[];
bool cmp(node n1,node n2){return n1.L<n2.L;}
int main()
{
int n,T_T=; double m,x,y;
while(scanf("%d%lf",&n,&m)!=EOF)
{
if(n==&&m==)break; bool bk=true;
for(int i=;i<=n;i++)
{
scanf("%lf%lf",&x,&y);
if(y>m)bk=false;
a[i].L=x-sqrt(m*m-y*y);
a[i].R=x+sqrt(m*m-y*y);
}
if(bk==false){printf("Case %d: -1\n",++T_T);continue;}
sort(a+,a+n+,cmp); int ans=;double R=a[].R;
for(int i=;i<=n;i++)
{
if(a[i].L>R)
ans++, R=a[i].R;
else
R=min(R,a[i].R);
}
printf("Case %d: %d\n",++T_T,ans);
}
return ;
}

poj1328(upd)

NOIP2012国王游戏:完全没有见过这种题型。。书上说微扰这个东西经常用于以排序为贪心策略的问题?反正这种数学东西证明,假如想到的话一般的还是不难推的。

顺便安利一道题bzoj3174: [Tjoi2013]拯救小矮人

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL; int n,m;LL k;
LL a[],b[],tt[];
void mergesort(int l,int r)
{
if(l==r)return ;
int mid=(l+r)/;
mergesort(l,mid);mergesort(mid+,r); int i=l,j=mid+,p=l;
while(i<=mid&&j<=r)
{
if(b[i]<=b[j])tt[p++]=b[i++];
else tt[p++]=b[j++];
}
while(i<=mid)tt[p++]=b[i++];
while(j<=r) tt[p++]=b[j++]; for(int i=l;i<=r;i++)b[i]=tt[i];
}
int clen;LL c[];
bool check(int l,int r)
{
if(r>n)return false; int blen=r-l+;
for(int i=l;i<=r;i++)b[i-l+]=a[i];
mergesort(,blen); int i=,j=,p=;
while(i<=blen&&j<=clen)
{
if(b[i]<=c[j])tt[p++]=b[i++];
else tt[p++]=c[j++];
}
while(i<=blen)tt[p++]=b[i++];
while(j<=clen)tt[p++]=c[j++]; p--;
LL sum=;
for(int i=;i<=m;i++)
{
if(p-i+<=i)break;
sum+=(tt[p-i+]-tt[i])*(tt[p-i+]-tt[i]);
} if(sum<=k)
{
clen=p;
for(int i=;i<=clen;i++)c[i]=tt[i];
return true;
}
else return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%lld",&n,&m,&k);
for(int i=;i<=n;i++)scanf("%lld",&a[i]); int ed,ans=;
for(int st=;st<=n;st=ed+)
{
ed=st;int L=;
clen=;c[++clen]=a[st];
while(L>)
{
if(check(ed+,ed+L)==true)
{
ed=ed+L;
L*=;
}
else L/=;
}
ans++;
}
printf("%d\n",ans);
}
return ;
}

NOIP2012国王游戏

poj2054:这题是真的难啊,大开眼界。。。有一个性质,树中除根以外的最大点,一定会在它的父亲被染色之后被染色,那么居然依此来合并点。此时我们的目标是先把染色顺序给弄好,选择了放弃原本的权值转而自己定义新的权值!定义为合并包含的所有点的权值平均值。是可以数学归纳法证明的(这个东西不太会啊,我只用过来证柯西不等式。。。)具体做法我就是用链表存储顺序,然后用一个并查集维护合并了的块。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL; int F[];
int findfa(int x)
{
if(F[x]==x)return x;
F[x]=findfa(F[x]);return F[x];
}
int u[],fa[];
struct node{int d,z;}c[];
bool v[];
int bef[],aft[],la[];
int main()
{
int n,R;
while(scanf("%d%d",&n,&R)!=EOF)
{
if(n==&&R==)break; for(int i=;i<=n;i++)
{
scanf("%d",&c[i].d);u[i]=c[i].d;
c[i].z=;F[i]=i;
bef[i]=aft[i]=-;la[i]=i;
}
int x,y;
for(int i=;i<n;i++)
scanf("%d%d",&x,&y), fa[y]=x; memset(v,false,sizeof(v));v[R]=true;
for(int i=;i<n;i++)
{
int id=-;
for(int j=;j<=n;j++)
if(v[j]==false)
if(id==-||c[id].d*c[j].z<c[j].d*c[id].z)id=j; v[id]=true;
int truefa=findfa(fa[id]);F[id]=truefa;
c[truefa].d+=c[id].d;
c[truefa].z+=c[id].z; bef[id]=la[truefa];
aft[la[truefa]]=id;
la[truefa]=la[id];
} LL ans=;
for(int i=;i<=n;i++)
ans+=(LL(i))*(LL(u[R])), R=aft[R];
printf("%lld\n",ans);
}
return ;
}

poj2054

0x07 贪心的更多相关文章

  1. 算法竞赛进阶指南 0x00 基本算法

    放在原来这个地方不太方便,影响阅读体验.为了读者能更好的刷题,另起一篇随笔. 0x00 基本算法 0x01 位运算 [题目][64位整数乘法] 知识点:快速幂思想的灵活运用 [题目][最短Hamilt ...

  2. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  3. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

  4. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]

    1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 786  Solved: 391[Submit][S ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. 【BZOJ-4245】OR-XOR 按位贪心

    4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 486  Solved: 266[Submit][Sta ...

  8. code vs 1098 均分纸牌(贪心)

    1098 均分纸牌 2002年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解   题目描述 Description 有 N 堆纸牌 ...

  9. 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心

    SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...

随机推荐

  1. (C++)错误提示 c2352 :非静态成员函数的非法调用

    静态成员函数相当于全局函数,只是有一个类名字空间的限制.而类成员函数是成员内部的函数,同一个类的对象实例可以有很多,每一个实例都有自已不同的成员变量值,成员函数一般都是对成员自已的成员变量值在操作.所 ...

  2. MVC:@RenderBody、@RenderPage、@RenderSection用法

    本文导读:在Razor引擎中没有了“母版页”,取而代之的是叫做“布局”的页面(_Layout.cshtml)放在了共享视图文件夹中.模板页:@RenderBody()占位符:局部页面:@RenderP ...

  3. (转)基于MVC4+EasyUI的Web开发框架形成之旅--附件上传组件uploadify的使用

    http://www.cnblogs.com/wuhuacong/p/3343967.html 大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随 ...

  4. 超酷消息警告框插件(SweetAlert)

    今天给大家推荐一款不错的超酷消息警告框–SweetAlert:SweetAlert是一款不需要jQuery支持的原生js提示框,风格类似bootstrap.它的提示框不仅美丽动人,并且允许自定义,支持 ...

  5. In-Out Parameters inout keyword

    You write an in-out parameter by placing the inout keyword right before a parameter’s type. An in-ou ...

  6. Spring依赖注入:@Autowired,@Resource和@Inject区别与实现原理

    一.spring依赖注入使用方式 @Autowired是spring框架提供的实现依赖注入的注解,主要支持在set方法,field,构造函数中完成bean注入,注入方式为通过类型查找bean,即byT ...

  7. Javase 简单代码练习

    public class Test10 { public static void main(String[] args) { System.out.println("------------ ...

  8. hibernate注解--@transient

    @transient:表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性. 如果一个属性并非数据库表的字段映射,就务必将其标示为@Transient,否则,ORM框架默认其注解为@Bas ...

  9. a标记地址的几种用法

    1.<a href="tel://号码"></a> 手机使用能自动拨打电话 //可以省略 2.<a href="mailto://邮箱&qu ...

  10. jenkins+testlink+python搭建自动化测试环境

    一. 环境搭建 jenkins安装与配置请参考我的另一篇博文:https://www.cnblogs.com/wuxunyan/p/9592953.html testlink安装请参考博文:https ...