牛客补题目

牛客补题
https://ac.nowcoder.com/acm/contest/84528/C
刚开始的思路是对的,这个一定是要用前缀和的。但是后面的代码时间复杂度on方。我把变量赋值在for里面,每次都是从原来的地方开始。

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
using ll = long long;
ll a[N], pre[N];
int main()
{
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
  {
    cin >> a[i];
    pre[i] = a[i] + pre[i - 1];
  }
  // 前缀和了
  ll count = 0;
  // for (int i = 1; i <= n - 2; i++)
  // {
  //   for (int j = i + 1; j < n; j++)
  //   {
  //     ll a = pre[i]; // 1-i和
  //     ll b = pre[j] - pre[i];
  //     ll c = pre[n] - pre[j];
  //     if (a < b && b > c)
  //     {
  //       count++;
  //     }
  //   }
  // }
  int i = 1;
  int j = 2;
  // i 和j要变为全局变量 这样才能控制时间复杂度。
  //i等于1的时候 j从2开始 如果符合 那么就直接加上去
  for (; i < n; i++)
  {
    for (; j < n; j++)
    {
      ll a = pre[i];
      ll b = pre[j] - pre[i];
      ll c = pre[n] - pre[j];
      if (a < b && b > c)
      {
        count += n - j;
        break;
      }
    }
  }
  cout << count;
}

相关推荐

  1. 题目

    2024-06-16 14:52:06       29 阅读
  2. 周赛 Round 40(题)C题

    2024-06-16 14:52:06       39 阅读
  3. 网课:机器翻译——题解

    2024-06-16 14:52:06       57 阅读
  4. 周赛 Round 51题解

    2024-06-16 14:52:06       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-16 14:52:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-16 14:52:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-06-16 14:52:06       82 阅读
  4. Python语言-面向对象

    2024-06-16 14:52:06       91 阅读

热门阅读

  1. RISC-V汇编总结

    2024-06-16 14:52:06       36 阅读
  2. 【LVGL v8.3】修改 ARC 控件指针图片风格

    2024-06-16 14:52:06       31 阅读
  3. 【机器学习】基于NeRF的3D动画渲染

    2024-06-16 14:52:06       25 阅读
  4. C#面:C#支持多重继承么?

    2024-06-16 14:52:06       29 阅读
  5. 简单游戏制作——飞行棋

    2024-06-16 14:52:06       31 阅读
  6. FPGA NET

    FPGA NET

    2024-06-16 14:52:06      35 阅读
  7. mac m芯片安装win11遇坑

    2024-06-16 14:52:06       30 阅读
  8. Spring Cloud应用框架

    2024-06-16 14:52:06       35 阅读