跳到正文

个人记录

TopCoder-WaterAndOxygen

lzzVIL约 9 分钟
原文
文章信息与版本

文章信息

发布
2018年4月27日
收录
2026年6月3日
更新
2026年11月17日
篇幅
约 9 分钟

版本

  1. v1当前

前言:

代码
在浙江省选二试集训的第一天下午,杭州学军中学的**mjy0724**大姐姐给我们讲了来自于**TopCoder Open 2017**的题目。(**TopCoder Open**系列的比赛分为3个环节5个部分。有海选Round123,半决赛SEMIFINAL和总决赛FINAL。**TopCoder**是一个外国的编程网站,但也面向中国开放。不过主要面向顶尖的高中生和大学生进行全球的交流与切磋。~~而且最重要的是它在中国的连接不是很稳定,总是要加载好久,应该要翻墙。~~)
这是我第一次在TopCoder上交题,也是第一次用类交题。刚开始还不会用,一窍不通。不过在[daklqw](https://www.luogu.org/space/show?uid=22066)和[网上文章](http://acmicpc.info/archives/164)的帮助下,我还是有点懂了。现在还在摸索中……[心得](https://21391.blog.luogu.org/TopCoder-With-OJ-Problem-class)

英文题面 English Problem

Problem Statement

代码
You are in a spaceship that is far away from the Earth. Your crew needs costH2O moles of water (H2O) and costO2 moles of oxygen (O2) per day. The spaceship currently has remainH2O moles of water and remainO2 moles of oxygen left in the tanks. This ship is old so you don't have any equipment to recycle used water and oxygen: once you use them, they are gone. The only thing you can do is electrolysis of water. For any positive real x, you can electrolyze x moles of water. You will lose those x moles of water but you will produce x/2 moles of oxygen. (You will also produce x moles of hydrogen but those don't matter in this task.) 
Compute and return the longest amount of time (in days) your crew can survive. Note that the answer is not necessarily an integer.

Definition

代码
Class: WaterAndOxygen
Method: maxDays
Parameters: int, int, int, int
Returns: double
Method signature: double maxDays(int remainH20, int remainO2, int costH2O, int costO2)
(be sure your method is public)

Limits

代码
Time limit (s): 2.000
Memory limit (MB): 256

Notes

代码
The returned value must have an absolute or relative error less than 1e-9.

Constraints

代码
costH2O will be between 1 and 1,000,000,000, inclusive.
costO2 will be between 1 and 1,000,000,000, inclusive.
remainH2O will be between 0 and 1,000,000,000, inclusive.
remainO2 will be between 0 and 1,000,000,000, inclusive.

Examples

0)

代码
64	
70	
3	
7	
Returns: 12.0	
If you don't do anything, after 70 / 7 = 10 days you will run out of oxygen. At that time you will have 64 - 10 * 3 = 34 moles of water left. In order to survive longer you will need to electrolyze some of your remaining water to produce the oxygen you need. To get enough oxygen for a day, you need to electrolyze 2 * 7 = 14 moles of water. Don't forget that you also need 3 moles of actual water per day. Thus, on each of the following days you will consume 17 moles of water, which means that you can survive for 34 / 2 = 2 extra days. In total you will survive for 10 + 2 = 12 days.

1)

代码
99	
102	
1	
1	
Returns: 99.0	
You will run out of water after 99/1 = 99 days, while the oxygen will last for 102/1 = 102 days. Electrolysis won't help you live longer than 99 days.

2)

代码
101	
99	
1	
1	
Returns: 99.66666666666667	
This time you should use 4/3 of a mole of water to produce 	2/3 of a mole of oxygen.

3)

代码
123456789	
987654321	
123	
456	
Returns: 1003713.731707317	

4)

代码
987654321	
123456789	
456	
123	
Returns: 1758643.7307692308	

5)

代码
0	
0	
13	
27	
Returns: 0.0	
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

题目简介:

代码
你来到了一个远离地球的空间站。你每天都会消耗一定mol的水和氧气。尽管空间站里有一定量的预备的水和氧气,但是它实在是过于老旧,以至于无法进行循环,也就是水和氧气用过了就没了。不过万幸的是,你还可以做一件事:电解水。1mol水电解后生成0.5mol氧气。	
传入四个参数:预备的水、氧气量,每天消耗的水、氧气量,返回你最多能活几天。

代码:

C++
#include<cstdio>
#include<iostream>
#define ll long long
using namespace std;
class WaterAndOxygen {
	public:
		double maxDays(int rw, int ro, int cw, int co) {
			ll RW=rw,RO=ro,CW=cw,CO=co;
			double x,DW,DO;
			x=(double)(RW*CO-RO*CW)/(double)(CW+2*CO);
			if (x<0) x=0;
			DW=(double)(RW-2*x)/(double)(CW);
			DO=(double)(RO+x)/(double)(CO);
			return (DW<DO)?DW:DO;
		}
};

/*int rw,ro,cw,co;
ll RW,RO,CW,CO;
double x,DW,DO;
int main(){
	ios::sync_with_stdio(false);
	cin>>RW>>RO>>CW>>CO;
	x=(double)(RW*CO-RO*CW)/(double)(CW+2*CO);
	if (x<0) x=0;
	DW=(double)(RW-2*x)/(double)(CW);
	DO=(double)(RO+x)/(double)(CO);
	cout<<((DW<DO)?DW:DO)<<endl;
	return 0;
}*/
原文