当前位置:网站首页>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;
}边栏推荐
- Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)
- STM32 learning record: LED light flashes (register version)
- Opencv learning log 28 -- detect the red cup cover
- PySide6 信号、槽
- Opencv learning log 13 corrosion, expansion, opening and closing operations
- Opencv learning log 31 -- background difference
- Openwrt source code generation image
- 【练习-3】(Uva 442)Matrix Chain Multiplication(矩阵链乘)
- Programmers, what are your skills in code writing?
- Ball Dropping
猜你喜欢

1689. Ten - the minimum number of binary numbers

Ball Dropping

Borg maze (bfs+ minimum spanning tree) (problem solving report)
![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’

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

1005. Maximized array sum after K negations

Penetration test (8) -- official document of burp Suite Pro
Quick to typescript Guide
![[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class](/img/57/bc6eda91f7263acda38b9ee8732318.png)
[teacher Gao UML software modeling foundation] collection of exercises and answers for level 20 cloud class

Nodejs+vue网上鲜花店销售信息系统express+mysql
随机推荐
Gartner: five suggestions on best practices for zero trust network access
The most complete programming language online API document
STM32 how to use stlink download program: light LED running light (Library version)
【练习-11】4 Values whose Sum is 0(和为0的4个值)
【练习-6】(PTA)分而治之
[exercise-9] Zombie's Treasury test
Socket communication
C basic grammar
D - function (HDU - 6546) girls' competition
What is the difficulty of programming?
Alice and Bob (2021 Niuke summer multi school training camp 1)
1903. Maximum odd number in string
渗透测试 2 --- XSS、CSRF、文件上传、文件包含、反序列化漏洞
渗透测试 ( 8 ) --- Burp Suite Pro 官方文档
X-Forwarded-For详解、如何获取到客户端IP
信息安全-史诗级漏洞Log4j的漏洞机理和防范措施
nodejs爬虫
China exterior wall cladding (EWC) market trend report, technical dynamic innovation and market forecast
对iptables进行常规操作
Opencv learning log 14 - count the number of coins in the picture (regardless of overlap)