@KirinBill
2017-10-08T19:29:23.000000Z
字数 3558
阅读 1166
题解
套题
题简单也要静下心来做...
目录
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <algorithm>
#include <cstring>
using std::min;
const int MAXN=505;
int n,m;
int a[MAXN];
inline long long DP(){
static long long dp[MAXN][MAXN];
for(int len=2;len<=m;++len){
for(int l=2,r,lim=m-len+1;l<=lim;++l){
r=l+len-1;
dp[l][r]=dp[l+1][r]+(long long)a[l-1]*a[l]*a[r];
for(int mid=l+1;mid<r;++mid)
dp[l][r]=min(dp[l][r],dp[l][mid]+dp[mid+1][r]+(long long)a[l-1]*a[mid]*a[r]);
}
}
return dp[2][m];
}
int main(){
setIO("A");
read(n);
read(a[++m]),read(a[++m]);
for(int i=2,tmp;i<=n;++i)
read(tmp),read(a[++m]);
write(DP());
return 0;
}
#include <cstdio>
#include <cctype>
#include <string>
using std::string;
inline void setIO(string file){
string in=file+".in",out=file+".out";
freopen(in.c_str(),"r",stdin);
freopen(out.c_str(),"w",stdout);
}
template<typename type>
inline void read(type &x){
int pm=1; char c;
do{
c=getchar();
if(c=='-') pm=-1;
}while(!isdigit(c));
x=c^'0';
while(c=getchar(),isdigit(c))
x=x*10+(c^'0');
x*=pm;
}
template<typename type>
void write(type x,char c=0){
if(x<0) putchar('-'),x=-x;
if(x>9) write(x/10);
putchar(x%10|'0');
if(c) putchar(c);
}
#include <algorithm>
using std::max;
const int MAXN=5e5+5;
int n,rt;
int he[MAXN];
long long ans;
long long dson[MAXN];
struct line{int to,nex,w;}ed[MAXN<<1];
inline void addE(int u,int v,int w){
static int cnt=0;
ed[++cnt]=(line){v,he[u],w};
he[u]=cnt;
}
void DFS(int u,int fa,long long d){
for(int i=he[u],v;i;i=ed[i].nex){
v=ed[i].to;
if(v!=fa){
DFS(v,u,d+ed[i].w);
dson[u]=max(dson[u],dson[v]+ed[i].w);
}
}
for(int i=he[u],v;i;i=ed[i].nex){
v=ed[i].to;
if(v!=fa) ans+=dson[u]-dson[v]-ed[i].w;
}
}
int main(){
setIO("B");
read(n),read(rt);
for(int i=1,u,v,w;i<n;++i){
read(u),read(v),read(w);
addE(u,v,w),addE(v,u,w);
}
DFS(rt,0,0);
write(ans);
return 0;
}
#include <cstdio>
#include <algorithm>
#include <climits>
#include <cstring>
using std::max;
const int MAXN=10005,MAXSTA=1<<10;
int n,k,a,b,sum;
int cnt[MAXSTA],grape[MAXN];
inline void prepare(){
for(int i=1,lim=1<<k;i<lim;++i)
cnt[i]=cnt[i>>1]+(i&1);
}
inline int DP(){
static int dp[2][MAXSTA];
memset(dp,-0x3f,sizeof(dp));
for(int i=0,lim=1<<k,tmp,j;i<lim;++i){
if(cnt[i]<a || b<cnt[i]) continue;
dp[k&1][i]=0;
tmp=i,j=1;
while(tmp){
if(tmp&1) dp[k&1][i]+=grape[j];
tmp>>=1,++j;
}
}
for(int i=k+1,lim=1<<k,mod=lim-1,top=lim>>1;i<=n;++i){
for(int j=0,tmp,l;j<lim;++j){
if(cnt[j]<a || b<cnt[j]) continue;
dp[i&1][j]=max(dp[~i&1][j<<1&mod],dp[~i&1][j<<1&mod|1])+grape[i]*(bool)(j&top);
}
}
int ret=INT_MIN;
for(int i=0,lim=1<<k;i<lim;++i){
if(a<=cnt[i] && cnt[i]<=b)
ret=max(ret,dp[n&1][i]);
}
return ret;
}
int main(){
freopen("C.in","r",stdin);
freopen("C.out","w",stdout);
scanf("%d%d%d%d",&n,&k,&a,&b);
for(int i=1;i<=n;++i){
scanf("%d",&grape[i]);
sum+=grape[i];
}
prepare();
printf("%d",(DP()<<1)-sum);
return 0;
}