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

 

posted on 2017-02-03 01:02  stupid_one  阅读(168)  评论(0编辑  收藏  举报

导航