当前位置:网站首页>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;
}边栏推荐
- Interesting drink
- 渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
- CS zero foundation introductory learning record
- The concept of C language array
- 渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
- [exercise -11] 4 values why sum is 0 (and 4 values of 0)
- Ball Dropping
- 最全编程语言在线 API 文档
- 【练习-8】(Uva 246)10-20-30==模拟
- 渗透测试 ( 4 ) --- Meterpreter 命令详解
猜你喜欢

B - Code Party (girls' competition)

C language must memorize code Encyclopedia

628. Maximum product of three numbers

Penetration test (4) -- detailed explanation of meterpreter command

Data storage in memory & loading into memory to make the program run

信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志

B - 代码派对(女生赛)

1010 things that college students majoring in it must do before graduation

Matlab comprehensive exercise: application in signal and system
![[exercise-4] (UVA 11988) broken keyboard = = (linked list)](/img/59/78ca7170ab1fd364ec44cfbcdc7ab5.png)
[exercise-4] (UVA 11988) broken keyboard = = (linked list)
随机推荐
Openwrt source code generation image
[exercise-6] (PTA) divide and conquer
渗透测试 ( 7 ) --- 漏洞扫描工具 Nessus
Programmers, what are your skills in code writing?
渗透测试 ( 3 ) --- Metasploit Framework ( MSF )
Nodejs+vue online fresh flower shop sales information system express+mysql
Truck History
Find 3-friendly Integers
Data storage in memory & loading into memory to make the program run
CS zero foundation introductory learning record
基于web的照片数码冲印网站
Basic Q & A of introductory C language
【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
Socket communication
【高老师UML软件建模基础】20级云班课习题答案合集
Research Report on market supply and demand and strategy of China's earth drilling industry
【练习-5】(Uva 839)Not so Mobile(天平)
2078. Two houses with different colors and the farthest distance
1903. Maximum odd number in string