# 2018.10.03 NOIP+ 模拟赛 解题报告

### $$T1$$：奥义商店（点此看题面）

#include<bits/stdc++.h>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define uint unsigned int
#define LL long long
#define ull unsigned long long
#define swap(x,y) (x^=y,y^=x,x^=y)
#define abs(x) ((x)<0?-(x):(x))
#define INF 1e9
#define Inc(x,y) ((x+=y)>=MOD&&(x-=MOD))
#define N 100000
using namespace std;
int n,a[N+5];
class FIO
{
private:
#define Fsize 100000
#define tc() (FinNow==FinEnd&&(FinEnd=(FinNow=Fin)+fread(Fin,1,Fsize,stdin),FinNow==FinEnd)?EOF:*FinNow++)
#define pc(ch) (FoutSize<Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,FoutSize,stdout),Fout[(FoutSize=0)++]=ch))
int f,FoutSize,OutputTop;char ch,Fin[Fsize],*FinNow,*FinEnd,Fout[Fsize],OutputStack[Fsize];
public:
FIO() {FinNow=FinEnd=Fin;}
inline void read(int &x) {x=0,f=1;while(!isdigit(ch=tc())) f=ch^'-'?1:-1;while(x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));x*=f;}
inline void read_char(char &x) {while(isspace(x=tc()));}
inline void read_string(string &x) {x="";while(isspace(ch=tc()));while(x+=ch,!isspace(ch=tc())) if(!~ch) return;}
inline void write(int x) {if(!x) return (void)pc('0');if(x<0) pc('-'),x=-x;while(x) OutputStack[++OutputTop]=x%10+48,x/=10;while(OutputTop) pc(OutputStack[OutputTop]),--OutputTop;}
inline void write_char(char x) {pc(x);}
inline void write_string(string x) {register int i,len=x.length();for(i=0;i<len;++i) pc(x[i]);}
inline void end() {fwrite(Fout,1,FoutSize,stdout);}
}F;
class Key1//处理t=1的情况，与哈希冲突类似
{
private:
#define SqrtN 250
int s[N+5][SqrtN+5];
public:
inline void Init() {for(register int i=1,j;i<=n;++i) for(j=1;j<SqrtN;++j) s[j][i%j]+=a[i];}//初始化
inline void Update(int x,int y) {for(register int i=1;i<SqrtN;++i) s[i][x%i]+=y-a[x];}//修改操作
inline double GetAns(int x,int y)//询问操作
{
register int i,p,ans=0;F.read(p);//将多余的一个数读入
if(y<SqrtN) return s[y][x%y];//如果小于Sqrtn，直接返回预处理得到的值
for(i=x%y;i<=n;i+=y) ans+=a[i];//暴力求解，且时间复杂度肯定不大于O(√n)
return ans;//返回答案
}
}S1;
class Key2//处理t>1的情况，与暴力类似，但是要加上一个玄学的优化
{
public:
inline double GetAns(int x,int y,int z)
{
register int i,lim,Min=2e9,w;register double ans=0,mul=1;
for(i=1;i<=x;++i) F.read(w),Min=min(Min,w);//可以保证，给k染上数目最少的颜色肯定是最优的
//暴力求解，注意将操作次数与60取min
for(i=0,lim=min(min(Min,(n-y)/z),60);i<=lim;++i) ans+=mul*a[y+i*z],mul*=1.0*(Min-i)/(n-i-1);
for(i=1,lim=min(min(Min,(y-1)/z),60),mul=1.0*Min/(n-1);i<=lim;++i) ans+=mul*a[y-i*z],mul*=1.0*(Min-i)/(n-i-1);
return ans;//返回答案
}
}S2;
int main()
{
register int i,Q,op,x,y,z;
for(F.read(n),F.read(Q),i=1;i<=n;++i) F.read(a[i]);
for(S1.Init();Q;--Q)
{
F.read(op),F.read(x),F.read(y);
if(op>>1) F.read(z),printf("%.6lf\n",x>1?S2.GetAns(x,y,z):S1.GetAns(y,z));//询问操作，对t的大小进行分类讨论
else S1.Update(x,y),a[x]=y;//修改操作
}
return 0;
}


### $$T2$$：访问计划（点此看题面）

#include<bits/stdc++.h>
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)<(y)?(x):(y))
#define uint unsigned int
#define LL long long
#define ull unsigned long long
#define swap(x,y) (x^=y,y^=x,x^=y)
#define abs(x) ((x)<0?-(x):(x))
#define INF 1e9
#define Inc(x,y) ((x+=y)>=MOD&&(x-=MOD))
#define N 2000
#define LogN 15
#define add(x,y,z) (e[++ee].nxt=lnk[x],e[lnk[x]=ee].to=y,e[ee].val=z)
#define Dis(x,y) (dis[x]+dis[y]-dis[LCA(x,y)])
using namespace std;
int n,m,k,ee=0,lnk[N+5];
struct edge
{
int to,nxt,val;
}e[2*N+5];
class FIO
{
private:
#define Fsize 100000
#define tc() (FinNow==FinEnd&&(FinEnd=(FinNow=Fin)+fread(Fin,1,Fsize,stdin),FinNow==FinEnd)?EOF:*FinNow++)
#define pc(ch) (FoutSize<Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,FoutSize,stdout),Fout[(FoutSize=0)++]=ch))
int f,FoutSize,OutputTop;char ch,Fin[Fsize],*FinNow,*FinEnd,Fout[Fsize],OutputStack[Fsize];
public:
FIO() {FinNow=FinEnd=Fin;}
inline bool read(int &x) {x=0;while(!isdigit(ch=tc())) if(!~ch) return false;while(x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc())) if(!~ch) return false;return true;}
inline void read_char(char &x) {while(isspace(x=tc()));}
inline void read_string(string &x) {x="";while(isspace(ch=tc()));while(x+=ch,!isspace(ch=tc())) if(!~ch) return;}
inline void write(int x) {if(!x) return (void)pc('0');if(x<0) pc('-'),x=-x;while(x) OutputStack[++OutputTop]=x%10+48,x/=10;while(OutputTop) pc(OutputStack[OutputTop]),--OutputTop;}
inline void write_char(char x) {pc(x);}
inline void write_string(string x) {register int i,len=x.length();for(i=0;i<len;++i) pc(x[i]);}
inline void end() {fwrite(Fout,1,FoutSize,stdout);}
}F;
struct Class_TreeDP
{
private:
int res,MaxAns,lst[N+5],Son1[N+5],Son2[N+5],Max1[N+5],Max2[N+5];
inline void dfs(int x)//求最长链
{
register int i;
for(Max1[x]=Max2[x]=0,i=lnk[Son1[x]=Son2[x]=x];i;i=e[i].nxt)
{
if(!(i^lst[x])) continue;
lst[e[i].to]=((i-1)^1)+1,dfs(e[i].to);
if(Max1[e[i].to]+e[i].val>Max1[x]) Max2[x]=Max1[x],Max1[x]=Max1[e[i].to]+e[i].val,Son2[x]=Son1[x],Son1[x]=Son1[e[i].to];
else if(Max1[e[i].to]+e[i].val>Max2[x]) Max2[x]=Max1[e[i].to]+e[i].val,Son2[x]=Son1[e[i].to];
}
Max1[x]+Max2[x]>MaxAns&&(MaxAns=Max1[x]+Max2[x],res=x);
}
public:
inline int GetAns()
{
register int i;dfs(res=MaxAns=0);
//将最长链上边的边权全部改为原边权的相反数
for(i=Son1[res];i^res;i=e[lst[i]].to) e[lst[i]].val=e[(((lst[i])-1)^1)+1].val=-e[lst[i]].val;
for(i=Son2[res];i^res;i=e[lst[i]].to) e[lst[i]].val=e[(((lst[i])-1)^1)+1].val=-e[lst[i]].val;
return MaxAns;//返回答案
}
}TreeDP;
int main()
{
register int i,j,x,y,z,t,ans;
while(F.read(n),F.read(m),F.read(k))
{
for(i=ee=ans=0;i<=n;++i) lnk[i]=0;
for(i=1;i<n;++i) F.read(x),F.read(y),F.read(z),add(x,y,z),add(y,x,z),ans+=z;
for(ans<<=(i=1);i<=m;++i)
{
if((t=TreeDP.GetAns())<=k) break;//如果最长链的长度比要付出的代价还要少，就退出循环
ans-=t-k;//更新ans
}
F.write(ans),F.write_char('\n');
}
return F.end(),0;
}


### $$T3$$：模范学长（点此看题面）

posted @ 2018-10-28 18:12  TheLostWeak  阅读(169)  评论(0编辑  收藏  举报