# 6.27联考题解

A：

$f\left[i\right]\left[j\right]=f\left[i-1\right]\left[k\right]+sin\left(ra{d}_{j}-ra{d}_{k}\right)$$f[i][j]=f[i-1][k]+sin(rad_j-rad_k)$

code：

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
#define ld long double
#define fir first
#define sec second
#define inf 1e7
using namespace std;

const int maxn = 2100;
const ld pi  = acos(-1);

int n,K;
pair<ld,int> ang[maxn];
struct point
{
int i;
ld x,y,ang;
friend inline point operator -(const point &x,const point &y){return (point){x.i,x.x-y.x,x.y-y.y};}
friend inline ld operator *(const point &x,const point &y){return x.x*y.y-x.y*y.x;}
}zero,p[maxn];
int R[maxn];
ld _sin[maxn][maxn];

struct data
{
ld f; int pf,k;
}f[maxn][maxn];
int ans[maxn],ansn; ld ansnow;

{
const ld eps = 1e-9;
int ST[maxn],stn;
void dp()
{
ansnow=-inf; ansn=0;
for(int i=1;i<=10&&i+K-1<=n;i++)
{
f[2][i].pf=0;
for(int j=i+1;j<=R[i];j++) f[2][j]=(data){_sin[i][j],i};
for(int j=R[i]+1;j<=n;j++) f[2][j].f=-1;

for(int k=3;k<=K;k++)
{
for(int j=i+1;j<=n;j++)
{

{
f[k][j].f=f[k-1][l].f+_sin[l][j],f[k][j].pf=l;
}
else f[k][j].f=-1;

if(f[k-1][j].f>-1+eps)
{

else
{
{
int las=q[tail].i;
int l=j+1,r=R[las];
while(l<=r)
{
int mid=(l+r)>>1;
if(f[k-1][las].f+_sin[las][mid]<f[k-1][j].f+_sin[j][mid]) r=mid-1;
else l=mid+1;
}
int tmp=r+1;
if(tmp<=q[tail].ti) tail--;
else { q[++tail]=(node){j,tmp};break; }
}
}
}
}
}
for(int j=i+2;j<=n;j++) if(f[K][j].f>-1+eps&&(zero-p[i])*p[j]>eps&&ansnow<f[K][j].f+_sin[i][j])
{
ansnow=f[K][j].f+_sin[i][j];
ansn=0; int tx=j,tk=K;
while(tx)
{
ans[++ansn]=p[tx].i;
tx=f[tk--][tx].pf;
}
}
}
}
}/*
{
#define inf 1e7
const ld eps = 1e-12;
int rei,rej;
void dp(ld val)
{
rei=0; ld sum=-inf;
for(int i=1;i+K-1<=n;i++)
{
f[i][i].pf=0;
for(int j=i+1;j<=R[i];j++) f[i][j]=(data){_sin[i][j],i,2};
for(int j=R[i]+1;j<=n;j++) f[i][j].f=-inf;

for(int j=i+1;j<=n;j++)
{

{
ld tmp=f[i][l].f+_sin[l][j]+val;
if(tmp>f[i][j].f)
{
f[i][j].f=tmp;
f[i][j].pf=l,f[i][j].k=f[i][l].k+1;
}
}

if(f[i][j].f>-inf+eps)
{

else
{
{
int las=q[tail].i;
int l=j+1,r=R[las];
while(l<=r)
{
int mid=(l+r)>>1;
if(f[i][las].f+_sin[las][mid]<f[i][j].f+_sin[j][mid]) r=mid-1;
else l=mid+1;
}
int tmp=r+1;
if(tmp<=q[tail].ti) tail--;
else { q[++tail]=(node){j,tmp};break; }
}
}
}
}
for(int j=i+2;j<=n;j++)
if(f[i][j].f>-inf+eps&&(zero-p[i])*p[j]>eps&&sum<f[i][j].f+_sin[i][j])
{
sum=f[i][j].f+_sin[i][j];
rei=i,rej=j;
}
}
}
void solve()
{
ansn=0;
ld l=-4.0,r=0;
while(r-l>eps)
{
ld mid=(l+r)/2.0;
dp(mid);
if(rei&&f[rei][rej].k<=K)
{
if(f[rei][rej].k==K)
{
int tx=rej; ansn=0;
while(tx)
{
ans[++ansn]=p[tx].i;
tx=f[rei][tx].pf;
}
}
l=mid;
}
else r=mid;
}
}
}
*/
int main()
{
freopen("light.in","r",stdin);
freopen("light.out","w",stdout);

srand(100000007);

zero=(point){0,0};

scanf("%d%d",&n,&K);
if(K<=2)
{
puts("-1");
return 0;
}
for(int i=1;i<=n;i++) scanf("%Lf",&ang[i].fir),ang[i].sec=i;

sort(ang+1,ang+n+1);
for(int i=1;i<=n;i++) p[i]=(point){ang[i].sec,cos(ang[i].fir),sin(ang[i].fir),ang[i].fir};
for(int i=1;i<n;i++) for(int j=i+1;j<=n;j++)
_sin[i][j]=fabs(sin(p[j].ang-p[i].ang));
for(int i=1;i<=n;i++)
{
R[i]=n;
for(int j=i+1;j<=n;j++) if((zero-p[i])*p[j]>-1e-9) { R[i]=j-1;break; }
}

if(ansn==0) puts("-1");
else
{
for(int i=ansn;i>=1;i--) printf("%d%c",ans[i],i==1?'\n':' ');
}

return 0;
}


B：

code：

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1e9
using namespace std;

{
char c; while(!((c=getchar())>='0'&&c<='9'));
x=c-'0';
while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';
}
const int maxn = 1100000;

int n,m;
int MX[maxn],MN[maxn],ans[maxn];
struct edge{int y,c,nex;}a[maxn<<1]; int len,fir[maxn];
inline void ins(const int x,const int y,const int c){a[++len]=(edge){y,c,fir[x]};fir[x]=len;}

struct node
{
int u,v,w;
friend inline bool operator <(const node x,const node y){return x.w<y.w;}
}e[maxn];
int t[maxn],tp;

int fa[maxn];
int findfa(const int x){ return fa[x]==x?x:fa[x]=findfa(fa[x]); }

multiset<int>S;
multiset<int>::iterator it;
int dfn[maxn],dfi,siz[maxn],son[maxn],par[maxn],dep[maxn],top[maxn],d[maxn];
void dfs(const int x)
{
MN[x]=inf;
for(int k=fir[x];k;k=a[k].nex)
{
MN[x]=min(MN[x],a[k].c);
it=S.find(a[k].c); S.erase(it);
}
MX[x]=S.empty()?0:(*S.rbegin());
for(int k=fir[x];k;k=a[k].nex) S.insert(a[k].c);

dfn[x]=++dfi;
siz[x]=1;
for(int k=fir[x],y=a[k].y;k;k=a[k].nex,y=a[k].y) if(y!=par[x])
{
d[x]++;
par[y]=x,dep[y]=dep[x]+1,dfs(y);
if(siz[son[x]]<siz[y]) son[x]=y;
siz[x]+=siz[y];
}
}
void build(const int x,const int Top)
{
top[x]=Top;
if(son[x]) build(son[x],Top);
for(int k=fir[x],y=a[k].y;k;k=a[k].nex,y=a[k].y) if(y!=par[x]&&y!=son[x])
build(y,y);
}
int LCA(int x,int y)
{
int f1=top[x],f2=top[y];
while(f1!=f2)
{
if(dep[f1]<dep[f2]) swap(f1,f2),swap(x,y);
x=par[f1],f1=top[x];
}
return dep[x]<dep[y]?x:y;
}

int main()
{
freopen("vain.in","r",stdin);
freopen("vain.out","w",stdout);

sort(e+1,e+m+1);

for(int i=1;i<=n;i++) fa[i]=i;
for(int i=1;i<=m;i++)
{
int x=e[i].u,y=e[i].v,c=e[i].w;
int f1=findfa(x),f2=findfa(y);
if(f1==f2) t[++tp]=i;
else
{
fa[f1]=f2;
ins(x,y,c); ins(y,x,c);
S.insert(c);
}
}
if(tp+n-1!=m)
{
printf("%d\n",-n);
return 0;
}
dep[1]=1,dfs(1),d[1]--;
build(1,1);
for(int i=1;i<=n;i++) ans[i]=max(MX[i],MN[i]);

for(int i=1;i<=n;i++) fa[i]=i;
for(int ii=1;ii<=tp;ii++)
{
int i=t[ii];
int x=e[i].u,y=e[i].v;
if(findfa(x)==findfa(y)) continue;
if(dfn[x]>dfn[y]) swap(x,y);

if(dfn[x]+siz[x]>dfn[y])
{
y=findfa(y);
while(dep[y]>dep[x]+1)
{
int ff=par[y];
d[ff]--;
if(!d[ff]) ans[ff]=max(ans[ff],e[i].w);
ff=findfa(ff);
fa[y]=ff,y=ff;
}
}
else
{
int ff=LCA(x,y);
x=findfa(x);
while(dep[x]>dep[ff]+1)
{
int fx=par[x];
d[fx]--;
if(!d[fx]) ans[fx]=max(ans[fx],e[i].w);
fx=findfa(fx);
fa[x]=fx,x=fx;
}
y=findfa(y);
while(dep[y]>dep[ff]+1)
{
int fy=par[y];
d[fy]--;
if(!d[fy]) ans[fy]=max(ans[fy],e[i].w);
fy=findfa(fy);
fa[y]=fy,y=fy;
}
if(x==y) continue;
if(dep[x]>dep[ff]||dep[y]>dep[ff])
{
d[ff]--;
if(!d[ff]) ans[ff]=max(ans[ff],e[i].w);
}
if(dep[x]<dep[y]) swap(x,y);
fa[x]=y;
}
}

ll re=0;
for(int i=1;i<=n;i++) re+=d[i]?-1:ans[i];
printf("%lld\n",re);

return 0;
}


C：

code：

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

{
char c; while(!((c=getchar())>='0'&&c<='9'));
x=c-'0';
while((c=getchar())>='0'&&c<='9') (x*=10)+=c-'0';
}
inline void up(int &a,const int &b){if(a<b)a=b;}
const int maxn = 410000;

int n,m;
int w[maxn],par[maxn];
{
int son[maxn][2],fa[maxn],fl[maxn];
ll sum[maxn];

bool isrt(int x){ return son[fa[x]][0]!=x&&son[fa[x]][1]!=x; }
void pushdown(int x)
{
int fc=fl[x];
up(fl[son[x][0]],fc);
up(fl[son[x][1]],fc);
}
int t[maxn],tp;
void Down(int x)
{
int y=x; while(!isrt(y)) t[++tp]=y,y=fa[y];
t[++tp]=y;
while(tp) pushdown(t[tp--]);
}
void pushup(int x) { sum[x]=sum[son[x][0]]+sum[son[x][1]]+w[x]; }

void rot(int x)
{
int y=fa[x],z=fa[y];
if(!isrt(y)) son[z][son[z][1]==y]=x;
fa[x]=z;
int l=son[y][1]==x;
fa[son[y][l]=son[x][!l]]=y;
fa[son[x][!l]=y]=x;
pushup(y);
}
void splay(int x)
{
Down(x);
for(;!isrt(x);rot(x))
{
int y=fa[x],z=fa[y];
if(!isrt(y)) rot(((son[y][1]==x)^(son[z][1]==y))?x:y);
}
pushup(x);
}
int nowi;
ll Access(int x)
{
ll re=0;
for(int y=0;x;y=x,x=fa[x])
{
splay(x); son[x][1]=y,pushup(x);
if(fl[x]!=nowi)
{
re+=sum[x];
if(fl[son[x][1]]==nowi) re-=sum[son[x][1]];
}
fl[x]=nowi;
}
return re;
}
{
fa[y]=x;
}
void Cut(int x,int y)
{
Access(x);
splay(y); fa[y]=0;
}
}LCT;
ll ans;
int t[maxn],tp;

int main()
{
freopen("death.in","r",stdin);
freopen("death.out","w",stdout);

for(int i=2;i<=n;i++)
{
}

while(m--)
{
if(type==1)
{
w[++n]=c;
}
else if(type==2)
{
w[++n]=c;
LCT.Cut(par[x],x);
par[n]=par[x],par[x]=n;
}
else
{
LCT.nowi++;
ans=0;
for(int i=1;i<=tp;i++)
ans+=LCT.Access(t[i]);
printf("%lld\n",ans);
}
}

return 0;
}


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120