CF Round #570 (Div. 3) C. Computer Game

本文解析了一道来自CodeForces的算法题,题目要求玩家在有限的电量下进行游戏,通过策略选择不同的游戏模式以达到最优解。文章详细介绍了问题背景、解题思路及代码实现。
链接

http://codeforces.com/contest/1183/problem/C

题意

 开始Vova的笔记本电脑电量为k,1.如果电量大于a,它可以进行一次游戏,之后总电量会减去a,2.如果电量大于b,那么它也可以进行一次游戏并同时充电,之后总电量减b,如果电量小于等于b,他想要进行n次游戏,并且希望尽可能多的进行第一种游戏方式(即不充电的),最后输出第一种游戏的次数,如果不能进行n次游戏就输出-1;

题解

本题即求满足ax+by<k的最大的x

代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define inf 0x7f7f7f7f
#define maxn 100006
#define mod 1000000007
#define N 1
#define P 2


typedef long long ll;
typedef struct {
    int u, v, next, w;
} Edge;
Edge e[1];
int cnt, head[1];

inline void add(int u, int v,int w) {
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    // e[cnt].f=f;
    e[cnt].next = head[u];
    head[u] = cnt++;
    e[cnt].u = v;
    e[cnt].v = u;
    e[cnt].w = w;
    //    e[cnt].f=-f;
    e[cnt].next = head[v];
    head[v] = cnt++;
}

inline void write(int x) {
    if (x < 0)
        putchar('-'), x = -x;
    if (x > 9)
        write(x / 10);
    putchar(x % 10 + '0');
}
inline ll read() {
    ll x = 0, f = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')
            f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}
ll k,n,a,b,q;
int main(){
    q=read();
    while(q--){
        k=read(),n=read(),a=read(),b=read();
        if(k<=n*b)
            cout<<-1<<endl;
        else if(k>n*a){
            cout<<n<<endl;
        }else{
            k=n*a-k;
            ll t=a-b;
            ll p=k/t+1;
            cout<<n-p<<endl;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值