zoukankan      html  css  js  c++  java
  • 51Nod 1065 最小正子段和

     1 #include <iostream>
     2 #include <algorithm>
     3 
     4 using namespace std;
     5 const int maxn = 50000 + 5;
     6 struct node{
     7     long long x;
     8     int id;
     9 }a[maxn];
    10 
    11 bool cmp(node xx, node yy){
    12     if (xx.x == yy.x)
    13         return xx.id < yy.id;
    14     else return xx.x < yy.x;
    15 }
    16 
    17 int main(){
    18     ios::sync_with_stdio(false);
    19     int n;
    20     cin >> n;
    21     int xx;
    22     a[0].id = 0;
    23     a[0].x = 0;
    24     for (int i = 1; i <= n; i++){
    25         cin >> xx;
    26         a[i].x = a[i - 1].x + xx;
    27         a[i].id = i;
    28     }
    29     sort(a, a + n + 1, cmp);
    30     long long ans;
    31     bool flag = true;
    32     for (int i = 1; i <= n; i++){
    33         if (a[i].x - a[i - 1].x > 0 && a[i].id - a[i - 1].id > 0){
    34             if (flag){
    35                 ans = a[i].x - a[i - 1].x;
    36                 flag = false;
    37             }
    38             else{
    39                 ans = min(ans, a[i].x - a[i - 1].x);
    40             }
    41         }
    42     }
    43     cout << ans << endl;
    44     system("pause");
    45     return 0;
    46 }
  • 相关阅读:
    大数据概述
    递归下降语法分析
    消除左递归c语言文法
    自动转换机
    简单的C语言文法
    实验报告一 词法分析程序
    组合数据类型练习
    Python绘制五星红旗
    熟悉常用Linux操作
    大数据概述
  • 原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8697135.html
Copyright © 2011-2022 走看看