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

编程入门 行业动态 更新时间:2024-10-18 06:02:44

Codeforces Round #674 C Increase and Copy【<a href=https://www.elefans.com/category/jswz/34/1770441.html style=数学】"/>

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 ⌈xn−x​⌉ 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;
}

更多推荐

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

本文发布于:2024-02-24 16:33:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1695963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数学   Codeforces   Copy   Increase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!