National Project CodeForces - 1303B

National Project

Problem
Your company was appointed to lay new asphalt on the highway of length n. You know that every day you can either repair one unit of the highway (lay new asphalt over one unit of the highway) or skip repairing.

Skipping the repair is necessary because of the climate. The climate in your region is periodical: there are g days when the weather is good and if you lay new asphalt these days it becomes high-quality pavement; after that, the weather during the next b days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again g good days, b bad days and so on.

You can be sure that you start repairing at the start of a good season, in other words, days 1,2,…,g are good.

You don’t really care about the quality of the highway, you just want to make sure that at least half of the highway will have high-quality pavement. For example, if the n=5 then at least 3 units of the highway should have high quality; if n=4 then at least 2 units should have high quality.

What is the minimum number of days is needed to finish the repair of the whole highway?

Input
The first line contains a single integer T (1≤T≤104) — the number of test cases.

Next T lines contain test cases — one per line. Each line contains three integers n, g and b (1≤n,g,b≤109) — the length of the highway and the number of good and bad days respectively.

Output
Print T integers — one per test case. For each test case, print the minimum number of days required to repair the whole highway if at least half of it should have high quality.

Example
Input
3
5 1 1
8 10 10
1000000 1 1000000

Output
5
8
499999500000

Note
In the first test case, you can just lay new asphalt each day, since days 1,3,5 are good.

In the second test case, you can also lay new asphalt each day, since days 1-8 are good.

思路当时没一下子想出来,浪费不少时间


import java.util.Scanner;

public class J {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		for (int i = 0; i < t; i++) {
			long n = sc.nextLong();
			long g = sc.nextLong();
			long b = sc.nextLong();
			long day = 0;
			long half = n/2;
			if(g <= b) {//两个一起走,走2*g天,这样导致坏天数不会超过好天数
				long cnt = n / (2 * g);
				if(cnt == 0) System.out.println(n);
				else {
					day += (cnt * (g + b));
					long add = n - half - cnt * g;//总g数-当前g数=添加g数
					day += add;
					long bad = n - add - cnt*g;//总n-添加g数-当前g数=剩下的需要的b数
					if(cnt * b >= bad) {//当前b数>=需要的b数,不需要添加
						if(add == 0) {//如果添加的g数为0,在上一层操作
							//求出多余的b数=截止到上一层的b数-需要b数
							long duo = cnt * b - bad;
							//如果当前多余的b数>b,则减去b
							if(duo > b) duo = b;
							day -= duo;
						}
					}
					else {
						day += bad - cnt * b;//添加剩下需要的b数
					}
					System.out.println(day);
				}
			}else {//好的天数大于坏的天数,可以一轮一轮(g+b)走,需要n天
				System.out.println(n);
			}
		}
		sc.close();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值