训练赛3_H_Roma and Changing Signs.md

本文探讨了如何通过改变序列中数字的符号来最大化公司总收入的算法策略。具体而言,给定一系列数字和允许改变符号的次数,文章提供了一种算法,通过恰当地改变指定数量的数字符号,以实现总收入的最大化。

Roma and Changing Signs

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Roma works in a company that sells TVs. Now he has to prepare a report for the last year.

Roma has got a list of the company’s incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.

The operation of changing a number’s sign is the operation of multiplying this number by -1.

Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactly k changes.

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 105), showing, how many numbers are in the sequence and how many swaps are to be made.

The second line contains a non-decreasing sequence, consisting of n integers a**i (|a**i| ≤ 104).

The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.

Output

In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes.

Examples

input

Copy

3 2
-1 -1 1

output

Copy

3

input

Copy

3 1
-1 -1 1

output

Copy

1

Note

In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.

In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1.

题目大意

给定n,kn,kn,k,有nnn个数字,你可以改变kkk次那数字的符号,每次一个。一定要用完kkk次操作,问改变后nnn个数的最大值是多少。

题目分析

其实写得时间比较多……但是写得快能过就行。用两次sort就行

首先,对输入进来的数升序排列。然后,遍历一遍改成正数,当次数到达k或者找到一个大于等于0的数的时候退出遍历。然后计算一下剩下还有多少次可以改变数字符号的操作。当还有剩下次数且次数为奇数(为偶数的时候,直接都对同一个数改变符号,耗尽操作次数。最后这个数的符号不变)时,寻找一下最小的数,然后改变那个数的符号就行。最后遍历求和

代码

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 7;
int arr[maxn];
int main(int argc, char const *argv[]) {
  int n, k;
  scanf("%d%d", &n, &k);
  for(int i = 0; i < n; i++){
    scanf("%d", &arr[i]);
  }
  sort(arr, arr + n);
  int i = 0;
  while(i < k && i < n){
    if(arr[i] < 0) arr[i] = -arr[i];
    else break;
    i++;
  }
  k -= i;
  if(k > 0 && k % 2){
    sort(arr, arr + n);
    arr[0] = -arr[0]; 
  }
  long long ans = 0;
  for(int i = 0; i < n; i++)
    ans += arr[i];
  printf("%lld\n", ans);
  return 0;
}

无界云图(开源在线图片编辑器源码)是由四川爱趣五科技推出的一款类似可画、创客贴、图怪兽的在线图片编辑器。该项目采用了React Hooks、Typescript、Vite、Leaferjs等主流技术进行开发,旨在提供一个开箱即用的图片编辑解决方案。项目采用 MIT 协议,可免费商用。 无界云图提供了一系列强大的图片编辑功能,包括但不限于: 素材管理:支持用户上传、删除和批量管理素材。 操作便捷:提供右键菜单,支持撤销、重做、导出图层、删除、复制、剪切、锁定、上移一层、下移一层、置顶、置底等操作。 保存机制:支持定时保存,确保用户的工作不会丢失。 主题切换:提供黑白主题切换功能,满足不同用户的视觉偏好。 多语言支持:支持多种语言,方便全球用户使用。 快捷键操作:支持快捷键操作,提高工作效率。 产品特色 开箱即用:无界云图采用了先进的前端技术,用户无需进行复杂的配置即可直接使用。 免费商用:项目采用MIT协议,用户可以免费使用和商用,降低了使用成本。 技术文档齐全:提供了详细的技术文档,包括技术文档、插件开发文档和SDK使用文档,方便开发者进行二次开发和集成。 社区支持:提供了微信技术交流群,用户可以在群里进行技术交流和问题讨论。 环境要求 Node.js:需要安装Node.js环境,用于运行和打包项目。 Yarn:建议使用Yarn作为包管理工具,用于安装项目依赖。 安装使用 // 安装依赖 yarn install // 启动项目 yarn dev // 打包项目 yarn build 总结 无界云图是一款功能强大且易于使用的开源在线图片编辑器。它不仅提供了丰富的图片编辑功能,还支持免费商用,极大地降低了用户的使用成本。同时,详细的文档和活跃的社区支持也为开发者提供了便利的二次开发和集成条件。无论是个人用户还是企业用户,都可以通过无界云图轻
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值