XTOJ 1252 Defense Tower 贪心

题目链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1252

思路:考虑每条边对玩家的伤害

假设连接的节点是u,v,破坏力是p[u]和p[v]

假设p[u]>p[v]

现在考虑u,v的删除顺序,如果先删u,这条边对玩家的伤害,是p[v],先删v,伤害是p[u]

所以显然对于每条边,我们都要先删权值大的,才能最好

怎么样才能对于每条边先删最大的呢,那就按照权值递减删就好了

所以 ret=Σ(min(p[u],p[v]))

复杂度O(n)

#include <cstdio>
using namespace std;
const int N=1e5+5;
int p[N];
int main(){
   int n;
   while(~scanf("%d",&n)){
    for(int i=1;i<=n;++i)
      scanf("%d",&p[i]);
    int ret=0;
    for(int i=1;i<n;++i){
      int u,v;
      scanf("%d%d",&u,&v);
      ret+=min(p[u],p[v]);
    }
    printf("%d\n",ret);
   }
   return 0;
}

时间: 2024-10-24 04:11:16

XTOJ 1252 Defense Tower 贪心的相关文章

XTU 1252 Defense Tower

$2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$J$题 贪心. 优先删除$power$大的点. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<map>

HDU 4939 Stupid Tower Defense(贪心+dp)

HDU Stupid Tower Defense 题目链接 题意:有一些塔,红塔能攻击经过他的,绿塔能攻击经过之后的,蓝塔能把经过之后的减速,求在1-n上放塔,求伤害最大值 思路:一开始以为直接贪心,绿塔最前,蓝塔中间,红塔最后就可以了,结果其实是错的 不过,红塔放最后是肯定的,这个很显然就不多证明了,是贪心的思想 然后就dp[i][j]表示放到i,前面有j个绿塔去状态转移即可 代码: #include <cstdio> #include <cstring> #include &l

HDU 4939 Stupid Tower Defense(dp+贪心)

dp[i][j]表示到了第i步放了j个减速,造成的伤害.我们用贪心的策略把造成一段伤害的放在最后面,造成持续伤害的与减速放在前i个中这样得到的伤害是最高的. 所以前(i,j)中的伤害为dp[i][j] = max(dp[i-1][j]+(j*z+t)*(max(0LL, i-1-j))*y, dp[i-1][j-1]+((j-1)*z+t)*(i-j)*y); 每次造成的伤害就为:dp[i][j]+(n-i)*(j*z+t)*(x+(i-j)*y).然后取一个最大值,就是伤害的最大值了. Stu

ZOJ3623:Battle Ships(完全背包)

Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes tiseconds to produce the

zoj3623 Battle Ships

Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti seconds to produce th

zoj 3623 Battle Ships dp

 Description Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N kinds of battle ships. The factory takes ti second

Battle Ships(复习泛化物品**)

传送门Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N

ZOJ3623 Battle Ships (完全背包)

Battle Ships Time Limit: 2 Seconds      Memory Limit: 65536 KB Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defense tower, which has L longevity. The player has a military factory, which can produce N ki

Battle Ships(完全背包)

Battle ShipsCrawling in process... Crawling failed Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Description Battle Ships is a new game which is similar to Star Craft. In this game, the enemy builds a defens