当前位置:网站首页>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;
}边栏推荐
- D - function (HDU - 6546) girls' competition
- 【练习-6】(Uva 725)Division(除法)== 暴力
- Opencv learning log 18 Canny operator
- [exercise-9] Zombie's Treasury test
- Analyse du format protobuf du rideau en temps réel et du rideau historique de la station B
- Determine the Photo Position
- B - 代码派对(女生赛)
- JS调用摄像头
- 【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
- Hdu-6025-prime sequence (girls' competition)
猜你喜欢

Ball Dropping

1689. Ten - the minimum number of binary numbers

Penetration test (2) -- penetration test system, target, GoogleHacking, Kali tool

【练习-5】(Uva 839)Not so Mobile(天平)

PySide6 信号、槽

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

信息安全-史诗级漏洞Log4j的漏洞机理和防范措施

1903. Maximum odd number in string
![MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’](/img/e6/f4a696179282fe1f4193410c5a493a.png)
MySQL import database error [err] 1273 - unknown collation: 'utf8mb4_ 0900_ ai_ ci’

基于web的照片数码冲印网站
随机推荐
b站 實時彈幕和曆史彈幕 Protobuf 格式解析
Opencv learning log 26 -- detect circular holes and mark them
初入Redis
Research Report on surgical fluid treatment industry - market status analysis and development prospect prediction
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
C language is the watershed between low-level and high-level
Auto.js入门
【练习-11】4 Values whose Sum is 0(和为0的4个值)
[exercise -11] 4 values why sum is 0 (and 4 values of 0)
nodejs爬虫
[exercise-8] (UVA 246) 10-20-30== simulation
Research Report of peripheral venous catheter (pivc) industry - market status analysis and development prospect prediction
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
【练习-8】(Uva 246)10-20-30==模拟
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)
Nodejs crawler
Penetration test (8) -- official document of burp Suite Pro
B - Code Party (girls' competition)
F - birthday cake (Shandong race)
Essai de pénétration (1) - - outils nécessaires, navigation