A Vacations

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int n;
int a[200][3];
int main()
{
// freopen("A.in","r",stdin);
// freopen(".out","w",stdout);

n=read(); MEM(a)
int ans=0;
For(i,n) {
int p=read();
a[i][0]=max(max(a[i-1][0],a[i-1][1]) ,a[i-1][2]);
if (p&1) a[i][1]=1+max(a[i-1][0],a[i-1][2]);
else a[i][1]=-INF;
if (p&2) a[i][2]=1+max(a[i-1][0],a[i-1][1]);
else a[i][2]=-INF;
}
ans=max(a[n][0],ans);
ans=max(a[n][1],ans);
ans=max(a[n][2],ans);
cout<<n-ans<<endl;

return 0;
}

B Fix a Tree

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int n;
#define
int fa[MAXN],ro=0,ans=0;
int b[MAXN]={0};
void search(int x,int tt) {
int p=x;
do {
b[x]=tt;
x=fa[x];
} while(!b[x]);
if (b[x]!=tt) return;
if (ro==0) {
ro=x;
fa[x]=x;
ans++;
return;
}
fa[x]=ro;
if (x!=ro) ++ans;


}
int main()
{
freopen("b.in","r",stdin);
// freopen(".out","w",stdout);
n=read();
For(i,n) fa[i]=read();
For(i,n) if (fa[i]==i) ro=i;
For(i,n) if (!b[i]) {
search(i,i);
}
cout<<ans<<endl;
For(i,n-1) cout<<fa[i]<<' ';cout<<fa[n]<<endl;

return 0;
}

C LRU

倒过来做

#include<bits/stdc++.h>
using namespace std;
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
#define
For(j,m-1) cout<<a[i][j]<<' ';\
cout<<a[i][m]<<endl; \
}
#pragma
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return ((a-b)%F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int read()
{
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) {if (ch=='-') f=-1; ch=getchar();}
while(isdigit(ch)) { x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int n;
double dp[1<<20],p[20],ans[20]={0};
int main()
{
// freopen("c.in","r",stdin);
// freopen(".out","w",stdout);

int n=read(),m=read();
Rep(i,n) cin>>p[i];
int S=1<<n;
dp[0]=1;
For(i,S-1) {
double sp=0;
int k=0;
Rep(j,n) if (((1<<j)&i) == 0 ) {
sp+=p[j];
++k;
}
dp[i]=0;
if (n-k>m) continue;
Rep(j,n) if (((1<<j)&i) !=0 ) {
dp[i]+=dp[i-(1<<j)] *p[j];
ans[j] += dp[i-(1<<j)] *p[j];
}
if (sp<1e-10) dp[i]=1 ;
else dp[i]/=sp;
}
Rep(i,n-1) printf("%.10lf ",ans[i]);
printf("%.10lf\n",ans[n-1]);

return 0;
}

D Limak and Shooting Points