T1 最大子序列的和
据芸哥的评价:“这是暴得不能更暴的暴力了,你不超时谁超时”
我是真的菜QAQ单调队列写了一半被标签上的线段树骗到 我的心路历程:“咦线段树!我不会诶,可以写树状数组嘛!呀超时怎么办 不怕不怕,怎么样都能过一个点的”,然后,呵呵,你一个点都不给我QAQ!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 id:gww 3 language: 4 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 5 */ 6 #include<bits/stdc++.h> 7 #define ll long long 8 using namespace std; 9 const int N=500000+10; 10 const int inf=0x3f3f3f3f; 11 int n,a,b,k[N],c[N],ans=-inf; 12 int rd() 13 { 14 int x=0,w=0;char ch=0; 15 while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} 16 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 17 return w?-x:x; 18 } 19 20 int lowbit(int x) 21 { 22 return x&(-x); 23 } 24 25 void update(int x,int y) 26 { 27 while(x<=n) 28 { 29 c[x]+=y; 30 x+=lowbit(x); 31 } 32 } 33 34 int query(int x) 35 { 36 int ans=0; 37 while(x>0) 38 { 39 ans+=c[x]; 40 x-=lowbit(x); 41 } 42 return ans; 43 } 44 45 int main() 46 { 47 n=rd(),a=rd(),b=rd(); 48 for(int i=1;i<=n;i++) 49 {update(i,rd());} 50 for(int i=b;i<=n;i++) 51 for(int j=i;j>=i-a;j--) 52 { 53 ans=max(ans,query(i)-query(j-1)); 54 } 55 printf("%d",ans); 56 return 0; 57 }
单调队列写出来都长一个样子QAQ
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /* 2 id:gww 3 language: 4 啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 5 */ 6 #include<bits/stdc++.h> 7 #define ll long long 8 using namespace std; 9 const int N=500000+10; 10 const int inf=0x3f3f3f3f; 11 int n,a,b,k[N],q[N],ans=-inf; 12 int rd() 13 { 14 int x=0,w=0;char ch=0; 15 while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} 16 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 17 return w?-x:x; 18 } 19 20 int main() 21 { 22 n=rd(),a=rd(),b=rd(); 23 for(int i=1;i<=n;i++) 24 {k[i]=rd();k[i]+=k[i-1];} 25 int tail=1,head=0; 26 for(int i=a;i<=n;i++) 27 { 28 while(head<=tail&&q[head]<i-b) head++;//不超过b 29 while(head<=tail&&k[q[tail]]>=k[i-a]) tail--;//不小于a 且判断sei更优秀 30 q[++tail]=i-a; 31 ans=max(ans,k[i]-k[q[head]]); 32 } 33 printf("%d",ans); 34 return 0; 35 }
线段树 我为什么总是有一些玄学错误!!!!
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=500000+5; 4 #define lson l,m,rt<<1 5 #define rson m+1,r,rt<<1|1 6 7 typedef long long ll; 8 9 ll s[maxn]; 10 int n,a,b; 11 ll mi[maxn<<2]; 12 13 void pushup(int rt) 14 { 15 mi[rt]=min(mi[rt<<1],mi[rt<<1|1]); 16 } 17 18 void buildtree(int l,int r,int rt) 19 { 20 if(l==r) 21 { 22 mi[rt]=s[l]; 23 return; 24 } 25 int m=(l+r)>>1; 26 buildtree(lson); 27 buildtree(rson); 28 pushup(rt); 29 } 30 31 ll query(int L,int R,int l,int r,int rt) 32 { 33 //printf("%d %d,%d %d %d ",L,R,l,r,rt); 34 if(R<=0)return 0; 35 if(R<l||L>r) return 1e15; 36 if (L<=l&& R>=r) 37 { 38 return mi[rt]; 39 } 40 int m=(l+r)>>1; 41 ll ans=1e15; 42 if(L<=m)ans=min(ans,query(L,R,lson)); 43 if(R>m)ans=min(ans,query(L,R,rson)); 44 return ans; 45 } 46 47 ll read() 48 { 49 ll x=0,f=1;char s=getchar(); 50 while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();} 51 while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();} 52 return x*f; 53 } 54 55 void init() 56 { 57 freopen("input.txt","r",stdin); 58 //freopen("output.txt","w",stdout); 59 } 60 61 void readdata() 62 { 63 n=read();a=read();b=read(); 64 s[0]=0; 65 for(int i=1;i<=n;i++) 66 { 67 s[i]=read()+s[i-1]; 68 } 69 buildtree(0,n,1); 70 } 71 72 void work() 73 { 74 ll ans=-1e15; 75 for (int i=a;i<=n;i++) 76 { 77 ans=max(ans,s[i]-query(max(0,i-b),max(0,i-a),0,n,1)); 78 } 79 printf("%lld",ans); 80 } 81 82 int main() 83 { 84 //init(); 85 readdata(); 86 work(); 87 return 0; 88 }
T3 数的划分
让各个大佬来说就是,这个数据太水了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /*id:gwwlanguage:啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 */ 2 #include<bits/stdc++.h> 3 #define ll long long 4 using namespace std; 5 const int N=1000000+10; 6 int n,m,c[N],a[20],ans=9999999,sum,cnt; 7 int rd() 8 { 9 int x=0,w=0;char ch=0; 10 while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} 11 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 12 return w?-x:x; 13 } 14 int main() 15 { 16 n=rd(),m=rd(); 17 for(int i=1;i<=n;i++) 18 a[i]=rd(); 19 for(int i=1;i<=1000000;i++) 20 { 21 random_shuffle(a+1,a+n+1); 22 sum=0,cnt=0; 23 for(int i=1;i<=n;i++) 24 { 25 sum+=a[i]; 26 if(sum>m) 27 { 28 cnt++; 29 sum=a[i]; 30 } 31 } 32 if(sum) cnt++; 33 ans=min(ans,cnt); 34 } 35 printf("%d",ans); 36 return 0; 37 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /*id:gwwlanguage:啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊 */ 2 #include<bits/stdc++.h> 3 #define ll long long 4 using namespace std; 5 const int N=20; 6 const int M=1000000000+10; 7 int n,m,a[N],ans=0,sum[N]; 8 bool vis[N]; 9 int rd() 10 { 11 int x=0,w=0;char ch=0; 12 while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} 13 while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar(); 14 return w?-x:x; 15 } 16 int main() 17 { 18 memset(vis,0,sizeof(vis)); 19 n=rd(),m=rd(); 20 for(int i=1;i<=n;i++) 21 a[i]=rd(); 22 sort(a+1,a+1+n); 23 for(int i=n;i>=1;i--)//从小到大排序 故需要倒序枚举 24 { 25 if(!vis[i]) 26 { 27 sum[i]=a[i]; 28 for(int j=i-1;j>=1;j--)//枚举比i小的 29 if(!vis[j]&&sum[i]+a[j]<=m) 30 {vis[j]=1;sum[i]+=a[j];} 31 } 32 } 33 for(int i=1;i<=n;i++) 34 if(sum[i]) ans++; 35 printf("%d",ans); 36 return 0; 37 }
我拒绝模拟退火
我可不可以也把正解拒绝了