当前位置:网站首页>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;
}
边栏推荐
- 渗透测试 ( 5 ) --- 扫描之王 nmap、渗透测试工具实战技巧合集
- Penetration test (1) -- necessary tools, navigation
- 信息安全-威胁检测-flink广播流BroadcastState双流合并应用在过滤安全日志
- 【高老师UML软件建模基础】20级云班课习题答案合集
- 信息安全-安全编排自动化与响应 (SOAR) 技术解析
- Opencv learning log 29 -- gamma correction
- [teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class
- Perform general operations on iptables
- D - function (HDU - 6546) girls' competition
- China's peripheral catheter market trend report, technological innovation and market forecast
猜你喜欢
Determine the Photo Position
Penetration test (4) -- detailed explanation of meterpreter command
STM32 learning record: LED light flashes (register version)
1323. Maximum number of 6 and 9
STM32 how to use stlink download program: light LED running light (Library version)
2027. Minimum number of operations to convert strings
Information security - threat detection engine - common rule engine base performance comparison
Information security - Analysis of security orchestration automation and response (soar) technology
Quick to typescript Guide
Openwrt source code generation image
随机推荐
C language must memorize code Encyclopedia
Alice and Bob (2021 Niuke summer multi school training camp 1)
nodejs爬虫
滲透測試 ( 1 ) --- 必備 工具、導航
socket通讯
Opencv learning log 12 binarization of Otsu method
【练习-7】Crossword Answers
[exercise-8] (UVA 246) 10-20-30== simulation
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
Borg Maze (BFS+最小生成树)(解题报告)
树莓派CSI/USB摄像头使用mjpg实现网页摄像头监控
China potato slicer market trend report, technical dynamic innovation and market forecast
Opencv learning log 31 -- background difference
China's peripheral catheter market trend report, technological innovation and market forecast
Information security - threat detection engine - common rule engine base performance comparison
Gartner: five suggestions on best practices for zero trust network access
Penetration test (8) -- official document of burp Suite Pro
Frida hook so layer, protobuf data analysis
【练习-6】(Uva 725)Division(除法)== 暴力
【练习-7】(Uva 10976)Fractions Again?!(分数拆分)