CCPC-Wannafly Winter Camp Day1部分题目解析

F 爬爬爬山

CODE

#include<cstdio>
#include<cctype>
#include<queue>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
typedef long long LL;
const int N=100005;
const LL INF=1e18;
struct edge
{
int to,nxt; LL v;
class FileInputOutput
{
private:
static const int S=1<<21;
char Fin[S],*A,*B;
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
}
#undef tc
}F;
class SSSP
{
private:
struct data
{
int id; LL val;
inline data(CI Id=0,const LL& Val=0)
{
id=Id; val=Val;
}
inline friend bool operator < (const data& A,const data &B)
{
return A.val>B.val;
}
}; priority_queue <data> small; int all; bool vis[N<<1]; LL dis[N<<1];
public:
#define to e[i].to
inline void Dijkstra(void)
{
RI i; for (i=2,all=n<<1;i<=all;++i) dis[i]=INF;
small.push(data(1,0)); while (!small.empty())
{
int now=small.top().id; small.pop();
if (vis[now]) continue; vis[now]=1;
if (!vis[to]&&dis[to]>dis[now]+e[i].v)
small.push(data(to,dis[to]=dis[now]+e[i].v));
}
printf("%lld",dis[all]);
}
#undef to
}G;
inline void add(CI x,CI y,const LL& z)
{
}
inline LL sqr(CI x)
{
return x<0?0:1LL*x*x;
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
return G.Dijkstra(),0;
}

J 夺宝奇兵

#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
typedef long long LL;
const int N=100005,S=1e9;
int n,m,x,y,mx,cur,lim; LL ans=1e18,ret; vector <int> v[N],p[N];
class FileInputOutput
{
private:
static const int S=1<<21;
char Fin[S],*A,*B;
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
}
#undef tc
}F;
class Segment_Tree
{
private:
static const int P=30;
struct Segment
{
int ch[2],size; LL sum;
}node[N*P]; int tot;
public:
int rt;
#define lc(x) node[x].ch[0]
#define rc(x) node[x].ch[1]
#define Si(x) node[x].size
#define S(x) node[x].sum
inline void modify(int& now,CI val,CI opt,CI l=1,CI r=S)
{
if (!now) now=++tot; Si(now)+=opt; S(now)+=opt*val; if (l==r) return; int mid=l+r>>1;
if (val<=mid) modify(lc(now),val,opt,l,mid); else modify(rc(now),val,opt,mid+1,r);
}
inline LL query(CI now,CI rk,CI l=1,CI r=S)
{
if (!now) return 0; if (l==r) return 1LL*l*rk; int mid=l+r>>1;
return rk<=Si(lc(now))?query(lc(now),rk,l,mid):S(lc(now))+query(rc(now),rk-Si(lc(now)),mid+1,r);
}
#undef lc
#undef rc
#undef Si
#undef S
}T;
inline bool cmp(CI x,CI y)
{
return x>y;
}
inline void maxer(int& x,int y)
{
if (y>x) x=y;
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
for (i=1;i<=n;++i) sort(v[i].begin(),v[i].end(),cmp),maxer(mx,v[i].size());
for (i=1;i<=n;++i) for (lim=v[i].size(),j=0;j<lim;++j)
p[j+1].push_back(v[i][j]); for (i=mx+1;~i;--i)
{
for (lim=p[i].size(),cur+=lim,j=0;j<lim;++j)
T.modify(T.rt,p[i][j],-1),ret+=p[i][j]; LL cost;
if (cur>=i) cost=ret; else cost=ret+T.query(T.rt,i-cur);
if (cost<ans) ans=cost;
}
return printf("%lld",ans),0;
}

C 拆拆拆数

#include<cstdio>
#include<cctype>
#define int long long
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
const int prime[15]={3,5,7,11,13,17,19,23,29,31,37,41,43,47,51};
int t,x,y;
class FileInputOutput
{
private:
static const int S=1<<21;
#define pc(ch) (Ftop<S?Fout[Ftop++]=ch:(fwrite(Fout,1,S,stdout),Fout[(Ftop=0)++]=ch))
char Fin[S],Fout[S],*A,*B; int Ftop,pt[25];
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
}
Tp inline void write(T x,const char& ch)
{
if (!x) return (void)(pc('0'),pc(ch)); RI ptop=0;
while (x) pt[++ptop]=x%10,x/=10; while (ptop) pc(pt[ptop--]+48); pc(ch);
}
inline void Fend(void)
{
fwrite(Fout,1,Ftop,stdout);
}
#undef tc
#undef pc
}F;
inline int gcd(CI x,CI y)
{
return y?gcd(y,x%y):x;
}
signed main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
{
if (gcd(x,y)==1)
{
F.write(1,'\n'); F.write(x,' ');
F.write(y,'\n'); continue;
}
F.write(2,'\n'); if ((x&1)&&(y&1))
{
F.write(2,' '); F.write(y-2,'\n');
F.write(x-2,' '); F.write(2,'\n'); continue;
}
bool flag=0; if (x&1) { int t=x; x=y; y=t; flag=1; }
for (RI i=0;i<15;++i) if (gcd(prime[i],y-2)==1)
{
if (flag)
{
F.write(y-2,' '); F.write(prime[i],'\n');
F.write(2,' '); F.write(x-prime[i],'\n'); break;
} else
{
F.write(prime[i],' '); F.write(y-2,'\n');
F.write(x-prime[i],' '); F.write(2,'\n'); break;
}
}
}
return F.Fend(),0;
}

E 流流流动

CODE

#include<cstdio>
#define RI register int
#define CI const int&
using namespace std;
const int N=105;
struct edge
{
int to,nxt;
{
}
inline int min(CI a,CI b)
{
return a<b?a:b;
}
inline int max(CI a,CI b)
{
return a>b?a:b;
}
#define to e[i].to
inline void DP(CI now,CI fa)
{
f[now][0]=0; f[now][1]=a[now]; vis[now]=1;
{
DP(to,now); f[now][0]+=max(f[to][0],f[to][1]);
f[now][1]+=max(f[to][0],f[to][1]-b[min(now,to)]);
}
}
#undef to
int main()
{
RI i; for (scanf("%d",&n),i=1;i<=n;++i) scanf("%d",&a[i]);
for (i=1;i<=n;++i) scanf("%d",&b[i]); for (i=2;i<=n;++i)
for (i=1;i<=n;++i) if (!vis[i]) DP(i,0),ans+=max(f[i][0],f[i][1]);
return printf("%d",ans),0;
}

I 起起落落

$f_i=\sum_{j<i} [p_j>p_i](f_j+1)\sum_{j<k<i} [p_k<p_i]$

CODE

#include<cstdio>
#include<cctype>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
const int N=100005,mod=1e9+7;
int n,x,ans,cur;
class FileInputOutput
{
private:
static const int S=1<<21;
char Fin[S],*A,*B;
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
}
#undef tc
}F;
inline void inc(int& x,CI y)
{
if ((x+=y)>=mod) x-=mod;
}
inline int sum(CI x,CI y)
{
int t=x+y; return t>=mod?t-mod:t;
}
class Segment_Tree
{
private:
struct Segment
{
int base,val,tag;
}node[N<<2];
#define B(x) node[x].base
#define V(x) node[x].val
#define T(x) node[x].tag
inline void pushup(CI now)
{
B(now)=sum(B(now<<1),B(now<<1|1)); V(now)=sum(V(now<<1),V(now<<1|1));
}
inline void pushdown(CI now)
{
if (!T(now)) return; inc(T(now<<1),T(now)); inc(V(now<<1),1LL*T(now)*B(now<<1)%mod);
inc(T(now<<1|1),T(now)); inc(V(now<<1|1),1LL*T(now)*B(now<<1|1)%mod); T(now)=0;
}
public:
#define TN CI now=1,CI l=1,CI r=n
#define O beg,end
inline int queryval(CI beg,CI end,TN)
{
if (beg<=l&&r<=end) return V(now); int mid=l+r>>1,ret=0; pushdown(now);
if (beg<=mid) inc(ret,queryval(O,now<<1,l,mid));
if (end>mid) inc(ret,queryval(O,now<<1|1,mid+1,r)); return ret;
}
inline int querybase(CI beg,CI end,TN)
{
if (beg<=l&&r<=end) return B(now); int mid=l+r>>1,ret=0; pushdown(now);
if (beg<=mid) inc(ret,querybase(O,now<<1,l,mid));
if (end>mid) inc(ret,querybase(O,now<<1|1,mid+1,r)); return ret;
}
inline void modify(CI beg,CI end,CI val,TN)
{
if (beg<=l&&r<=end) return (void)(inc(T(now),val),inc(V(now),1LL*val*B(now)%mod));
int mid=l+r>>1; pushdown(now); if (beg<=mid) modify(O,val,now<<1,l,mid);
if (end>mid) modify(O,val,now<<1|1,mid+1,r); pushup(now);
}
#undef O
inline void updata(CI pos,CI mb,CI mv,TN)
{
if (l==r) return (void)(inc(B(now),mb),inc(V(now),mv)); int mid=l+r>>1; pushdown(now);
if (pos<=mid) updata(pos,mb,mv,now<<1,l,mid); else updata(pos,mb,mv,now<<1|1,mid+1,r); pushup(now);
}
#undef TN
#undef B
#undef V
#undef T
}T;
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
{
T.updata(x,cur+1,sum(-T.querybase(x+1,n),mod));
}
return printf("%d",ans),0;
}

A 机器人

CODE

#include<cstdio>
#include<cctype>
#include<algorithm>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
const int N=100005;
int n,r,m,k,s,p[2][N],cnt[2],sp[N],cnt_sp,x,y,mi=1e9,mx,pmi,pmx;
class FileInputOutput
{
private:
static const int S=1<<21;
char Fin[S],*A,*B;
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
}
#undef tc
}F;
inline void miner(int& x,CI y)
{
if (y<x) x=y;
}
inline void maxer(int& x,CI y)
{
if (y>x) x=y;
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
sort(sp+1,sp+cnt_sp+1); cnt_sp=unique(sp+1,sp+cnt_sp+1)-sp-1;
sort(p[0]+1,p[0]+cnt[0]+1); sort(p[1]+1,p[1]+cnt[1]+1);
if (cnt[0]) miner(mi,p[0][1]),maxer(mx,p[0][cnt[0]]);
if (cnt[1]) miner(mi,p[1][1]),maxer(mx,p[1][cnt[1]]);
pmx=lower_bound(sp+1,sp+cnt_sp+1,mx)-sp; pmx=sp[pmx];
pmi=upper_bound(sp+1,sp+cnt_sp+1,mi)-sp-1; pmi=sp[pmi];
if (!cnt[1])
{
if (mx<=s) pmx=s; if (mi>=s) pmi=s;
return printf("%d",pmx-pmi<<1),0;
}
int cur=(pmx-pmi<<1)+(k<<1);
if (s<pmi) cur+=(pmi-s<<1); if (s>pmx) cur+=(s-pmx<<1);
return printf("%d",cur),0;
}

B 吃豆豆（only for Div2）

Div2的话由于$$c\le 1018$$，所以我们直接大力DP，以颜色为状态转移感觉不是很好策，所以我们以时间为状态

$$f_{i,j,k}$$表示时间$$k$$$$i,j$$最多能得到多少糖果，转移的话分四个方向移动和不动转移一下即可

CODE

#include<cstdio>
#include<cctype>
#include<cstring>
#define RI register int
#define CI const int&
#define Tp template <typename T>
using namespace std;
const int N=15,C=1100;
int n,m,c,t[N][N],f[N][N][N*C],sx,sy,tx,ty,ans;
class FileInputOutput
{
private:
static const int S=1<<21;
char Fin[S],*A,*B;
public:
{
x=0; char ch; while (!isdigit(ch=tc()));
while (x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));
}
#undef tc
}F;
inline void maxer(int& x,CI y)
{
if (y>x) x=y;
}
inline void miner(int& x,CI y)
{
if (y<x) x=y;
}
int main()
{
//freopen("CODE.in","r",stdin); freopen("CODE.out","w",stdout);
for (f[sx][sy][0]=0,k=0;k<=10*c;++k) for (i=1;i<=n;++i) for (j=1;j<=m;++j)
{
if (i-1>=1) maxer(f[i-1][j][k+1],f[i][j][k]+((k+1)%t[i-1][j]?0:1));
if (i+1<=n) maxer(f[i+1][j][k+1],f[i][j][k]+((k+1)%t[i+1][j]?0:1));
if (j-1>=1) maxer(f[i][j-1][k+1],f[i][j][k]+((k+1)%t[i][j-1]?0:1));
if (j+1<=m) maxer(f[i][j+1][k+1],f[i][j][k]+((k+1)%t[i][j+1]?0:1));
maxer(f[i][j][k+1],f[i][j][k]+((k+1)%t[i][j]?0:1));
}
for (k=0;k<=10*c;++k) if (f[tx][ty][k]>=c) { ans=k; break; }
return printf("%d",ans),0;
}