bzoj2809: [Apio2012]dispatching
2016-01-20 15:06
387 查看
链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2809
题意:中文题。。
分析:稍微分析下其实就知道要从叶子往根贪心保留薪水最少的人,因为只要算人头数和根的领导能力,所以从叶子往根合并是发现花费超过了总预算就可以删除薪水高的忍者。
代码:
#include<map> #include<set> #include<cmath> #include<queue> #include<math.h> #include<cstdio> #include<vector> #include<string> #include<cstring> #include<iostream> #include<algorithm> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int N=100010; const int MAX=151; const int MOD=1000000007; const int MOD1=100000007; const int MOD2=100000009; const int INF=2100000000; const double EPS=0.00000001; typedef long long ll; typedef unsigned long long uI64; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } ll ans=0; int m,c ,l ; int tot,d ,h ,pre ; int lc ,rc ,fa ,sum ,peo ; void add(int a,int b) { d[tot]=b;pre[tot]=h[a];h[a]=tot++; } int find_fa(int x) { if (fa[x]==x) return x; return fa[x]=find_fa(fa[x]); } int mergee(int a,int b) { if (!a||!b) return a+b; if (c[a]<c[b]) { a^=b;b^=a;a^=b; } rc[a]=mergee(rc[a],b); if (h[lc[a]]<h[rc[a]]) { lc[a]^=rc[a];rc[a]^=lc[a];lc[a]^=rc[a]; } h[a]=h[rc[a]]+1; return a; } void dfs(int x) { int i,a,b,f; for (i=h[x];i!=-1;i=pre[i]) { dfs(d[i]); a=find_fa(x); b=find_fa(d[i]); f=mergee(a,b); fa[a]=fa[b]=fa[f]=f; sum[f]=sum[a]+sum[b];peo[f]=peo[a]+peo[b]; while (sum[f]>m) { fa[lc[f]]=fa[rc[f]]=mergee(lc[f],rc[f]); fa[f]=fa[lc[f]]; sum[fa[f]]=sum[f]-c[f];peo[fa[f]]=peo[f]-1; f=fa[f]; } } f=find_fa(x); ans=max(ans,(ll)peo[f]*l[x]); } int main() { int i,n,x; scanf("%d%d", &n, &m); tot=0; memset(h,-1,sizeof(h)); for (i=1;i<=n;i++) { scanf("%d%d%d", &x, &c[i], &l[i]); add(x,i); } lc[0]=rc[0]=fa[0]=sum[0]=peo[0]=0; for (i=1;i<=n;i++) { lc[i]=rc[i]=0;fa[i]=i;sum[i]=c[i];peo[i]=1; } dfs(1); printf("%lld\n", ans); return 0; } /* 5 4 0 3 3 1 3 5 2 2 2 1 2 4 2 3 1 7 6 0 3 4 1 3 7 2 2 5 2 2 5 1 5 4 5 3 1 5 2 5 */
相关文章推荐
- 导入时如何定制spring-boot依赖项的版本
- 修改DrawerLayout 和toolbar 配合navigation的颜色
- HDU 4725 The Shortest Path in Nya Graph(好题)
- nonnull, nullable
- 修改DrawerLayout 和toolbar 配合navigation的颜色
- Moloch
- Unity 4.2.0 官方最新破解版(Unity3D 最新破解版,3D游戏开发工具和游戏引擎套件)
- Mongoose的模糊查询
- 查询sql表列名
- jfreechart createBarChart 去掉立体感
- 让DIV标签中的P标签水平和垂直都居中
- 通过iscsi配置在aix上挂载存储设备
- 深入浅出设计模式(九):12.装饰模式(Decorator) 13.桥模式(Bridge)14.策略模式(Strategy)
- 扩展系统功能——装饰模式(三):图形界面构件库的解决方案
- Android自定义控件样式实例详解
- Database catalog and schema
- 如何处理iOS中照片的方向
- Linux系统中的信号量(semphore)与互斥体(mutex)
- spring---定时任务(quartz定时器)
- 每天一点小知识004--关于获取物体名字