0
点赞
收藏
分享

微信扫一扫

C. Increase and Copy(贪心算法)Codeforces Round #674 (Div. 3)

i奇异 2022-06-24 阅读 21

原题链接: ​​https://codeforces.com/contest/1426/problem/C​​

C. Increase and Copy(贪心算法)Codeforces Round #674 (Div. 3)_数组
测试样例

input
5
1
5
42
1337
1000000000
output
0
3
11
72
63244

题意: 给你一个数组C. Increase and Copy(贪心算法)Codeforces Round #674 (Div. 3)_ios_02,初始只有一个元素,值为C. Increase and Copy(贪心算法)Codeforces Round #674 (Div. 3)_数组_03。你接下来可以进行两种操作:

  • 选择其中某个元素对其进行C. Increase and Copy(贪心算法)Codeforces Round #674 (Div. 3)_数组_04操作。
  • 选择其中某个元素对其进行复制操作,并添加到数组末尾。

问你要经过多少次操作才可以让数组总和至少为C. Increase and Copy(贪心算法)Codeforces Round #674 (Div. 3)_ios_05

解题思路: 我们先来确定一个事实,根据贪心原则,当我们开始进行追加操作后就必不可能进行第一个操作了。 所以我们一定是先对第一个元素加到一定的值。最后一直进行复制操作直到达到C. Increase and Copy(贪心算法)Codeforces Round #674 (Div. 3)_ios_05。当然,我们加到一定的值自然是要去判断的,这个很关键。那么我们什么时候确定最小值呢?当然是我们一直往上叠加第一个操作值后这种得到的操作次数竟然是更大的,这个时候我们就要退出了。 (本来我们这样子进行就是为了寻求最优方案,当后面越来越来多了,那么自然接下来也不会出现最小值了。相当于是一个先单调递减再单调递增的规则。)OK,具体看代码。

AC代码

/*

*
*/
#include<bits/stdc++.h> //POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

ll t,n;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>n;
ll cnt,minn=inf;
//确定好了就停止自增,直接进行追加操作。
for(int i=1;i<=n;i++){
cnt=i-1;
cnt+=(n-i)/i;
if(n%i){
cnt++;
}
if(cnt<=minn){
minn=cnt;
}
else{
break;
}
}
cout<<minn<<endl;
}
}
return 0;
}


举报

相关推荐

0 条评论