[Contest] THUSC 2016 解题报告
2016-06-18 20:38
441 查看
T1
想到了区间dp 没想到再加两维
f[i][j][down][up] 表示在区间[i,j]取数 还剩下[down,up]中的数的最小代价
转移f[i][j][down][up]=min{初始值,f[i][k][down][up]+f[k+1][j][down][up]}
其中 初始值 为[i,j]中去掉头尾连续的都处于值区间的数后取完的最小代价
取完的代价g[i][j]=min{f[i][j][down][up]+去掉剩下的数的代价}
代码么 有三份
单峰
<span style="font-family:Microsoft YaHei;font-size:14px;color:#000066;">#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(ll &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline ll sqr(ll x){
return x*x;
}
const int N=55;
ll n,x
;
ll a,b;
ll f
;
int main()
{
freopen("t.in","r",stdin);
freopen("t1.out","w",stdout);
read(n); read(a); read(b);
for (int i=1;i<=n;i++) read(x[i]);
sort(x+1,x+n+1);
n=unique(x+1,x+n+1)-x-1;
memset(f,0x3f,sizeof(f));
f[0]=0;
for (int i=1;i<=n;i++)
for (int j=0;j<i;j++)
f[i]=min(f[i],f[j]+a+b*sqr(x[i]-x[j+1]));
printf("%lld\n",f
);
return 0;
}</span>
状压dp
<span style="font-family:Microsoft YaHei;font-size:14px;color:#000066;">#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline ll sqr(int x){
return x*x;
}
const int N=21;
int n,x
;
int a,b;
ll f[1<<N];
inline ll dp(int sta)
{
if (f[sta]<1e15) return f[sta];
int minv,maxv,cur;
for (int i=1;i<=n;i++)
{
if (sta&(1<<(i-1))) continue;
cur=sta; minv=1<<30; maxv=-1<<30;
for (int j=i;j<=n;j++)
{
if (sta&(1<<(j-1))) continue;
cur|=1<<(j-1); maxv=max(maxv,x[j]); minv=min(minv,x[j]);
f[sta]=min(f[sta],dp(cur)+a+sqr(maxv-minv)*b);
}
}
return f[sta];
}
int main()
{
freopen("t.in","r",stdin);
freopen("t2.out","w",stdout);
read(n); read(a); read(b);
for (int i=1;i<=n;i++) read(x[i]);
memset(f,0x3f,sizeof(f));
f[(1<<n)-1]=0;
printf("%lld\n",dp(0));
return 0;
}</span>
正解
<span style="font-family:Microsoft YaHei;font-size:14px;color:#000066;">#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline ll sqr(int x){
return x*x;
}
const int N=21;
int sx
,icnt;
inline int Bin(int x){
return lower_bound(sx+1,sx+icnt+1,x)-sx;
}
int n,x
;
int a,b;
ll ans,f
,g
;
int main()
{
int l,r,down,up,tl,tr,maxv,minv;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(a); read(b);
for (int i=1;i<=n;i++) read(x[i]),sx[++icnt]=x[i];
sort(sx+1,sx+icnt+1); icnt=unique(sx+1,sx+icnt+1)-sx-1;
for (int i=1;i<=n;i++) x[i]=Bin(x[i]);
for (int i=1;i<=n;i++)
for (int j=1;j+i-1<=n;j++)
{
l=j,r=j+i-1;
maxv=-1<<30,minv=1<<30;
for (int k=l;k<=r;k++) maxv=max(maxv,x[k]),minv=min(minv,x[k]);
g[l][r]=a+sqr(sx[maxv]-sx[minv])*b;
for (down=1;down<=icnt;down++)
for (up=down;up<=icnt;up++)
{
if (up<minv || down>maxv) continue;
tl=l-1,tr=r+1;
for (int k=l;k<=r;k++) if (down<=x[k] && x[k]<=up) tl=k; else break;
for (int k=r;k>=l;k--) if (down<=x[k] && x[k]<=up) tr=k; else break;
if (tl>=tr)
f[l][r][down][up]=0;
else
f[l][r][down][up]=g[tl+1][tr-1];
for (int k=l;k<r;k++)
f[l][r][down][up]=min(f[l][r][down][up],f[l][k][down][up]+f[k+1][r][down][up]);
g[l][r]=min(g[l][r],f[l][r][down][up]+a+sqr(sx[up]-sx[down])*b);
}
for (down=maxv+1;down<=icnt;down++)
for (up=down;up<=icnt;up++)
f[l][r][down][up]=g[l][r];
for (up=1;up<minv;up++)
for (down=1;down<=up;down++)
f[l][r][down][up]=g[l][r];
}
printf("%lld\n",g[1]
);
return 0;
}</span>
T2
Trie套vector 自己都不知道对不对
T3
前两个点就是环 第三个点线段很短 可以当做点 其他么 贪心啊 模拟退火啊 随机排列啊 取个较优的吧 根本没想过把线段割开
想到了区间dp 没想到再加两维
f[i][j][down][up] 表示在区间[i,j]取数 还剩下[down,up]中的数的最小代价
转移f[i][j][down][up]=min{初始值,f[i][k][down][up]+f[k+1][j][down][up]}
其中 初始值 为[i,j]中去掉头尾连续的都处于值区间的数后取完的最小代价
取完的代价g[i][j]=min{f[i][j][down][up]+去掉剩下的数的代价}
代码么 有三份
单峰
<span style="font-family:Microsoft YaHei;font-size:14px;color:#000066;">#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(ll &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline ll sqr(ll x){
return x*x;
}
const int N=55;
ll n,x
;
ll a,b;
ll f
;
int main()
{
freopen("t.in","r",stdin);
freopen("t1.out","w",stdout);
read(n); read(a); read(b);
for (int i=1;i<=n;i++) read(x[i]);
sort(x+1,x+n+1);
n=unique(x+1,x+n+1)-x-1;
memset(f,0x3f,sizeof(f));
f[0]=0;
for (int i=1;i<=n;i++)
for (int j=0;j<i;j++)
f[i]=min(f[i],f[j]+a+b*sqr(x[i]-x[j+1]));
printf("%lld\n",f
);
return 0;
}</span>
状压dp
<span style="font-family:Microsoft YaHei;font-size:14px;color:#000066;">#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline ll sqr(int x){
return x*x;
}
const int N=21;
int n,x
;
int a,b;
ll f[1<<N];
inline ll dp(int sta)
{
if (f[sta]<1e15) return f[sta];
int minv,maxv,cur;
for (int i=1;i<=n;i++)
{
if (sta&(1<<(i-1))) continue;
cur=sta; minv=1<<30; maxv=-1<<30;
for (int j=i;j<=n;j++)
{
if (sta&(1<<(j-1))) continue;
cur|=1<<(j-1); maxv=max(maxv,x[j]); minv=min(minv,x[j]);
f[sta]=min(f[sta],dp(cur)+a+sqr(maxv-minv)*b);
}
}
return f[sta];
}
int main()
{
freopen("t.in","r",stdin);
freopen("t2.out","w",stdout);
read(n); read(a); read(b);
for (int i=1;i<=n;i++) read(x[i]);
memset(f,0x3f,sizeof(f));
f[(1<<n)-1]=0;
printf("%lld\n",dp(0));
return 0;
}</span>
正解
<span style="font-family:Microsoft YaHei;font-size:14px;color:#000066;">#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
inline ll sqr(int x){
return x*x;
}
const int N=21;
int sx
,icnt;
inline int Bin(int x){
return lower_bound(sx+1,sx+icnt+1,x)-sx;
}
int n,x
;
int a,b;
ll ans,f
,g
;
int main()
{
int l,r,down,up,tl,tr,maxv,minv;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(n); read(a); read(b);
for (int i=1;i<=n;i++) read(x[i]),sx[++icnt]=x[i];
sort(sx+1,sx+icnt+1); icnt=unique(sx+1,sx+icnt+1)-sx-1;
for (int i=1;i<=n;i++) x[i]=Bin(x[i]);
for (int i=1;i<=n;i++)
for (int j=1;j+i-1<=n;j++)
{
l=j,r=j+i-1;
maxv=-1<<30,minv=1<<30;
for (int k=l;k<=r;k++) maxv=max(maxv,x[k]),minv=min(minv,x[k]);
g[l][r]=a+sqr(sx[maxv]-sx[minv])*b;
for (down=1;down<=icnt;down++)
for (up=down;up<=icnt;up++)
{
if (up<minv || down>maxv) continue;
tl=l-1,tr=r+1;
for (int k=l;k<=r;k++) if (down<=x[k] && x[k]<=up) tl=k; else break;
for (int k=r;k>=l;k--) if (down<=x[k] && x[k]<=up) tr=k; else break;
if (tl>=tr)
f[l][r][down][up]=0;
else
f[l][r][down][up]=g[tl+1][tr-1];
for (int k=l;k<r;k++)
f[l][r][down][up]=min(f[l][r][down][up],f[l][k][down][up]+f[k+1][r][down][up]);
g[l][r]=min(g[l][r],f[l][r][down][up]+a+sqr(sx[up]-sx[down])*b);
}
for (down=maxv+1;down<=icnt;down++)
for (up=down;up<=icnt;up++)
f[l][r][down][up]=g[l][r];
for (up=1;up<minv;up++)
for (down=1;down<=up;down++)
f[l][r][down][up]=g[l][r];
}
printf("%lld\n",g[1]
);
return 0;
}</span>
T2
Trie套vector 自己都不知道对不对
T3
前两个点就是环 第三个点线段很短 可以当做点 其他么 贪心啊 模拟退火啊 随机排列啊 取个较优的吧 根本没想过把线段割开
相关文章推荐
- Eric.Wong主讲软件测试精华
- iOS关于程序被强杀调用的方法
- JQuery实战中体验学习的快乐
- linux编译C++的cmake文件(CMakeList.txt)
- jQuery设置和修改HTML
- 【扯】2016618
- Java函数的定义
- 通过了装饰器contextmanager实现的上下文管理器,
- linux系统如何安装vmware Tools(下面以CentOS为例)
- php批量sql删除
- Fragment重叠问题引发的思考
- 机器调度
- JavaSE入门学习50:多线程编程(一)
- python之数据类型(一)
- 工程规划(答案错误)
- 关于广播无法启动的一个小问题
- ICML论文|这违反直觉的“升噪”方法,反而能很好的解决激活函数梯度弥散的问题
- Leetcode no. 107
- java线程池基础知识
- Labview实现AM调制