「CF618X」Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)
A. Slime Combining
模拟或二进制拆分
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define pi acos(-1) #define mod 10000007 #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n; int bin[25]; int main() { bin[0]=1;for(int i=1;i<=20;i++)bin[i]=bin[i-1]<<1; n=read(); for(int i=20;i>=0;i--) if(bin[i]<=n) { n-=bin[i]; printf("%d ",i+1); } return 0; } |
B. Guess the Permutation
第i行出现的最多的数就是数列中pi的值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define pi acos(-1) #define mod 10000007 #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n; int a[55][55],ans[55]; bool check(int x,int l) { for(int i=1;i<=n;i++) if(a[l][i]&&a[l][i]!=x)return 0; return 1; } int main() { n=read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) a[i][j]=read(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(check(i,j)&&!ans[j]) { ans[j]=i; for(int k=1;k<=n;k++) a[j][k]=a[k][j]=0; break; } for(int i=1;i<=n;i++) printf("%d ",ans[i]); return 0; } |
C. Constellation
选一条任意直线上最近的两点,和离该直线最近的点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define pi acos(-1) #define eps 1e-13 #define mod 10000007 #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } struct P{ double x,y; friend double dis(P a){ return sqrt(a.x*a.x+a.y*a.y); } friend P operator-(P a,P b){ return (P){a.x-b.x,a.y-b.y}; } friend double operator*(P a,P b){ return a.x*b.y-a.y*b.x; } friend double operator/(P a,P b){ return a.x*b.x+a.y*b.y; } }a[100005]; double dis(P a,P b,P c) { return abs((a-b)*(c-b))/dis(a-b); } int n,p1,p2; double mn=1e60; int main() { n=read(); for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read(); p1=2; for(int i=3;i<=n;i++) if((a[i]-a[1])*(a[i]-a[p1])==0&&dis(a[i]-a[1])<dis(a[p1]-a[1])) p1=i; for(int i=2;i<=n;i++) if(i!=p1&&mn>dis(a[1],a[p1],a[i])&&(a[i]-a[1])*(a[p1]-a[1])!=0) { mn=dis(a[1],a[p1],a[i]); p2=i; } printf("1 %d %d",p1,p2); return 0; } |
D. Hamiltonian Spanning Tree
菊花图特判
x>=y直接输出(n-1)*y
否则树形dp或贪心求出最少要用的非树边数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#include<map> #include<set> #include<cmath> #include<stack> #include<queue> #include<cstdio> #include<vector> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define pi acos(-1) #define mod 10000007 #define inf 1000000000 #define ll long long using namespace std; ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,x,y; vector<int>e[200005]; ll f[200005][3]; int min3(int a,int b,int c) { return min(min(a,b),c); } void dp(int x,int fa) { for(int i=0;i<e[x].size();i++) { int y=e[x][i]; if(y!=fa) { dp(y,x); ll g0=f[x][0],g1=f[x][1],g2=f[x][2]; f[x][0]=min(g1+f[y][1],g0+min3(f[y][0],f[y][1],f[y][2])+1); f[x][1]=g1+min3(f[y][0],f[y][1],f[y][2])+1; f[x][1]=min(f[x][1],g2+f[y][1]); f[x][2]=g2+min3(f[y][0],f[y][1],f[y][2])+1; } } } int main() { n=read();x=read();y=read(); for(int i=1;i<=n-1;i++) { int u=read(),v=read(); e[u].push_back(v); e[v].push_back(u); } if(x>=y) { for(int i=1;i<=n;i++) if(e[i].size()==n-1) { cout<<(ll)(n-2)*y+x; return 0; } cout<<(ll)(n-1)*y; return 0; } dp(1,0); ll ans=inf; for(int i=0;i<2;i++) ans=min(ans,f[1][i]); cout<<ans*y+(n-1-ans)*x; return 0; } |
Subscribe