洛谷 P4552 [Poetize6] IncDec Sequence【差分+腦洞】
阿新 • • 發佈:2018-09-19
class print getc += std -a 不為 lld 數組
一看區間操作,很容易想到差分
所以就是先差分,然後為了保證最小步數,把政府差分抵消,也就相當於原數組區間加減
第二問,因為差分數組抵消之後不為0就需要使用n+1的虛擬位置,而這個的值其實沒有,所以我們可以在這個抵消後的數的範圍內隨意操作,也就是任意取值
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int N=100005; long long n,a[N],c[N],t1,t2; int read() { int r=0,f=1; char p=getchar(); while(p>‘9‘||p<‘0‘) { if(p==‘-‘) f=-1; p=getchar(); } while(p>=‘0‘&&p<=‘9‘) { r=r*10+p-48; p=getchar(); } return r*f; } int main() { n=read(); for(int i=1;i<=n;i++) a[i]=read(); for(int i=2;i<=n;i++) { c[i]=a[i]-a[i-1]; if(c[i]>0) t1+=c[i]; else t2-=c[i]; } printf("%lld\n%lld\n",max(t1,t2),abs(t1-t2)+1); return 0; }
洛谷 P4552 [Poetize6] IncDec Sequence【差分+腦洞】