Blue Bridge Zhenti: Postfix Expression, Plane Segmentation

text

postfix expression

Topic description

时间限制: 1.0s 内存限制: 512.0MB 本题总分:25

【问题描述】 

给定 N 个加号、M 个减号以及 N + M + 1 个整数 A1,A2,··· ,AN+M+1
小明想知道在所有由这 N 个加号、M 个减号以及 N + M +1 个整数
凑出的合法的后缀表达式中,结果最大的是哪一个?
请你输出这个最大的结果。 
例如使用1 2 3 + -,则 “2 3 + 1 -” 这个后缀表达式结果是 4,是最大的。

【输入格式】
第一行包含两个整数 N 和 M。 第二行包含 N + M + 1 个整数 A1,A2,··· ,AN+M+1

【输出格式】
输出一个整数,代表答案。

【样例输入】 
1 1 1 2 3
【样例输出】 
4
【评测用例规模与约定】 
对于所有评测用例,0≤ N,M ≤100000,−109 ≤ Ai ≤109

Topic ideas and codes

There are two cases:

  • The first one: In the case of no negative sign, just add all of them directly.
  • The second case: there are plus signs and minus signs, no matter how you input, we can take the maximum value minus the minimum value and then add the absolute value of the remaining number.
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll maxn=1e6+10;
ll n,m;
ll a[maxn],b[maxn];
ll cnt;
ll sum=0,sum1;
ll cnt1=0;
int main(){
    
    
    cin>>n>>m;
    for(int i=1;i<=n+m+1;i++){
    
    
        cin>>a[i];
        sum1+=a[i];
    }
    sort(a+1,a+1+n+m+1);
    if(m==0)cout<<sum1;
    else{
    
    
        sum=a[n+m+1]-a[1];
        for(int i=2;i<=n+m;i++){
    
    
            sum+=abs(a[i]);
        }
        cout<<sum;
    }
    return 0;
}

Plane segmentation

Topic description

Problem Description
There is a straight line in a plane, where the first straight line is .

Calculate the lines that divide the plane into sections.

Input format
The first line contains an integer.

The following N lines, each containing two integers.

Output Format
An integer representing the answer.

样例输入
3
1 1
2 2
3 3
Data
样例输出
6

Topic ideas and codes

Idea: First, deduplicate the input data through the set container. According to "the contribution of each additional line to the increase in the number of planes is the number of intersections with the previous line (excluding points that overlap with existing intersections) + 1 "Conclusions are accumulated, and the final result can be output.
Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<double,double> P;
set<P> se;
const ll maxn=1e6+10;
ll n;
ll a[maxn],b[maxn];
int main(){
    
    
    cin>>n;
    for(int i=1;i<=n;i++){
    
    
        ll x,y;
        cin>>x>>y;
        se.insert(make_pair(x,y));
    }
    int n=0;
    for(set<P>::iterator it=se.begin();it!=se.end();it++){
    
    
        a[n]=(*it).first;
        b[n]=(*it).second;
        n++;
    }
    se.clear();
    ll ans=2;
    for(int i=1;i<n;i++){
    
    
        for(int j=i-1;j>=0;j--){
    
    
            if(a[i]==a[j])continue;
            double x=(b[i]-b[j])*1.0/(a[j]-a[i]);
            double y=a[i]*(x)+b[i];
            se.insert(make_pair(x,y));
        }
        ans+=se.size()+1;
        se.clear();
    }
    cout<<ans;
    return 0;
}

Epilogue


"If you are undecided, you can ask the spring breeze, and if the spring breeze does not speak, you will follow your heart" means: if you are hesitant about something, ask the spring breeze how to do it. . "If you are undecided, you can ask the spring breeze. If the spring breeze does not speak, you will follow your heart." The sentence comes from the "Jianlai" written by the Internet writer "Fenghuo Opera Princes". The original text is: "If you are undecided, you can ask the spring breeze. Follow your heart".

insert image description here


Guess you like

Origin blog.csdn.net/weixin_46627433/article/details/124028762