Simulation 20190922CSP-S

This is USACO 2019 JAN Gold's original title, probably because too much water, so I started to do even more than eight also advance ak. . . To write a solution to a problem. .

A.Cow Poetry

Clearly they charge the same rhyme line requires only the last word belongs to a rhyme unit, front length $ K-s_i $ casually row, DP about the length of the $ i $ the number of species, similar to the transfer backpack, just put items on the inner layer, capacity on the outer enumeration, order is not guaranteed so that there is a different number of kinds simultaneously.

Then, for each of the rows require the same rhyme, can enumerate every rhyme, combined multiplication and addition principle principles derived number scheme.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define dbg(x) cerr << #x << " = " << x <<endl
 7 using namespace std;
 8 typedef long long ll;
 9 typedef double db;
10 typedef pair<int,int> pii;
11 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
12 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
13 template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,1):0;}
14 template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,1):0;}
15 template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
16 template<typename T>inline T read(T&x){
17     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
18     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
19 }
20 const int N=5000+7,P=1e9+7;
21 char opt[3];
22 int f[N],now;
23 int cnt[27],typ[N];
24 int s[N],c[N];
25 int n,m,k,maxc,ans=1;
26 inline void add(int&A,int B){A+=B;A>=P&&(A-=P);}
27 inline int fpow(int x,int p){int ret=1;for(;p;p>>=1,x=x*1ll*x%P)if(p&1)ret=ret*1ll*x%P;return ret;}
28 
29 int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
30     read(n),read(m),read(k);
31     for(register int i=1;i<=n;++i)read(s[i]),MAX(maxc,read(c[i]));
32     for(register int i=1;i<=m;++i)scanf("%s",opt),++cnt[opt[0]-'A'];
33     f[0]=1;
34     for(register int i=0;i<=k;++i)
35         for(register int j=1;j<=n;++j)if(i+s[j]<=k)add(f[i+s[j]],f[i]);
36     for(register int i=1;i<=n;++i)if(k>=s[i])add(typ[c[i]],f[k-s[i]]);
37     for(register int i=0;i<26;++i)if(cnt[i]){
38         int tmp=0;
39         for(register int j=1;j<=maxc;++j)if(typ[j]){
40             add(tmp,fpow(typ[j],cnt[i]));
41         }
42         years = years * * 1ll tmp% P;
43      }
 44      printf ( " % d \ n " , year);
45      return  0 ;
46 }
View Code

B. Sleepy Cow Sorting

This is really water. . Obviously you can find the last increase in the longest sequence ordered not to move, put the number in front of the back constantly plug, thus ensuring a minimum number of times. So BIT maintain the situation as can be.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #define dbg(x) cerr << #x << " = " << x <<endl
 7 using namespace std;
 8 typedef long long ll;
 9 typedef double db;
10 typedef pair<int,int> pii;
11 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
12 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
13 template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,1):0;}
14 template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,1):0;}
15 template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
16 template<typename T>inline T read(T&x){
17     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
18     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
19 }
20 const int N=1e5+7;
21 int C[N];
22 int a[N];
23 int n,las,pos;
24 #define lowbit(x) x&(-x)
25 inline void add(int x){for(;x<=n;x+=lowbit(x))++C[x];}
26 inline int sum(int x){int ret=0;for(;x;x-=lowbit(x))ret+=C[x];return ret;}
27 
28 int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
29     read(n);
30     for(register int i=1;i<=n;++i)read(a[i]);
31     las=a[n];add(las);pos=n-1;
32     for(register int i=n-1;i;--i)if(MIN(las,a[i]))--pos,add(a[i]);else break;
33     printf("%d\n",pos);
34     for(register int i=1;i<=pos;++i)printf("%d ",pos-i+sum(a[i])),add(a[i]);
35     puts("");return 0;
36 }
View Code

C. Shortcut

Each point shortest path to the root of the tree is the shortest unique (as required have been lexicographically smallest), so that the shortest path tree is the only configuration, then run again dijkstra parent recording side (Note Update min) after and enumerate the root of violence which is connected to calculate the contribution can take max.

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<queue>
 7 #define dbg(x) cerr << #x << " = " << x <<endl
 8 using namespace std;
 9 typedef long long ll;
10 typedef double db;
11 typedef pair<int,int> pii;
12 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
13 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
14 template<typename T>inline char MIN(T&A,T B){return A>B?(A=B,1):0;}
15 template<typename T>inline char MAX(T&A,T B){return A<B?(A=B,1):0;}
16 template<typename T>inline void _swap(T&A,T&B){A^=B^=A^=B;}
17 template<typename T>inline T read(T&x){
18     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
19     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
20 }
21 const int N=1e5+7;
22 struct thxorz{int to,nxt,w;}G[N];
23 int Head[N],tot,c[N];
24 int n,m,t;
25 inline void Addedge(int x,int y,int z){
26     G[++tot].to=y,G[tot].nxt=Head[x],Head[x]=tot,G[tot].w=z;
27     G[++tot].to=x,G[tot].nxt=Head[y],Head[y]=tot,G[tot].w=z;
28 }
29 int dis[N],fa[N],val[N];
30 priority_queue<pii,vector<pii>,greater<pii> > q;
31 #define y G[j].to
32 inline void dij(){
33     memset(fa,0x3f,sizeof fa);
34     memset(dis,0x3f,sizeof dis);q.push(make_pair(dis[1]=0,1));
35     while(!q.empty()){
36         int d=q.top().first,x=q.top().second;q.pop();
37         if(dis[x]^d)continue;
38         for(register int j=Head[x];j;j=G[j].nxt)
39             if(d+G[j].w==dis[y]&&MIN(fa[y],x))val[y]=G[j].w;
40             else if(d+G[j].w<dis[y])q.push(make_pair(dis[y]=d+G[j].w,y)),fa[y]=x,val[y]=G[j].w;
41     }
42 }
43 ll ans=0;
44 int dfs(int x,int fa,int dep){
45     int siz=c[x];
46     for(register int j=Head[x];j;j=G[j].nxt)if(y^fa)siz+=dfs(y,x,dep+G[j].w);
47     MAX(ans,(dep-t)*1ll*siz);
48     return siz;
49 }
50 #undef y
51 int main(){//freopen("test.in","r",stdin);//freopen("test.ans","w",stdout);
52     read(n),read(m),read(t);
53     for(register int i=1;i<=n;++i)read(c[i]);
54     for(register int i=1,x,y,z;i<=m;++i)read(x),read(y),read(z),Addedge(x,y,z);
55     dij();memset(Head,0,sizeof Head);tot=0;
56     for(register int i=2;i<=n;++i)Addedge(i,fa[i],val[i]);
57     dfs(1,0,0);
58     printf("%lld\n",ans);
59     return 0;
60 }
View Code

 

Guess you like

Origin www.cnblogs.com/saigyouji-yuyuko/p/11566642.html