Educational Codeforces Round 82 (Rated for Div. 2):B. National Project

Discription
Your company was appointed to lay new asphalt on the highway of length nn. 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 gg 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 bb days is bad, and if you lay new asphalt these days it becomes low-quality pavement; again gg good days, bb 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,…,g1,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=5n=5 then at least 33 units of the highway should have high quality; if n=4n=4 then at least 22 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 TT (1≤T≤1041≤T≤104) — the number of test cases.
Next TT lines contain test cases — one per line. Each line contains three integers nn, gg and bb (1≤n,g,b≤1091≤n,g,b≤109) — the length of the highway and the number of good and bad days respectively.

Output
Print TT 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,51,3,5 are good.In the second test case, you can also lay new asphalt each day, since days 11-88 are good.

题意
一共有n段路,一天修一段路。在好天修的试好路,在坏天修的是坏路。
好天有g天,坏天有b天,并且是循环的(g天好天,b天坏天,g天好天…)
要保证有至少一半的路是好的,问最少需要多少天。

思路
以(g+b)为一个循环节,要保证至少x=(n+1)/2的人都在好天,所以最少为ans=x/g*(g+b)-b
然后判断在此循环中能否在剩下的坏天中将剩下的一半修完,如果不能加上剩下的路数。能否整除要分开判断。

AC代码

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define pd(n) printf("%d\n", (n))
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define rep(i, a, n) for(int i=a; i<=n; i++)
#define per(i, n, a) for(int i=n; i>=a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;

int t;
ll n,g,b,ans;

int main()
{
    sd(t);
    while(t--)
    {
        slddd(n,g,b);
        ll x=(n+1)/2;
        ll y=n-x;
        ans=x/g*(g+b)-b;
        if(x%g)
        {
            ans=ans+x%g+b;
            y=y-x/g*b;
        }
        if(x%g==0)
            y=y-(x/g-1)*b;
        if(y>0)
                ans+=y;
        cout<<ans<<endl;
    }
}
发布了306 篇原创文章 · 获赞 105 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43460224/article/details/104296452
今日推荐