CodeCraft-22 and Codeforces Round #795 (Div. 2) C. Sum of Substrings
and of Codeforces div 22 round sum
2023-09-11 14:14:52 时间
题意:
给一个二进制的字符串,f(x)函数表示将这个字符串每相邻的两位看做十进制(允许前导零)相加的值。每次操作可以交换相邻两位,问最多k次可以得到f(x)的最小值是多少。
思路:
贪心题,首先去考虑一个对象对答案的贡献是多少,然后去极端化地(即不考虑其他条件)使得贡献最大(或最小),最后再去考虑恰好满足限定条件
在此题中,我们去考虑1对函数值的贡献,在除去第一个字符和最后一个字符的1的所有位置中,1对答案的贡献都是11(1作为个位和作为十位),而第一个位置1的贡献是10,最后一个位置1的贡献是1
考虑完贡献之后,我们想使得贡献最小,极端化地考虑,就是把离最后一个位置最近的1移到最后一个位置(这里也有一个小小的贪心,就是我们要尽可能地减少操作次数,它操作次数有限制),然后把离第一个位置最近的1移到第一个位置,然后去模拟这个过程就好了,在模拟的过程中去维护中间1的个数,和移动的1对答案贡献的变化
Code:
#include <bits/stdc++.h>
using namespace std;
string s;
int n,k,L,R,cnt=0,res1=0,res2=0;
void solve(){
cnt=0;res1=0;res2=0;
s.clear();
cin>>n>>k;
cin>>s;
s=" "+s;
for(int i=1;i<=n;i++){
if(s[i]=='1') cnt++;
}
for(int i=1;i<=n;i++){
if(s[i]=='1'){
L=i;
break;
}
}
for(int i=n;i>=1;i--){
if(s[i]=='1'){
R=i;
break;
}
}
if(cnt&&k>=n-R){
k-=(n-R);
cnt--;
res1+=1;
}
if(cnt&&(k>=L-1)){
k-=(L-1);
cnt--;
res2+=10;
}
cout<<cnt*11+res1+res2<<'\n';
}
int main(){
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int T;
cin>>T;
while(T--)solve();
return 0;
}
总结:
1、对于贪心题,首先去考虑一个对象对答案的贡献是多少,然后去极端化地(即不考虑其他条件)使得贡献最大(或最小),最后再去考虑恰好满足限定条件
相关文章
- Leetcode: Find First and Last Position of Element in Sorted Array
- 2011 ACM-ICPC 成都赛区A题 Alice and Bob (博弈动规)
- What are different types of test doubles and their uses?
- Cucumber and Gherkin
- Kentico lots of log with event code “CREATEOBJ” and “UPDATEOBJ”
- What is the difference between HttpApplication class and IHttpModule?
- The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
- code point of € ,and é
- What is the difference between application server and web server?
- Why there is two completely different version of Reverse for List and IEnumerable?
- kentico version history and upgrade
- 关系抽取TPLinker: Single-stage Joint Extraction of Entities and Relations Through Token Pair Linking
- XIII Open Cup named after E.V. Pankratiev. GP of Asia and South Caucasus
- Storage System and File System Courses
- English Voice of <<All Of Me>>
- .NET错误The 'targetFramework' attribute in the <compilation> element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- CodeForces 518B Tanya and Postcard (题意,水题)
- 论文阅读:Design and Implementation of a Virtual Reality Application for Mechanical Assembly Training
- 论文阅读:Learning Motion Parameterizations of Mobile Pick and Place Actions from Observing Humans in Virtual Environments
- 转 dos 下的 find 和 重定向 and 删除
- Futures and promises
- EXTRACT FILES AND IMAGES FROM A SHAREPOINT CONTENT DATABASE
- The Pros and Cons of Using Third-Party APIs
- csharp: Export DataSet into Excel and import all the Excel sheets to DataSet
- Csharp: Winform 顏色選擇器 Color convert RGB and RGB convert Color
- 【转载】 Do's and Don'ts of using t-SNE to Understand Vision Models —— t-SNE 作者写的使用指南(PPT版本)
- Linux C/C++ ------ “” and <> in the use of head include file(Pending Verification)
- hdu5414(2015多校10)--CRB and String(字符串匹配)
- leetcode 34. Find First and Last Position of Element in Sorted Array 在排序数组中查找元素的第一个和最后一个位置(中等)