傳送門
(這個題感覺就是進階指南的原題,但是我還是wa了好幾發)
題意:給定一個陣列,讓后可以選擇前k個或者后k個數,將其減少1,問最終能否將其變成全0,
一開始用假演算法把自己騙了,今天走路的時候禿然想起來用差分就可以秒了這個題,可以構造出差分陣列,那么兩個操作就轉換成了
(1) 在第一個位置-1,讓后在[2,n+1]選擇一個位置+1,
(2) 在[1,n]選擇一個位置-1,讓后在n+1的位置+1,
對于差分之后正數的位置我們不需要考慮,因為操作(2)就可以解決,對于負數的位置,只能由第一個位置來消去,那么只需要判斷負數的大小和第一個位置大小即可,
//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;
void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int N=30010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;
int n,a[N];
int f[N];
bool check()
{
int cnt=0;
for(int i=1;i<=n;i++) if(f[i]<0) cnt-=f[i];
if(cnt<=f[1]) return true;
return false;
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int _; scanf("%d",&_);
while(_--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),f[i]=a[i]-a[i-1];
if(check()) puts("YES");
else puts("NO");
}
return 0;
}
/*
9
15 15 12 24 17 17 23 26 19
7
11 12 12 15 13 13 13
*/
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/205009.html
標籤:其他
下一篇:Android知識點2
