栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 3715 Kindergarten Election

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 3715 Kindergarten Election

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define inf 0x3f3f3f3f#define maxn 106using namespace std;int has[maxn], cost[maxn], a[maxn];int a2[maxn];int n, max1, sum;struct Node{ int num; int id;} node[maxn];bool cmp(const Node a, const Node b){ return a.num < b.num;}int main(){ int ncase, tmp, t, sum2; cin >> ncase; while(ncase--) { memset(has,0,sizeof(has)); memset(cost,0,sizeof(cost)); memset(a,0,sizeof(a)); memset(node,0,sizeof(node)); max1 = inf; cin >> n; for(int i= 2; i <= n; i++) { scanf("%d", &a[i]); has[ a[i] ]++; } for(int i = 2; i <= n; i ++) { scanf("%d", &cost[i]); } for(int j = has[1]; j < n; j++) { memset(node, 0, sizeof(node)); sum = 0; tmp = has[1]; for(int i = 2; i <= n; i++) { if(has[i] > j - 1) { sum += has[i] - ( j - 1); } } if(sum + has[1] > j) continue; for(int i = 0 ; i <= n; i ++) { a2[i] = a[i]; } t = 0 , sum2 = 0 ; for(int i = 2; i <=n; i++) { if(has[i] > j - 1) { t = 0; for( int k = 2; k <= n; k ++) if(a2[k] == i) { node[t].num = cost[k]; node[t].id = k; t++; } sort(node,node+t,cmp); for(int k = 0; k < has[i]-(j-1); k++ ) { sum2 += node[k].num; a2[ node[k].id ] = 1; has[1]++; } } } t = 0; for(int i = 2 ; i <= n; i++) { if(a2[i] != 1) { node[t].num = cost[i]; node[t].id = i ; t++; } } sort(node, node+t, cmp); for(int k = 0; k < t ; k++) { if(has[1] >= j) break; sum2 += node[k].num; a2[ node[k].id ] = 1; has[1]++; } max1 = min(max1, sum2); has[1] = tmp; } printf("%dn",max1); } return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/372319.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号