凉肝B题(Just Eat It!)

B. Just Eat It!
Today, Yasser and Adel are at the shop buying cupcakes. There are n cupcake types, arranged from 1 to n on the shelf, and there are infinitely many of each type. The tastiness of a cupcake of type i is an integer ai. There are both tasty and nasty cupcakes, so the tastiness can be positive, zero or negative.
Yasser, of course, wants to try them all, so he will buy exactly one cupcake of each type.
On the other hand, Adel will choose some segment [l,r] (1≤l≤r≤n1≤l≤r≤n 1≤l≤r≤n1≤l≤r≤n) that does not include all of cupcakes (he can’t choose [l,r]=[1,n]) and buy exactly one cupcake of each of types l,l+1,…,r.l,l+1,…,r. l,l+1,…,r.l,l+1,…,r.
After that they will compare the total tastiness of the cupcakes each of them have bought. Yasser will be happy if the total tastiness of cupcakes he buys is strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice.
For example, let the tastinesses of the cupcakes be [7,4,−1]. Yasser will buy all of them, the total tastiness will be 7+4−1=10. Adel can choose segments [7],[4],[−1],[7,4] or [4,−1], their total tastinesses are 7,4,−1,11 and 3, respectively. Adel can choose segment with tastiness 11, and as 10 is not strictly greater than 11, Yasser won’t be happy 😦
Find out if Yasser will be happy after visiting the shop.

Input
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains n (2≤n≤105).
The second line of each test case contains n integers a1,a2,…,an (−109≤aiai a_iai​≤109), where ai represents the tastiness of the ithith i_{th}ith​ type of cupcake.
It is guaranteed that the sum of n over all test cases doesn’t exceed 105105 10^5105.
Output
For each test case, print “YES”, if the total tastiness of cupcakes Yasser buys will always be strictly greater than the total tastiness of cupcakes Adel buys regardless of Adel’s choice. Otherwise, print “NO”.
Example
Input
3
4
1 2 3 4
3
7 4 -1
3
5 -5 5
Output
YES
NO
NO

Note
In the first example, the total tastiness of any segment Adel can choose is less than the total tastiness of all cupcakes.
In the second example, Adel will choose the segment [1,2] with total tastiness 11, which is not less than the total tastiness of all cupcakes, which is 10.
In the third example, Adel can choose the segment [3,3] with total tastiness of 5. Note that Yasser’s cupcakes’ total tastiness is also 5, so in that case, the total tastiness of Yasser’s cupcakes isn’t strictly greater than the total tastiness of Adel’s cupcakes.

#include <iostream>
#include <cstdio>
using namespace std;
int a[200],opt[200],N; 
int max(int a,int b){
  return a>b ? a:b;
}
int main(int argc, char const *argv[]) {
  int n;
  scanf("%d",&n);
  for(int i=1; i<=n; i++){
  	int sum = 0,flag = 0;
    scanf("%d",&N);
    for(int i = 0; i < N; i++){
    	scanf("%d", &a[i]);
      sum += a[i];
      if(a[i] <= 0) 
      	flag = 1;
    }
    if(flag == 0){//全是正数
    	printf("YES\n");
    	continue;} 
    else{
    opt[0] = a[0];
    for (int i = 1; i < N; i++) {
    opt[i] = max(a[i],opt[i-1] + a[i]);
    }
    int optmax = opt[0];
    for (int i = 1; i < N; i++) {
      if (optmax < opt[i]) 
        optmax = opt[i];
    }
  if(sum >= optmax)
    printf("YES\n");
  else
    printf("NO\n");}
  }
  return 0;
}

3
3
1 2 3
4
1 2 3 4
2
-1 2

output
6
10
2

1.暴力 复杂度O(N^3)

#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[]) {
  int n,N,a[200];
  scanf("%d",&n);
  for(int i=1; i<=n; i++){
    scanf("%d",&N);
  for(int i=0; i<N; i++)
    scanf("%d",&a[i]);
  int anx = a[0];
  //ans保存最大子序列和,初始化为a[0]能保证最终结果正确
    //i和j分别是枚举的子序列的起点和终点,k所在循环计算每个子序列的和
  for(int i=0; i<N; i++){
    for(int j=i; j<N; j++){
      int s = 0;
      for(int k=i; k<=j; k++)
        s += a[k];
      if (s > anx)
        anx = s;
    }
  }
  printf("%d\n", anx);
  }
  return 0;
}

2.一点简单的优化 复杂度O(N^2)

#include <iostream>
#include <cstdio>
using namespace std;
int main(int argc, char const *argv[]) {
  int n,N,a[200],sum[200];
  scanf("%d",&n);
  for(int i=1; i<=n; i++){
    scanf("%d",&N);
  for(int i=0; i<N; i++)
    scanf("%d",&a[i]);
  int anx = a[0];
  //开一个sum数组,sum[i]代表数组a[N]前i项和 
  for (int i = 0; i < N; i++) {
    /* code */
    sum[i] = a[i] + sum[i-1];
  }
  //ans保存最大子序列和,初始化为a[0]能保证最终结果正确
    //i和j分别是枚举的子序列的起点和终点,k所在循环计算每个子序列的和
  for(int i=0; i<N; i++){
    for(int j=i; j<N; j++){
      int s = 0;
      s = sum[j] - sum[i-1];
      if (s > anx)
        anx = s;
    }
  }
  printf("%d\n", anx);
  }
  return 0;
}

3.分而治之(递归)复杂度O(N*logN)

#include <iostream>
#include <cstdio>
using namespace std;
/*1.最大序列在左边left
  2.最大序列在右边right
  3.最大序列在中间,横跨
*/
int a[200],N; 
int solve(int left,int right){
  //序列长度为1时
  if (left == right) {
    return a[left];
  }
  int mid = (left + right) >> 1;//相当于(left + right)/2
  int lans = solve(left,mid);
  int rans = solve(mid+1,right);
  //第3种情况
  int sum = 0,lmax = a[mid],rmax = a[mid+1];
  for (int i=mid; i>=0; i--) {
    sum += a[i];
    if(sum > lmax)
      lmax = sum;
  }
  for (int i = mid+1; i < N; i++) {
    sum += a[i];
    if(sum > rmax)
      rmax = sum;
  }
  int ans = rmax + lmax;
  if(lans > ans)
    ans = lans;
  else if(rans > ans)
    ans = rans;
  return ans;
}
int main(int argc, char const *argv[]) {
  int n;
  scanf("%d",&n);
  for(int i=1; i<=n; i++){
    scanf("%d",&N);
    for(int i = 1; i <= N; i++)
     scanf("%d", &a[i]);
  printf("%d\n", solve(1,N));
  }
  return 0;
}

4.动态规划 复杂度O(N)

#include <iostream>
#include <cstdio>
using namespace std;
int a[200],opt[200],N; 
int max(int a,int b){
  return a>b ? a:b;
}
int main(int argc, char const *argv[]) {
  int n;
  scanf("%d",&n);
  for(int i=1; i<=n; i++){
    scanf("%d",&N);
    for(int i = 0; i < N; i++)
     scanf("%d", &a[i]);
    opt[0] = a[0];
    for (int i = 1; i < N; i++) {
      opt[i] = max(a[i],opt[i-1] + a[i]);
    }
    int optmax = opt[0];
    for (int i = 1; i < N; i++) {
      if (optmax < opt[i]) 
        optmax = opt[i];
    }
  printf("%d\n",optmax );
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值