当前位置:网站首页>Suffix expression (greed + thinking)
Suffix expression (greed + thinking)
2022-07-06 16:08:00 【AC__ dream】
analysis : First, if the given symbols are all positive signs , Then you can only add up all the numbers as the result , If the given symbol contains a symbol , We can make the following changes :
Ax-(Ai-Aj+Ak)=Ax-Ai+Aj-Ak, If the plus sign is placed in brackets , Then the plus sign can become a minus sign , The plus sign is placed outside the brackets , The same goes for the minus sign , Put it in brackets and turn it into a plus sign , And the minus sign is still placed outside the brackets , But because there is a minus sign in front of the bracket , So the first number in brackets must be preceded by a minus sign , The number to the left of the bracket must be a plus sign , So the number of numbers we can add is 1~n+m, According to the greedy thought, it is not difficult to figure out that the number on the left of the bracket should take the maximum , And the first number in brackets is taken as a minus , Therefore, the minimum value should be taken , That's the idea , Now let's look at the code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
const int N=2e5+10;
long long a[N];
int main()
{
int n,m;
cin>>n>>m;
for(int i=1;i<=n+m+1;i++)
scanf("%lld",&a[i]);
long long ans=0;
if(!m)
{
for(int i=1;i<=n+m+1;i++)
ans+=a[i];
}
else
{
sort(a+1,a+n+m+2);
ans=a[n+m+1]-a[1];
for(int i=2;i<=n+m;i++)
ans+=abs(a[i]);
}
printf("%lld",ans);
return 0;
}
边栏推荐
- STM32 how to use stlink download program: light LED running light (Library version)
- China potato slicer market trend report, technical dynamic innovation and market forecast
- D - function (HDU - 6546) girls' competition
- [exercise-3] (UVA 442) matrix chain multiplication
- [exercise -10] unread messages
- Understand what is a programming language in a popular way
- If you want to apply for a programmer, your resume should be written like this [essence summary]
- 渗透测试 ( 4 ) --- Meterpreter 命令详解
- What is the difficulty of programming?
- 信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
猜你喜欢
b站 实时弹幕和历史弹幕 Protobuf 格式解析
信息安全-威胁检测引擎-常见规则引擎底座性能比较
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool
628. Maximum product of three numbers
【练习-4】(Uva 11988)Broken Keyboard(破损的键盘) ==(链表)
快速转 TypeScript 指南
C language is the watershed between low-level and high-level
Web based photo digital printing website
Data storage in memory & loading into memory to make the program run
随机推荐
Analysis of protobuf format of real-time barrage and historical barrage at station B
Differential (one-dimensional, two-dimensional, three-dimensional) Blue Bridge Cup three body attack
2078. Two houses with different colors and the farthest distance
Vs2019 initial use
对iptables进行常规操作
【练习4-1】Cake Distribution(分配蛋糕)
SSM框架常用配置文件
Ball Dropping
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Penetration testing (5) -- a collection of practical skills of scanning King nmap and penetration testing tools
Penetration test (3) -- Metasploit framework (MSF)
信息安全-安全编排自动化与响应 (SOAR) 技术解析
Luogu P1102 A-B number pair (dichotomy, map, double pointer)
想应聘程序员,您的简历就该这样写【精华总结】
JS call camera
基于web的照片数码冲印网站
STM32 learning record: LED light flashes (register version)
Truck History
信息安全-威胁检测-NAT日志接入威胁检测平台详细设计
【高老师UML软件建模基础】20级云班课习题答案合集