zoukankan      html  css  js  c++  java
  • 1246 罐子和硬币 模拟题,感觉只能模拟

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1246

    这题平均分不是最优的,需要有一些空位置。

    比如3 10 10 答案应该是11,分配就是,第一个是0,其他的均分。

    所以我需要知道应该空出多少个位置,使得答案更优。

    我觉得这个只能模拟,比如暴力枚举空出i个位置,然后算出来,取最小值就好了。

    网上的有些代码,是错误的。

    5 13 13.网上的很多都输出14,其实答案是15

    所以直接暴力吧,写个函数算,就好。

    再来一组数据,23 80 78

    至于为什么可以提前break,我也没得到严格的证明,只不过不break害怕超时,其实不break,也不超市。

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <assert.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <bitset>
    int tocalc(int n, int k, int c) {
        if (k / n * n >= c) return c;
        return n - k % n + c;
    }
    void work() {
        int n, k, c;
        cin >> n >> k >> c;
        int ans = tocalc(n, k, c);
        for (int i = n - 1; i >= 1; --i) {
            int tans = n - i + tocalc(i, k, c);
            if (ans >= tans) ans = tans;
            else break;
        }
        cout << ans << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    Redis缓存穿透和雪崩
    Redis主从复制
    Redis发布订阅
    IO多路复用
    Synchronized解读
    日志导致jvm内存溢出相关问题
    tomcat及springboot实现Filter、Servlet、Listener
    MySQL主从复制针对trigger的特殊处理
    二、变量/常量/数据类型
    Ubuntu21.04 / Linux Mint20.2 安装 TradingView分析软件
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6361910.html
Copyright © 2011-2022 走看看