Codeforces Round #249 (Div. 2)——B. Pasha Maximizes

本文介绍如何使用贪心算法解决将一个整数通过最多k次相邻数字交换操作变为最大的问题,包括输入输出规范、示例解析及代码实现。
B. Pasha Maximizes
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.

Help Pasha count the maximum number he can get if he has the time to make at most k swaps.

Input

The single line contains two integers a and k (1 ≤ a ≤ 1018; 0 ≤ k ≤ 100).

Output

Print the maximum number that Pasha can get if he makes at most k swaps.

Sample test(s)
Input
1990 1
Output
9190
Input
300 0
Output
300
Input
1034 2
Output
3104
Input
9090000078001234 6
Output
9907000008001234




简单贪心应用

#include<map> 
#include<set>
#include<list>  
#include<queue>  
#include<stack>  
#include<vector>  
#include<cmath>  
#include<cstdio>  
#include<cstring>  
#include<iostream>  
#include<algorithm>  
  
using namespace std; 

int cmp(int a, int b)
{
	return a > b;
}

int arr[20];
int b[20];
int main()
{
	__int64 a;
	int k;
	while(~scanf("%I64d", &a))
	{
		scanf("%d", &k);
		__int64 ca = a;
		int dig = 0;
		while(ca)
		{
			ca /= 10;
			dig++;
		}
		for(int i = dig - 1; i >= 0; i--)
		{
			arr[i] = a % 10;
			b[i] = arr[i];
			a /= 10;
		}
		sort(b, b + dig, cmp);
		int s = 0;
		while(k > 0)
		{
			int maxs = -1, pos = 0;
			int len = min(s + k, dig - 1);
			for(int i = s; i <= len; i++)
			{
				if(maxs < arr[i])
				{
					maxs = arr[i];
					pos = i;
				}
			}
			for(int i = pos; i > s; i--)
			{
				arr[i] ^= arr[i - 1];
				arr[i - 1] ^= arr[i];
				arr[i] ^= arr[i - 1];
			}
			k -= pos - s;
			s++;
		}
		for(int i = 0; i < dig; i++)
			printf("%d", arr[i]);
		printf("\n");
	}
	return 0;
}


内容概要:本文详细介绍了一个基于Java与Vue的食品安全溯源与智能分析系统的设计与实现,涵盖项目背景、目标意义、面临挑战及解决方案,并阐述了系统的整体架构与核心技术模块。系统通过集成物联网设备实现全流程数据采集,采用分布式数据库保障大数据存储与高效访问,结合机器学习算法进行风险预测与智能预警,同时利用可视化技术呈现溯源链路与分析结果,实现了食品从生产到销售全过程的透明化、智能化管理。文中还提供了关键模块的代码示例,如数据清洗、特征提取、决策树模型训练与预测、溯源接口开发等,增强了项目的可实施性与参考价值。; 适合人群:具备Java开发基础、熟悉Spring Boot和Vue框架,有一定前后端开发经验的软件工程师或计算机专业学生,尤其适合从事食品安全、物联网、大数据分析等相关领域技术研发的人员; 使用场景及目标:①构建食品全链条溯源体系,提升企业对食品安全事件的快速响应能力;②实现生产流程数字化管理,支持政府监管与消费者透明查询;③应用机器学习进行风险建模与智能预警,推动食品行业智能化转型; 阅读建议:建议结合文中提供的模型描述与代码示例,深入理解各模块设计逻辑,重点关注数据处理流程、算法实现与前后端交互机制,可基于该项目进行二次开发或拓展应用于其他行业的溯源系统建设。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值