先看题目:
T1:数正方形,数学签到题,找规律;
T2:取数,看起来很眼熟的一道题,并且何某8.22时告诫我们要考动规。所以显然。。。不是动规。动规动规念着念着就成“堆”了。
T3:葡萄酒交易,唯一的一道动规+最小生成树+状态亚索。
T1:
在n * n的点阵中任取4个点,回答:
问题1:这4个点恰好是“正放”的正方形的4个顶点的方案数是多少?
问题2:这4个点恰好是正方形(包括“正放”和“斜放”)的4个顶点的方案数是多少?
下图为一个4*4的点阵,上图表示一种“正放”的方案,下图表示一种“斜放”的方案。
int main()
{
//freopen("count.in","r",stdin);
//freopen("count.out","w",stdout);
re(n);re(k);
for(i=1;i<=n-1;i++)ans1+=(n-i)*(n-i);
for(i=1;i<=n-2;i++)ans2+=i*i*((n-1)-i);
if(k==1)printf("%I64d",ans1%mod);
else printf("%I64d",(ans1+ans2)%mod);
}
随便找找规律就可以了。
T2:n个整数组成的一个环,现在要从中取出m个数,取走一个数字就不能取跟它相邻的数字(相邻的数不能同时取)。要求取出的数字的总和尽可能大,问这个最大和是多少? 如果无解,请输出“Error!”
输入 : 第一行包含两个正整数n、m。 第二行为n个整数Ai。
输出:仅一个整数,表示所求结果。如果无解输出“Error!”,不包含引号。
样例数据:
in:7 3 1 2 3 4 5 6 7
out:15
in:7 4 1 2 3 4 5 6 7
out:Error!
in:8 4 8 5 6 2 3 4 8 9
out:25
炸一看是动规,不过一看动规是O(2n方),怎么就变成堆了呢?
利用结构体,分别记录每个数的编号,值,左边值,右边值,然后放进一个大根堆里面。每次取出堆顶的数,标记左右不可选的数,用ans把它加起来。注意还没完,现在要生成一个新的数,值为(左+右-当前数值),并且更新左数和右数,如此,若当前选择并非最优,就可以重新选择。
这。只能膜拜想出这个方法的神犇为敬了。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<cmath>
using namespace std;
priority_queue<pair<int,int> >q;
const int MAX=-999999999;
const int maxn=400200;
inline void re(int &d)
{
char t=getchar();bool f=false;
while(t<'0'||t>'9'){if(t=='-')f=true;t=getchar();}
for(d=0;t>='0'&&t<='9';t=getchar())d=d*10+t-'0';
if(f==true)d=-d;
}
int n,m,a,b,c,d,e,ans=0,tot=0;
int p[maxn],l[maxn],r[maxn];
bool used[maxn];
int main()
{
re(n);re(m);
for(a=1;a<=n;a++)
{
re(p[a]); //录入值
l[a]=a-1;r[a]=a+1; //记录左边和右边的编号
q.push(make_pair(p[a],a));
tot++;
}
l[1]=n;r[n]=1;
if(m>(n/2)){printf("Error!");return 0;}
while(m!=0)
{
a=q.top().second;q.pop();
if(used[a]==true)continue;
ans+=p[a];
tot++;
p[tot]=p[l[a]]+p[r[a]]-p[a]; //建立新数,用于重新选择
used[a]=true;used[l[a]]=true;used[r[a]]=true;//标记为已使用;
l[tot]=l[l[a]];r[tot]=r[r[a]];
l[r[r[a]]]=tot;r[l[l[a]]]=tot; //更新一些乱七八糟的
q.push(make_pair(p[tot],tot)); //新数入堆
m--;
}
printf("%d",ans);
return 0;
}
T3:
啊不想写了,状态亚索记录某点是否在集合之中,然后用最小生成树乱搞一下就可以了。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int inf=1e9;
const int maxn=(1<<17);
struct node{
int x,y,v;
}wine[200];
int i,j,n,m,toot,a[20],father[20],sum[maxn],cost[maxn],f[maxn];
bool cmp(node x,node y){return x.v<y.v;}
inline void re(int &d)
{
bool f=false;char t=getchar();
while(t<'0'||t>'9'){if(t=='-')f=true;t=getchar();}
for(d=0;t>='0'&&t<='9';t=getchar())d=(d<<3)+(d<<1)+t-'0';
if(f==true)d=-d;
}
int getfather(int x)
{
if(x!=father[x])father[x]=getfather(father[x]);
return father[x];
}
int kru(int s)
{
int i,tot=0,mincost=0,cnt=0;
for(i=0;i<n;i++)
{
father[i]=i;
if((s>>i)&1)tot++;
}
for(i=1;i<=m;i++)
{
if(((s>>wine[i].x)&1)&&((s>>wine[i].y)&1))
{
int fx=getfather(wine[i].x);int fy=getfather(wine[i].y);
if(fx!=fy)
{
father[fx]=fy;
cnt++;
mincost+=wine[i].v;
}
}
}
if(cnt+1!=tot)return inf;
else return mincost;
}
int main()
{
re(n);re(m);
for(i=0;i<n;i++)re(a[i]);
for(i=1;i<=m;i++)
{
re(wine[i].x);re(wine[i].y);re(wine[i].v);
}
sort(wine+1,wine+1+m,cmp);
toot=(1<<n)-1;
for(i=0;i<=toot;i++)
for(j=0;j<n;j++)
{
if((i>>j)&1)sum[i]+=a[j];
}
for(i=0;i<=toot;i++)
{
if(sum[i]==0)cost[i]=kru(i);
else cost[i]=inf;
}
for(i=1;i<=toot;i++)f[i]=inf;
f[0]=0;
for(i=0;i<=toot;i++)
{
if(sum[i]!=0)continue;
for(j=1;j<=toot;j++)
{
if(sum[j]!=0)continue;
f[i|j]=min(f[i|j],f[i]+cost[j]);
}
}
if(f[toot]==inf)printf("Impossible\n");else printf("%d\n",f[toot]);
return 0;
}
就这样吧。