CF-845B
B. Luba And The Tickettime limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Luba has a ticket consisting of 6 digits. In one move she can choose digit in any position and replace it with arbitrary digit. She wants to know the minimum number of digits she needs to replace in order to make the ticket lucky.
The ticket is considered lucky if the sum of first three digits equals to the sum of last three digits.
InputYou are given a string consisting of 6 characters (all characters are digits from 0 to 9) — this string denotes Luba's ticket. The ticket can start with the digit 0.
OutputPrint one number — the minimum possible number of digits Luba needs to replace to make the ticket lucky.
Examplesinput000000output0input123456output2input111000output1NoteIn the first example the ticket is already lucky, so the answer is 0.
In the second example Luba can replace 4 and 5 with zeroes, and the ticket will become lucky. It's easy to see that at least two replacements are required.
In the third example Luba can replace any zero with 3. It's easy to see that at least one replacement is required.
题意:
对于给出的长度为6的整数串,求最小改变多少个整数可以使前三个数之和等于后三个数。
起初直接暴力写太复杂了,可以先求出前后的和s1,s2 。将较小的一边三个数变为9-a[i],即将它们全部变为9时差值的变化量,而后排序,从大到小减差值直到差值不大于0即可。
AC代码:
#include<bits/stdc++.h>
using namespace std; int a[]; int main(){
ios::sync_with_stdio(false);
string s;
cin>>s;
int s1=,s2=;
for(int i=;i<;i++){
a[i]=s[i]-'';
s1+=a[i];
}
for(int i=;i<;i++){
a[i]=s[i]-'';
s2+=a[i];
}
int num=s1-s2;
if(num>){
for(int i=;i<;i++){
a[i]=-a[i];
}
}
else{
for(int i=;i<;i++){
a[i]=-a[i];
}
num=-num;
}
sort(a,a+);
for(int i=;i>;i--){
if(num<=){
cout<<-i<<endl;
break;
}
num-=a[i];
}
return ;
}
CF-845B的更多相关文章
- ORA-00494: enqueue [CF] held for too long (more than 900 seconds) by 'inst 1, osid 5166'
凌晨收到同事电话,反馈应用程序访问Oracle数据库时报错,当时现场现象确认: 1. 应用程序访问不了数据库,使用SQL Developer测试发现访问不了数据库.报ORA-12570 TNS:pac ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- cf Round 613
A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...
- ARC下OC对象和CF对象之间的桥接(bridge)
在开发iOS应用程序时我们有时会用到Core Foundation对象简称CF,例如Core Graphics.Core Text,并且我们可能需要将CF对象和OC对象进行互相转化,我们知道,ARC环 ...
- [Recommendation System] 推荐系统之协同过滤(CF)算法详解和实现
1 集体智慧和协同过滤 1.1 什么是集体智慧(社会计算)? 集体智慧 (Collective Intelligence) 并不是 Web2.0 时代特有的,只是在 Web2.0 时代,大家在 Web ...
- CF memsql Start[c]UP 2.0 A
CF memsql Start[c]UP 2.0 A A. Golden System time limit per test 1 second memory limit per test 256 m ...
- CF memsql Start[c]UP 2.0 B
CF memsql Start[c]UP 2.0 B B. Distributed Join time limit per test 1 second memory limit per test 25 ...
- CF #376 (Div. 2) C. dfs
1.CF #376 (Div. 2) C. Socks dfs 2.题意:给袜子上色,使n天左右脚袜子都同样颜色. 3.总结:一开始用链表存图,一直TLE test 6 (1)如果需 ...
- CF #375 (Div. 2) D. bfs
1.CF #375 (Div. 2) D. Lakes in Berland 2.总结:麻烦的bfs,但其实很水.. 3.题意:n*m的陆地与水泽,水泽在边界表示连通海洋.最后要剩k个湖,总要填掉多 ...
- CF #374 (Div. 2) D. 贪心,优先队列或set
1.CF #374 (Div. 2) D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...
随机推荐
- 转载 ---原生和H5交互挺多的,最近也有朋友再问。这儿我写个简单的例子给大家 直接贴代码 js的
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> ...
- Android-彻底地理解Binder
转自:https://blog.csdn.net/huachao1001 https://blog.csdn.net/huachao1001/article/details/51504469 你是不是 ...
- iOS开发 僵尸调试
本文转载至 http://blog.sina.com.cn/s/blog_a843a8850101dxin.html 引自:http://blog.csdn.net/likendsl/articl ...
- JS基础常识理解
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- C#单元测试(转)
C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...
- python 搜集参数的共有项和所有项
搜集共性项和所有项 ###搜集共有参数值 def intersect(*args): res=[] for x in args[0]: for other in args[1:]: if x not ...
- 设计模式 - 单件模式(singleton pattern) 具体解释
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/28595349 单件模式(singleton ...
- 使用阿里云maven镜像加速jar包下载
编辑 MAVEN_HOME/conf 文件夹下的 settings.xml,找到 <mirrors> 节点,把下面内容添加在其子节点内: <mirror> <id> ...
- nginx负载均衡设置
在nginx的配置文件[./conf/nginx.conf]中加入: upstream test.balance123.com { server 192.168.1.223:80; server 19 ...
- Android Wifi简单的梳理【转】
本文转载自:http://blog.csdn.net/gabbzang/article/details/10005411 代表一个已经配置过的网络.包含网络ID(networkId).该网络ID代表的 ...