BZOJ 1666 水..
BZOJ 1579 分层图最短路.
BZOJ 1782 从一开始若某头牛停在U,那么U的子树的时间都会加一用BIT维护DFS序就行了
BZOJ 1572
贪心+堆 排序后查看是否超过了时间,超过了就弹出
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define LL long long
using namespace std;
const LL Maxn=;
struct Node{LL p,d;}A[Maxn];
LL Ans,Cnt,n;
priority_queue<LL> Q;
inline bool cmp(Node A,Node B)
{return A.d<B.d || (A.d==B.d && A.p<B.p);}
int main()
{
scanf("%lld",&n);
for (LL i=;i<=n;i++) scanf("%lld%lld",&A[i].d,&A[i].p); sort(A+,A+n+,cmp); Cnt=;
for (LL i=;i<=n;i++)
{
Ans+=A[i].p; Cnt++; Q.push(-A[i].p);
if (Cnt>A[i].d) {Ans+=Q.top(); Q.pop(); Cnt--; }
}
printf("%lld\n",Ans);
return ;
}
BZOJ 1572
BZOJ 1592
这道题有个结论就是最小花费的高度肯定和在原来的高度中.
这个结论感觉上非常显然,如果不在原来的高度中,那个肯定能从原来的高度中找到最优的.然后直接Dp就可以了
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int Maxn=;
const int Inf=0x3f3f3f3f;
int a[Maxn],b[Maxn],C[Maxn][Maxn],F[Maxn][Maxn],Ans,n;
inline int Min(int x,int y) {return x>y?y:x;}
inline int Abs(int x) {return x>?x:-x;}
inline bool cmp1(int a,int b) {return a<b;}
inline bool cmp2(int a,int b) {return a>b;}
int main()
{
scanf("%d",&n);
for (int i=;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i];
for (int i=;i<=n;i++) F[i][]=Inf;
sort(b+,b+n+,cmp1);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
{
C[i][j]=F[i-][j]+Abs(b[j]-a[i]);
F[i][j]=Min(F[i][j-],C[i][j]);
}
Ans=F[n][n];
sort(b+,b+n+,cmp2);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
{
C[i][j]=F[i-][j]+Abs(b[j]-a[i]);
F[i][j]=Min(F[i][j-],C[i][j]);
}
printf("%d\n",Min(Ans,F[n][n]));
return ;
}
BZOJ 1592
BZOJ 1827
树形DP,U节点向子节点转移时,
ans′=ans–size∗w+(tot–size)∗w
ans′=ans+(tot–2∗size)∗w 若(tot–2∗size)<0那么ans'比ans优
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std; const LL Maxn=;
struct EDGE{LL to,next,w;}edge[Maxn<<];
LL head[Maxn],C[Maxn],n,u,v,w,Ans,Dis[Maxn],Size[Maxn],cnt;
inline void Add(LL u,LL v,LL w)
{edge[cnt].to=v;edge[cnt].next=head[u];edge[cnt].w=w;head[u]=cnt++;}
LL Dfs(LL u,LL fa)
{
LL Ret=Dis[u]*C[u]; Size[u]=C[u];
for (LL i=head[u];i!=-;i=edge[i].next)
{
if (edge[i].to==fa) continue;
Dis[edge[i].to]=Dis[u]+edge[i].w;
Ret+=Dfs(edge[i].to,u);
Size[u]+=Size[edge[i].to];
}
return Ret;
}
void Move(LL u,LL fa)
{
for (LL i=head[u];i!=-;i=edge[i].next)
{
if (edge[i].to==fa) continue;
if (Size[]-*Size[edge[i].to]<)
{
Ans+=(Size[]-*Size[edge[i].to])*edge[i].w;
Move(edge[i].to,u);
}
}
}
int main()
{
scanf("%lld",&n);
for (LL i=;i<=n;i++) scanf("%lld",&C[i]);
memset(head,-,sizeof(head));
for (LL i=;i<n;i++)
{
scanf("%lld%lld%lld",&u,&v,&w);
Add(u,v,w),Add(v,u,w);
}
Ans=Dfs(,);
Move(,);
printf("%lld\n",Ans);
return ;
}
BZOJ 1827