2 条题解

  • 1
    @ 2025-9-16 20:53:23
    n=int(input())
    for _ in range(n):
        print("YES")
    
    • 0
      @ 2025-9-16 20:48:54

      核心逻辑

      题目要求判断是否存在一个子数组(连续元素),其最大值大于等于该子数组的元素和。关键观察:

      • 单元素子数组一定满足条件:此时最大值等于元素和(自身),即 max = sum,满足 max >= sum
      • 由于数组长度 n ≥ 1,必然存在至少一个单元素子数组,因此所有测试用例的答案都是 "YES"

      代码实现

      C

      #include <stdio.h>
      
      int main() {
          int t;
          scanf("%d", &t);
          while (t--) {
              int n;
              scanf("%d", &n);
              // 无需处理数组元素,直接输出YES
              printf("YES\n");
              // 读取数组(避免输入残留影响后续测试用例)
              for (int i = 0; i < n; i++) {
                  long long x;
                  scanf("%lld", &x);
              }
          }
          return 0;
      }
      

      C++

      #include <bits/stdc++.h>
      using namespace std;
      
      int main() {
          ios::sync_with_stdio(false);
          cin.tie(nullptr);
          
          int t;
          cin >> t;
          while (t--) {
              int n;
              cin >> n;
              // 读取数组但不处理
              vector<long long> a(n);
              for (auto &x : a) cin >> x;
              // 直接输出YES
              cout << "YES\n";
          }
          return 0;
      }
      

      Python

      import sys
      
      t = int(sys.stdin.readline())
      for _ in range(t):
          n = int(sys.stdin.readline())
          # 读取数组但不处理
          a = list(map(int, sys.stdin.readline().split()))
          # 直接输出YES
          print("YES")
      

      逻辑验证

      对于任意非空数组(n ≥ 1):

      • 取子数组 (i, i)(单元素),此时 max(ai) = aisum(ai) = ai,显然满足 max ≥ sum
      • 因此,无论数组元素如何取值,必然存在满足条件的子数组,答案恒为 "YES"。

      所有测试用例均符合此逻辑,代码简洁高效。

      • 1

      信息

      ID
      497
      时间
      1000ms
      内存
      256MiB
      难度
      6
      标签
      (无)
      递交数
      24
      已通过
      10
      上传者