标签:main problem 模拟退火 hup 数字 data efi 线段 天才
#include<bits/stdc++.h>
#define ll long long
#define N 1000010
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
ll sum[N],q[N];
ll n,ans=-2147483647,A,B;
int main()
{
//freopen("input.txt","r",stdin);
n=read(),A=read(),B=read();
rep(i,1,n)
sum[i]=sum[i-1]+read();
int head=0,tail=1;
rep(i,A,n)
{
while(head<=tail && q[head]<i-B)//不处于维护范围内的,出队
head++;
while(head<=tail && sum[i-A]<=sum[q[tail]])//更优的sum[l - 1]予以插队
tail--;
q[++tail]=i-A;
ans=max(ans,sum[i]-sum[q[head]]);//更新答案
}
printf("%lld\n",ans);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int maxn=500000+5;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
ll s[maxn];
int n,a,b;
ll mi[maxn<<2];
void pushup(int rt)
{
mi[rt]=min(mi[rt<<1],mi[rt<<1|1]);
}
void buildtree(int l,int r,int rt)
{
if(l==r)
{
mi[rt]=s[l];
return;
}
int m=(l+r)>>1;
buildtree(lson);
buildtree(rson);
pushup(rt);
}
ll query(int L,int R,int l,int r,int rt)
{
//printf("%d %d,%d %d %d\n",L,R,l,r,rt);
if(R<=0)return 0;
if(R<l||L>r) return 1e15;
if (L<=l&& R>=r)
{
return mi[rt];
}
int m=(l+r)>>1;
ll ans=1e15;
if(L<=m)ans=min(ans,query(L,R,lson));
if(R>m)ans=min(ans,query(L,R,rson));
return ans;
}
ll read()
{
ll x=0,f=1;char s=getchar();
while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
return x*f;
}
void init()
{
freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
}
void readdata()
{
n=read();a=read();b=read();
s[0]=0;
for(int i=1;i<=n;i++)
{
s[i]=read()+s[i-1];
}
buildtree(0,n,1);
}
void work()
{
ll ans=-1e15;
for (int i=a;i<=n;i++)
{
ans=max(ans,s[i]-query(max(0,i-b),max(0,i-a),0,n,1));
}
printf("%lld",ans);
}
int main()
{
//init();
readdata();
work();
return 0;
}
/*
1.random_shuffle 随机大法好!
2.状压基本都可以靠随机和模拟退火水过去……还是100分的那种……
*/
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
int n,m,ans,cnt,sum;
int w[20];
int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int main()
{
n=read(),m=read();
ans=n;//最坏情况
rep(i,1,n)
w[i]=read();
rep(times,1,1000000)
{
random_shuffle(w+1,w+n+1);
sum=0,cnt=0;
rep(i,1,n)
{
sum+=w[i];
if(sum>m)
{
++cnt;
sum=w[i];
}
}
if(sum)++cnt;
ans=min(ans,cnt);
}
printf("%d",ans);
return 0;
}
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dwn(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
int n,m,ans;
long long w[20],sum[20];
bool vis[20];
int read()
{
int x=0,f=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int main()
{
//freopen("input.txt","r",stdin);
n=read(),m=read();
rep(i,1,n)
w[i]=read();
sort(w+1,w+n+1);
dwn(i,n,1)//精髓
{
if(!vis[i])
{
sum[i]=w[i];
dwn(j,i-1,1)//精髓
{
if(!vis[j]&&sum[i]+w[j]<=m)
{
vis[j]=1;
sum[i]+=w[j];
}
}
}
}
rep(i,1,n)
if(sum[i])
ans++;
printf("%d",ans);
return 0;
}
标签:main problem 模拟退火 hup 数字 data efi 线段 天才
原文地址:https://www.cnblogs.com/sjsjsj-minus-Si/p/11635638.html