poj 1694 An old stone game
2013-07-19 08:11
309 查看
#include<iostream> #include<stdio.h> #include<algorithm> #include<fstream> #include<functional> #include<string.h> using namespace std; class node{ public: int mx; //到达此节点所调动的最少石子数 //int cur; //到达此节点时实际占用的节点数 int num; //子节点的数目 int*child; //子节点编号 }; node Tree[210]; void Clear(); void Search(int); int main(){ //freopen("1.txt","r",stdin); int m,n,i,p,r,c,j,cc; scanf("%d",&m); while(m--){ scanf("%d",&n); Clear(); for(i = 0; i < n; ++i){ scanf("%d%d",&p,&r); Tree[p].num = r; if(r ){ Tree[p].child = new int[r+1]; for(j = 0; j < r; ++j){ scanf("%d",&cc); Tree[p].child[j] = cc; } } } Search(1); cout << Tree[1].mx << endl; } return 0; } void Clear(){ for(int i = 0; i < 210; ++i){ Tree[i].mx = Tree[i].num = -1; if(Tree[i].child != NULL){ delete[]Tree[i].child; Tree[i].child = NULL; } } } void Search(int id){ int copychild[201],Needed[201]; memset(copychild,0,sizeof(copychild)); memset(Needed,0,sizeof(Needed)); int i , j,tempNum= Tree[id].num,maxStone = -1,total = 0,available = 0,tempUnused,temp; if(tempNum){ for(i = 0; i < tempNum; ++i){ temp = copychild[i] = Tree[id].child[i]; if(Tree[copychild[i]].mx == -1){ Search(copychild[i]); } Needed[i] = Tree[temp].mx; } //减少在访问数据上的时间开销 sort(Needed,Needed+tempNum,greater<int>()); for(int peek = 0; peek < tempNum; ++peek){ if(available < Needed[peek]){ total += Needed[peek]-available; available = Needed[peek]-1; } else{ --available; } } Tree[id].mx = total; return; } else{ Tree[id].mx = 1; return; } }
思路:
建立一棵关系树,对每个节点,设mx为填满此节点所需用到的最少石子数;若无子节点,则填满所需石子为1;
否则,要把这个节点的所有子节点填满,对其子节点所需的石子数排序,先填满所需石子数最多的节点(设需要m个),然后填满后有m-1个节点可用,
接着再填排序为第二位的,如果石子不够,那么就要增加石子,依次计算
为什么要按递减序填满子节点呢?各个子节点所需的石子数互不相关,而当一个子节点被填满后,它所需的石子只有1个,即所需石子数越多,那么填满后的石子数也越多,
如果先填少的,可能出现石子被占没有被充分使用的情况~
比如两个节点所需石子数为1和3;先填1,则需要4个石子,先填3,只需3个石子
相关文章推荐
- ZOJ1427 POJ1694 An Old Stone Game
- POJ 1694 An Old Stone Game
- POJ--1694--An Old Stone Game
- POJ 1694 An Old Stone Game 笔记
- POJ_1694_An Old Stone Game
- POJ 1694 An Old Stone Game【递归+排序】
- POJ 1694 An Old Stone Game【递归+排序】
- POJ 1694 An Old Stone Game
- POJ 1694 An Old Stone Game
- An Old Stone Game(poj1694模拟与排序)
- poj 1694 An Old Stone Game 树形dp
- 【poj1694】 An Old Stone Game
- POJ 1694 An Old Stone Game ★(排序+树+递归)
- poj 1694 An Old Stone Game 树形dp
- POJ 1694 An Old Stone Game ★(排序+树+递归)
- POJ 1738:An old Stone Game 石子归并(GarsiaWachs算法)
- POJ 1738 An old Stone Game 笔记
- POJ 1738:An old Stone Game 石子归并(GarsiaWachs算法)
- POJ 1738 An old Stone Game 石子合并之GarsiaWachs算法
- poj1738 An old Stone Game 石子合并(归并) GarsiaWachs算法