「CF512X」Codeforces Round #290 (Div. 1)
做了俩题就丧失了动力
A.Fox And Names
建图完拓扑排序
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 |
#include<map> #include<set> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long #define pa pair<ll,int> 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,top,tot,d[105],st[105]; char a[105][105],b[105]; vector<int>e[105]; void insert(int u,int v) { e[u].push_back(v); d[v]++; } bool topsort() { while(top) { int now=st[top--];b[++tot]=now+'a'-1; for(int i=0;i<e[now].size();i++) { int v=e[now][i]; d[v]--;if(!d[v])st[++top]=v; } } for(int i=1;i<=26;i++)if(d[i])return 0; return 1; } int main() { n=read(); for(int i=1;i<=n;i++) scanf("%s",a[i]+1); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) { int t1=strlen(a[i]+1),t2=strlen(a[j]+1); for(int k=1;k<=t1;k++) if(k>t2){puts("Impossible");return 0;} else if(a[i][k]!=a[j][k]) { insert(a[i][k]-'a'+1,a[j][k]-'a'+1);break; } } for(int i=1;i<=26;i++)if(!d[i])st[++top]=i; if(!topsort())puts("Impossible"); else { for(int i=1;i<=tot;i++)printf("%c",b[i]); } return 0; } |
B. Fox And Jumping
记忆化暴力QAQ
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 |
#include<map> #include<set> #include<cmath> #include<ctime> #include<queue> #include<cstdio> #include<vector> #include<bitset> #include<cstring> #include<iostream> #include<algorithm> #define inf 1000000000 #define ll long long #define pa pair<ll,int> 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 gcd(int a,int b) { return b==0?a:gcd(b,a%b); } int n; int l[305],c[305]; map<int,int>f[305]; int dp(int n,int t) { if(t==1)return 0; if(n==0)return inf; if(f[n][t])return f[n][t]; f[n][t]=min(dp(n-1,t),dp(n-1,gcd(l[n],t))+c[n]); return f[n][t]; } int main() { n=read(); for(int i=1;i<=n;i++)l[i]=read(); for(int i=1;i<=n;i++)c[i]=read(); int ans=dp(n,0); if(ans==inf)puts("-1"); else printf("%d\n",ans); return 0; } |
Subscribe