Codeforces Round #674 C Increase and Copy【数学】

题目链接

题目描述:

在这里插入图片描述


题解:

it is pretty intuitive that we firstly need to do all increaments and only then copy numbers. You could notice that the answer does not exceed n 1 / 2 n^{1/2} n1/2 so we can iterate from 1 to ⌊ n 1 / 2 ⌋ \lfloor n^{1/2} \rfloor n1/2 and fix number we will copy. Let it be x. Then we need x - 1 moves to obtain it and also need ⌈ n − x x ⌉ \lceil \frac{n-x}{x} \rceil xnx moves to get the enough number of copies. So we can update the answer with this number of moves.

二分答案:

我比赛的时候用的二分答案做的,首先令 x 为increments 的次数,y 为 copy 的次数,n 为总 moves 的次数,有 n = x + y。设 w 为经过 x 次 increment 和 y 次 copy 后得到的值,即 w = (1 + x) * y,经过与 n = x + y 联立,求导并代入,最终得到 ( n + 1 2 ) 2 (\frac{n+1}{2})^2 (2n+1)2 为 n 次操作的最大值,然后进行二分答案即可,最终得到的结果要减 1。

Tips:

If you want to calculate ⌈ X Y ⌉ \lceil \frac{X}{Y} \rceil YX then you do this: (X + Y - 1) / Y or (X - 1) / Y + 1 .
this works because division is rounded down if you cast the result to int


做法1(93ms):

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
//#include <unordered_set>
//#include <unordered_map>
#include <deque>
#include <list>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(2);
//cout << setw(2);
const int N = 2e5 + 6, M = 1e9 + 7;



int main() {
    //freopen("/Users/xumingfei/Desktop/ACM/test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int ans = 1e9;
        for (int i = 1; i * i <= n; i++) {
            ans = min(ans, i - 1 + (n - i - 1) / i + 1);
        }
        cout << ans << '\n';
    }
    return 0;
}

二分答案(15ms):

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
//#include <unordered_set>
//#include <unordered_map>
#include <deque>
#include <list>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(2);
//cout << setw(2);
const int N = 2e5 + 6, M = 1e9 + 7;



int main() {
    //freopen("/Users/xumingfei/Desktop/ACM/test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int t;
    cin >> t;
    while (t--) {
        int n;
        cin >> n;
        int l = 0, r = 1e9;
        while (l + 1 < r) {
            double mid = (l + r) >> 1;
            if (pow((mid + 1) / 2, 2) >= n) {
                r = mid;
            } else {
                l = mid;
            }
        }
        if (pow((1.0 * l + 1) / 2, 2) >= n) {
            cout << l - 1 << '\n';
        } else {
            cout << r - 1 << '\n';
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值