CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作
题目链接:http://codeforces.com/problemset/problem/93/B
1 second
256 megabytes
standard input
standard output
Students love to celebrate their holidays. Especially if the holiday is the day of the end of exams!
Despite the fact that Igor K., unlike his groupmates, failed to pass a programming test, he decided to invite them to go to a cafe so that each of them could drink a bottle of... fresh cow milk. Having entered the cafe, the m friends found n different kinds of milk on the menu, that's why they ordered n bottles — one bottle of each kind. We know that the volume of milk in each bottle equals w.
When the bottles were brought in, they decided to pour all the milk evenly among the m cups, so that each got a cup. As a punishment for not passing the test Igor was appointed the person to pour the milk. He protested that he was afraid to mix something up and suggested to distribute the drink so that the milk from each bottle was in no more than two different cups. His friends agreed but they suddenly faced the following problem — and what is actually the way to do it?
Help them and write the program that will help to distribute the milk among the cups and drink it as quickly as possible!
Note that due to Igor K.'s perfectly accurate eye and unswerving hands, he can pour any fractional amount of milk from any bottle to any cup.
The only input data file contains three integers n, w and m (1 ≤ n ≤ 50, 100 ≤ w ≤ 1000, 2 ≤ m ≤ 50), where n stands for the number of ordered bottles, w stands for the volume of each of them and m stands for the number of friends in the company.
Print on the first line "YES" if it is possible to pour the milk so that the milk from each bottle was in no more than two different cups. If there's no solution, print "NO".
If there is a solution, then print m more lines, where the i-th of them describes the content of the i-th student's cup. The line should consist of one or more pairs that would look like "b v". Each such pair means that v (v > 0) units of milk were poured into the i-th cup from bottle b (1 ≤ b ≤ n). All numbers b on each line should be different.
If there are several variants to solve the problem, print any of them. Print the real numbers with no less than 6 digits after the decimal point.
2 500 3
YES
1 333.333333
2 333.333333
2 166.666667 1 166.666667
4 100 5
YES
3 20.000000 4 60.000000
1 80.000000
4 40.000000 2 40.000000
3 80.000000
2 60.000000 1 20.000000
4 100 7
NO
5 500 2
YES
4 250.000000 5 500.000000 2 500.000000
3 500.000000 1 500.000000 4 250.000000 题目大意:输入n,w,m 分别代表n杯不同的饮料,每杯饮料有w体积,要平均分为m杯
思路:这题A出来感觉非常不简单,一天就做了这一道题。。。 其实这题要懂的就是如何贪心,然后如何存数据答案,还有一个很重要的就是double的精度问题,控制不好就别想过了
贪心思想是: 每次都使得当前用的饮料尽可能的多,如果用了两次都没有用完的话,那就显然是“NO”了,贪心思想很简单。
然后关于存储数据的:因为我们并不知道装满一杯要多少种饮料,所以这个就必须要用动态存储了,这就需要用到vector,我们要存储的有第几杯,用了哪种饮料,用了多少,所以要三个
变量来存储。 学了一下如果用vector来存储三个变量,就是再用一个pair,具体怎么用在代码中可以看到
double精度的控制:这题本来自己感觉是没有问题的,然后提交,wa在第十个吧,忘记了,然后试了数据,发现结果跟自己想的不一样,后来改代码,把过程中的结果输出来,发现
在运行过程中double 保留的是三位小数,但是相减却又保留的是六位小数,比如一个是333.333,1000和它相减则会得到666.666667,,但是下一次和666.667比较,如果我们简单
的比较二者是否相等的话,显然是不相等的,但是二者实际上是相等的,所以要二者相减判断差值是否在误差之外就行了。
看代码
#include<iostream>
#include<string.h>
#include<map>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<cmath>
#include<ctype.h>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e3+;
const int maxk=5e3+;
const int maxx=1e4+;
const ll maxe=+;
#define INF 0x3f3f3f3f3f3f
#define Lson l,mid,rt<<1
#define Rson mid+1,r,rt<<1|1
typedef pair<int,double> p;
int main()
{ //vector<pair<int,double> > ans[100];
vector<p>ans[];
p fun;
int n,w,m;
cin>>n>>w>>m;
if(m>n*) cout<<"NO"<<endl;//n瓶饮料最多能分出2*n杯饮料,所以大于它直接NO
else
{
int cnt=,sum1=,sum3=;//cnt代表第几瓶,sum1代表第几个人,sum3判断是否用了两次以上
double ave=1.0*n*w/m;
double sum=ave,sum2=w;//补满一瓶还需的容量,一瓶还剩多少
while(cnt<=n&&sum1<=m)//
{
if(fabs(sum-sum2)<1e-)//精度判断,一定要放在最前面,不然会判断别的,也就wa了
{
//cout<<"2"<<endl;
fun.first=cnt;
fun.second=sum2;
ans[sum1].push_back(fun);
//ans[sum1].push_back({cnt,sum2});
sum3++;
if(sum3>)
{
cout<<"NO"<<endl;
return ;
}
//ans[cnt].insert(n);
//ans.insert(cnt,n);
sum3=;
sum2=w;
sum=ave;
cnt++;
sum1++;
//cout<<cnt<<" "<<sum<<" "<<sum2<<endl;
}
else if(sum>sum2)
{
// cout<<"1"<<endl;
fun.first=cnt;
fun.second=sum2;
ans[sum1].push_back(fun);
//ans[sum1].push_back({cnt,sum2}); sum3++;
if(sum3>)
{
cout<<"NO"<<endl;
return ;
}
sum3=;
sum-=sum2;
sum2=w;
cnt++;
}
//else if(sum==sum2)
else
{
//cout<<"3"<<endl;
//printf("%.3lf %.6lf\n",sum,sum2);
fun.first=cnt;
fun.second=sum;
ans[sum1].push_back(fun);
//ans[sum1].push_back({cnt,sum});
sum2=sum2-sum;
if(sum2==0.000000) cout<<"sss"<<endl;
sum=ave;
sum3++;
sum1++;
}
if(sum3>)
{
cout<<"NO"<<endl;
return ;
}
}
cout<<"YES"<<endl;
for(int i=;i<=m;i++)
{
for(int j=;j<ans[i].size();j++)
{
printf("%d %.6lf ",ans[i][j].first,ans[i][j].second);
//cout<<ans[i][j].first<<" "<<ans[i][j].second;
}
cout<<endl;
}
}
return ;
}
CodeForces - 93B(贪心+vector<pair<int,double> >+double 的精度操作的更多相关文章
- 2018宁夏邀请赛G(DFS,动态规划【VECTOR<PAIR>】)
//代码跑的很慢四秒会超时,结尾附两秒代码(标程) #include<bits/stdc++.h>using namespace std;typedef long long ll;cons ...
- 【vector+pair】洛谷 P4715 【深基16.例1】淘汰赛
题目:P4715 [深基16.例1]淘汰赛 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 这道题因为数据范围不大,所以做法可以非常简单,使用一个vector加上pair就可以了: ...
- 关于c中 int, float, double转换中存在的精度损失问题
先看一段代码实验: #include<limits> #include<iostream> using namespace std; int main() { unsigned ...
- C++中将string类型转换为int, float, double类型 主要通过以下几种方式:
C++中将string类型转换为int, float, double类型 主要通过以下几种方式: # 方法一: 使用stringstream stringstream在int或float类型转换为 ...
- QT中QString 与 int float double 等类型的相互转换
Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...
- [C++] string与int, float, double相互转换
参考:http://blog.csdn.net/candadition/article/details/7342380 将string类型转换为int, float, double类型 主要通过以下几 ...
- Converter -> public static int ToInt32(double value) 你用对了么?
Convert.ToInt32() 是我们经常使用的方法,但如果我们写如下的代码,能确定它的输出值么? var x = 7.5; Console.WriteLine(7.5 + ": &q ...
- C 语言实例 - 计算 int, float, double 和 char 字节大小
C 语言实例 - 计算 int, float, double 和 char 字节大小 C 语言实例 C 语言实例 使用 sizeof 操作符计算int, float, double 和 char四种变 ...
- Qt中 QString 和int,double等的转换
Qt中 int ,float ,double转换为QString 有两种方法 1.使用 QString::number(); 如: long a = 63; QString s = QString:: ...
随机推荐
- mesos的zookeeper变更
采用rpm方式安装你了mesos,碰到zookeeper(采用了cloudera的zookeeper)的IP地址变化了,肿么办? 在master机器中: /etc/mesos/zk进行编辑修改zk路径 ...
- WPF中Xaml编译正常而Designer Time时出错的解决办法
开发wpf时我们经常遇到一个xaml文件在设计时显示解析错误(比如在:VS或者Blend)而编译正常运行正常. 原因是:xaml的在Debug版本下必须为anyCPU. 解决办法: 1.打开工程文件x ...
- Springboot监控之一:SpringBoot四大神器之Actuator之2--spring boot健康检查对Redis的连接检查的调整
因为项目里面用到了redis集群,但并不是用spring boot的配置方式,启动后项目健康检查老是检查redis的时候状态为down,导致注册到eureka后项目状态也是down.问下能不能设置sp ...
- AI:AI
ylbtech-AI:AI 人工智能(Artificial Intelligence),英文缩写为AI.它是研究.开发用于模拟.延伸和扩展人的智能的理论.方法.技术及应用系统的一门新的技术科学. 人工 ...
- Java中使用同步关键字synchronized需要注意的问题
在Java中,synchronized关键字是用来控制线程同步的,就是在多线程的环境下,控制synchronized代码段不被多个线程同时执行.synchronized既可以加在一段代码上,也可以加在 ...
- HTTP 协议入门(转载)
作者:阮一峰 (@ruanyf) www.ruanyifeng.com/blog/2016/08/http.html HTTP 协议是互联网的基础协议,也是网页开发的必备知识,最新版本 HTTP/2 ...
- 三 volatile关键字
一:内存模型: 大家都知道,计算机在执行程序时,每条指令都是在CPU中执行的,而执行指令过程中,势必涉及到数据的读取和写入.由于程序运行过程中的临时数据是存放在主存(物理内存)当中的,这时就存在一个问 ...
- BarTender SDK 实现调用模板条码打印
Demo:MyZebraPrint 基于BatTender .Net SDK 实现调用模板进行条码打印 有需要的朋友可以拿去研究下 在已经安装了BatTender10.1的电脑里测试通过. 下载地址: ...
- k8s 基础(4) k8s安装
转自 http://www.cnblogs.com/informatics/p/7389806.html 安装和配置 从github.com/kubernetes/kubernetes上下载1.6.8 ...
- shell入门-awk-1
awk对于sed grep的优势在于分段,把分隔符分割成一小段一小段,针对段进行匹配,替换,操作,逻辑判断,比较...等 awk的基本操作 [root@wangshaojun ~]# awk -F ' ...