hdoj 4310 贪心
不知为毛,过不了
我的代码:
#include<stdio.h>
int main()
{
int n,a[30],b[30],temp,i,j,s1,s2;
double c[30];
while(scanf("%d",&n)!=EOF)
{
s1=0;
s2=0;
for(i=0;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
s1+=a[i];
c[i]=a[i]/b[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(c[i]<c[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
s2+=s1*b[i];
s1-=a[i];
}
printf("%d\n",s2);
}
}
Ac的代码:
- #include <stdio.h>
- #include <algorithm>
- using namespace std;
- struct Node
- {
- int dps;
- int hp;
- }a[30];
- int cmp(Node x,Node y)
- {
- return x.hp*y.dps<y.hp*x.dps;//按比率排,避免小数,所以讲式子进行转换
- }
- int main()
- {
- int t,i;
- int sum,ans;
- while(~scanf("%d",&t))
- {
- sum = ans = 0;
- for(i = 0;i<t;i++)
- {
- scanf("%d%d",&a[i].dps,&a[i].hp);
- sum+=a[i].dps;
- }
- sort(a,a+t,cmp);
- for(i = 0;i<t;i++)
- {
- ans+=sum*a[i].hp;
- sum-=a[i].dps;
- }
- printf("%d\n",ans);
- }
- return 0;
- }
- 有毛区别
hdoj 4310 贪心的更多相关文章
- HDU 4310 贪心
题意 在游戏中你的dps为1但是hp无限 给出n个敌人的dps与hp 你一秒能打掉一个敌人你的dps的hp 当你输出的时候 所有活着的敌人都会打你 求杀死所有敌人时你掉的最少hp 一开始想错了 排序的 ...
- HDOJ 2209 贪心
翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU4310HERO贪心问题
问题描述 When playing DotA with god-like rivals and pig-like team members, you have to face an embarrass ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 4310 Hero (贪心算法)
A - Hero Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- 贪心 HDOJ 4726 Kia's Calculation
题目传送门 /* 这题交给队友做,做了一个多小时,全排列,RE数组越界,赛后发现读题读错了,囧! 贪心:先确定最高位的数字,然后用贪心的方法,越高位数字越大 注意:1. Both A and B wi ...
- BFS+贪心 HDOJ 5335 Walk Out
题目传送门 /* 题意:求从(1, 1)走到(n, m)的二进制路径值最小 BFS+贪心:按照标程的作法,首先BFS搜索所有相邻0的位置,直到1出现.接下去从最靠近终点的1开始, 每一次走一步,不走回 ...
- [HDOJ - 5208] Where is Bob 【DFS+按位贪心】
题目链接:HDOJ - 5208 题目分析 使用按位贪心的思想,即从高位向低位枚举,尽量使这一位的答案为 1 . 我们使用 DFS ,每次就是对于 [l1, r1] [l2, r2] x 进行处理 ...
- HDOJ(HDU).1864 最大报销额 (贪心)
HDOJ(HDU).1864 最大报销额 题意分析 题目有点问题,原题中说的 单项物品的价值不得超过600元 应该是单类物品的价值不能超过600元. 一开始以为是01背包,后来按贪心写过了. 一张一张 ...
随机推荐
- Unity 的“Vertex Lit Rendering path“中 shader Pass 的注意事项
"MADFINGER/Environment/Unlit (Supports Lightmap)"是 ShadowGun 示例中最简单的 shader 了,如下: // Unlit ...
- HDOJ1518Square 深搜
Square Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- (经常看看)jdk 设计模式
在JDK(Java Development Kit)类库中,开发人员使用了大量设计模式,正因为如此,我们可以在不修改JDK源码的前提下开发出自己的应用软件,本文列出了部分JDK中的模式应用实例,有兴趣 ...
- lightoj 1018 dp
题目链接:http://lightoj.com/volume_showproblem.php?problem=1018 #include <cstdio> #include <cst ...
- C#三种判断数据库中取出的字段值是否为空(NULL) 的方法
操作数据库,需要判断返回的字段值是否为空,收集了3种方法供参考 1 通过System.DBNull判断,网上大部分都使用这个方法. DataTable dt; ...
- wdlinux mysql innodb的安装
mysql innodb的安装 wget -c http://down.wdlinux.cn/in/mysql_innodb_ins.sh chmod 755 mysql_innodb_ins.sh ...
- [Javascript] Using JSHint for Linting with Gulp
gulpfile.js var gulp = require('gulp'); var jshint = require('gulp-jshint'); var stylish = require(' ...
- c++ 文件写样例
#include <iostream> #include <sstream> #include <fstream>> using namespace std; ...
- 将html中的br换行符转换为文本输入中的换行符(转)
PHP中的有个非常好的函数:nl2br(),将文本框中的换行转换为HTML页面的<br />,但是如何实现将html中的<br />换行符转换为文本框中的换行符呢?下面这几个方 ...
- Bit data type
mysql> create table t(a bit()); Query OK, rows affected (0.04 sec) mysql> insert into t '; Que ...