HPU组队赛L(没有标题。。)(贪心)
时间限制 1 Second 内存限制 512 Mb
题目描述
给个字符串s和整数k.(字符串中只有0-9)
问至少修改字符串中的几位才可以让字符串的各个位之和大于等于k.
输入
第一行一个整数T表示有T组测试数据 (1 ≤ T ≤ 10)
接下来每组测试数据:
第一行给定一个整数k.
第二行给定字符串s. (1 ≤ strlen(s), k ≤ 1e5)
输出
每组数据输出一行表示最小的修改次数,如果无法满足题意请输出-1.
输入样例
2
5
13
100
123
输出样例
1
-1
思路
这个题的Tag是贪心,但是感觉和贪心没什么关系(可能是我对贪心的理解不够)
思路就是先求出来字符串的长度L,计算字符串所有的字符更改后能达到的最大值(也就是9*L)与k进行比较,如果小于k,那么一定无法达到要求,输出-1
如果大于k,将字符串的各位换成整型存起来,并计算各位数字相加的总和,如果大于k,那么输出0 ,如果小于k,将整型数升序排序,从第一位开始更改(因为题意要求输出最小的修改次数,所以要把小的数变成最大的才能使每一次修改后的总和增加的最大,升序排序会更容易操作一些),记录下来更改的次数输出就可以了
其实上面的这些过程都可以在一起进行(标程也是这样写的),但是用的时间稍微多了一点
AC代码
我自己写的
/**
* Time: 1ms Memory: 3MB Lang: C++ Author: wzy
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <limits.h>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <string>
#define ll long long
#define ull unsigned long long
#define ms(a) memset(a,0,sizeof(a))
#define pi acos(-1.0)
#define INF 0x7f7f7f7f
#define lson o<<1
#define rson o<<1|1
const double E=exp(1);
const int maxn=1e6+10;
const int mod=1e9+7;
using namespace std;
char ch[maxn];
int a[maxn];
int main(int argc, char const *argv[])
{
ios::sync_with_stdio(false);
int t;
int k;
cin>>t;
while(t--)
{
cin>>k;
cin>>ch;
int l=strlen(ch);
if(9*l<k)
{
cout<<-1<<endl;
continue;
}
int res=0;
for(int i=0;i<l;i++)
{
a[i]=ch[i]-'0';
res+=a[i];
}
if(res>=k)
{
cout<<0<<endl;
continue;
}
sort(a,a+l);//升序
int ans=0;
for(int i=0;i<l;i++)
{
if(a[i]==9)
continue;
res=res+9-a[i];
ans++;
if(res>=k)
{
cout<<ans<<endl;
break;
}
}
}
return 0;
}
标程
/**
* Time: 2ms Memory: 3MB Lang: C++
*/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll k, g, ans;
string s;
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T; cin>>T;
while(T--) {
ans=0,g=0;
s.clear();
cin >> k;
cin >> s;
for (int i = 0; i < s.length(); i++)
g += s[i] - '0';
sort(s.begin(), s.end());
for (int i = 0; i < s.length(); i++) {
if(s[i] == '9') break;
if (g < k) {
g += 9 - s[i] + '0';
ans++;
}
}
if(g<k) cout<< -1<<endl;
else cout << ans << endl;
}
return 0;
}
HPU组队赛L(没有标题。。)(贪心)的更多相关文章
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- upc组队赛3 T-net【贪心】
T-net 题目描述 T-net which is a new telecommunications company, plans to install its base stations in th ...
- HPU组队赛J:Ball King(线段树)
时间限制 1 Second 内存限制 512 Mb 题目描述 HPU601球王争霸赛即将举行,ACMER纷纷参加. 现在有n个人报名参赛,每个人都有一个实力值 ai,实力值较大者获胜. 为保证比赛公 ...
- HPU组队赛B:问题(二进制枚举)
时间限制1 Second 内存限制 512 Mb 题目描述 你有n个问题,你已经估计了第i个问题的难度为Ci,现在你想使用这些问题去构造一个问题集.比赛的问题集必须包含至少两个问题,而且比赛的总难度必 ...
- upc 组队赛18 STRENGTH【贪心模拟】
STRENGTH 题目链接 题目描述 Strength gives you the confidence within yourself to overcome any fears, challeng ...
- L 裁纸片 贪心 + 模拟
https://biancheng.love/contest-ng/index.html#/123/problems 如果只是输出最小的值,那么好办,a升序,b降序,这样是最优的. 但是需要次数,这就 ...
- Liebig's Barrels CodeForces - 985C (贪心)
链接 大意:给定$nk$块木板, 要制作$n$个$k$块板的桶, 要求任意两桶容积差不超过$l$, 每个桶的容积为最短木板长, 输出$n$个桶的最大容积和 假设最短板长$m$, 显然最后桶的体积都在$ ...
- NOIP模拟 poke - 贪心
题目大意: 给一副牌(\(n \le 1000000, a_i \le 1000000\)),问最多能打出多少对对子(x, x)或者顺子(x, x + 1, x + 2). 题目分析: 感觉跟斗地主很 ...
- 马克飞象 Markdown 使用和学习
第三方学习网站 http://sspai.com/25137https://maxiang.io/ 使用工具:有道云笔记 https://note.youdao.com/ 新建时使用Mardown类型 ...
随机推荐
- 小程序swiper效果高宽设置(微信小程序交流群:604788754)
swiper的宽和高一定要设置在swiper上面.swiper-item默认继承swiper的宽和高.swiper-item容器里面的宽和高没有继承他的父节点宽和高,需要从新设置. 不明白之处,可以咨 ...
- linux初始化宏__init, __exit
我们在内核中经常遇到初始化函数是这样定义的:static int __init init_func(); ,与普通函数相比,定义中多了__init.那么,__init是什么意思呢?还有与其匹配的__e ...
- day19 反射
今日所学 : 1. isinstance , type , issubclass 2.如何区分方法和函数(代码) 3.反射(重要) 1. isinstance ,type ,issubclass is ...
- 主机访问虚拟机centos7的服务器
一.虚拟机开启桥梁接 1.编辑-->虚拟网络编辑器 2.虚拟机-->设置 二.Centos的配置---关闭防火墙下的服务器接口 Centos7.0 默认使用firewall作为防火墙,这里 ...
- 多态概念,C++
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- vue-router-5-命名路由
创建 Router 实例的时候,在 routes 配置中给某个路由设置名称 const router = new VueRouter({ routes: [ { path: '/user/:userI ...
- vue-10-混合
混合对象可以包含任意组件选项, // 定义一个混合对象 var myMixin = { created: function () { this.hello() }, methods: { hello: ...
- java中的方法method
java中的方法必须存在于类class里,不能独立存在.类是描述具有某种特征的事物,方法则是这类 事物具有的某种功能,通过调用方法可以实现某种特定的功能.方法名一般以小写的动词开头. 例: publi ...
- c算法:字符串查找-KMP算法
/* *用KMP算法实现字符串匹配搜索方法 *该程序实现的功能是搜索本目录下的所有文件的内容是否与给定的 *字符串匹配,如果匹配,则输出文件名:包含该字符串的行 *待搜索的目标串搜索指针移动位数 = ...
- 输入系统:epoll & inotify
一.epoll 作用:检测一个或多个文件的可读.可写等属性变化: 代码示例: #include <sys/epoll.h> #include <stdio.h> #includ ...