牛客多校第六场

A.Garbage Classification

签到,看半天看懂干垃圾湿垃圾==

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int A[26];
 4 int B[26];
 5 int C[3];
 6 int main()
 7 {
 8     string s;
 9     int t;
10     scanf("%d",&t);
11     for(int i=1;i<=t;i++){
12         cout<<"Case #"<<i<<": ";
13         cin>>s;
14         memset(B,0,sizeof B);
15         int n=s.length();
16         for(int i=0;i<n;i++){
17             B[s[i]-'a']++;
18         }
19         cin>>s;
20         
21         for(int i=0;i<26;i++){
22             if(s[i]=='h')A[i]=1;
23             else if(s[i]=='d')A[i]=2;
24             else A[i]=3;
25         }
26         C[0]=C[1]=C[2]=0;
27         for(int i=0;i<26;i++){
28             if(A[i]==1){
29                 C[0]+=B[i];
30             }else if(A[i]==2){
31                 C[1]+=B[i];
32             }else{
33                 C[2]+=B[i];
34             }
35         }
36         if(C[0]*1.0/n>=0.25)puts("Harmful");
37         else if(C[0]*1.0/n<=0.1)puts("Recyclable");
38         else if(C[1]>=2*C[2])puts("Dry");
39         else puts("Wet");
40     }
41 }

B.Shorten IPv6 Address模拟

细节:

要求字符串最短且字母序最小

双冒号加在不同位置贡献是不一样的==

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 stack<char> st;
  4 vector<char> v;
  5 vector<int>ans;
  6 char B[10][100];
  7 char A[10][8];
  8 void deal(string s)
  9 {
 10     ans.clear();
 11     memset(A,0,sizeof A);
 12     v.clear();
 13     int n=s.length();
 14     int j,t;
 15     for(int i=n-1; i>=0;)
 16     {
 17         if(i-3>=0)
 18         {
 19             j=i-3;
 20         }
 21         else j=0;
 22         t=0;
 23         for(j; j<=i; j++)
 24         {
 25             t=t*2+s[j]-'0';
 26         }
 27         i-=4;
 28 
 29         if(t<10)
 30         {
 31             st.push(char(t+'0'));
 32         }
 33         else
 34         {
 35             t=t-10+'a';
 36             st.push(char(t));
 37 
 38         }
 39     }
 40     while(!st.empty())
 41     {
 42         v.push_back(st.top());
 43         //cout<<st.top();
 44         st.pop();
 45     }
 46     //cout<<'
';
 47     int _n=v.size();
 48     bool f=0;
 49     bool g=0,h=0;
 50     int _x,_y=0;
 51     for(int i=0; i<_n;)
 52     {
 53         f=0;
 54         _x=0;
 55         for(int j=0; j<4; j++)
 56         {
 57             if(!f&&v[i]=='0'&&j<3)
 58             {
 59                 i++;
 60             }
 61             else if(!f&&v[i]=='0'&&j==3)
 62             {
 63                 A[_y][_x++]='0';
 64                 i++;
 65             }
 66             else
 67             {
 68                 f=1;
 69 
 70                 A[_y][_x++]=v[i];
 71                 i++;
 72             }
 73 
 74         }
 75         A[_y][_x]='';
 76         _y++;
 77 
 78     }
 79 
 80     int _a=0,_b=0,_c=0,_d=0;
 81 
 82     for(int i=0; i<8; i++)
 83     {
 84         if(strcmp(A[i],"0")==0)
 85         {
 86             _a++;
 87         }
 88         else
 89         {
 90             if(_a>_b)
 91             {
 92                 _b=_a,_c=i-_b;
 93                 ans.clear();
 94                 ans.push_back(_c);
 95             }
 96             else if(_a==_b)
 97             {
 98                 _c=i-_b;
 99                 ans.push_back(_c);
100             }
101             _a=0;
102         }
103 
104     }
105     if(_a>_b){_b=_a;_c=8-_b;ans.clear();ans.push_back(_c);}
106     else if(_a==_b){_c=8-_b;ans.push_back(_c);}
107 
108     if(_b>1)
109     {
110         _d=1;
111 
112     }
113     else
114     {
115         _c=-1;
116         ans.clear();
117     }
118     ans.push_back(-1);
119     int N=ans.size();
120     int _k;
121 
122     for(int j=0; j<N; j++)
123     {
124         _k=0;
125         _c=ans[j];
126       //  cout<<_c<<'
';
127         //cout<<j<<endl;
128         for(int i=0; i<8; i++)
129         {
130             if(i==_c)
131             {
132                 B[j][_k++]=':';
133                 B[j][_k++]=':';
134                 i=_c+_b-1;
135                 continue;
136             }
137             else
138             {
139                 int _y=strlen(A[i]);
140                 for(int kk=0;kk<_y;kk++){
141                     B[j][_k++]=A[i][kk];
142                 }
143             }
144             if(i<7&&i!=_c-1)
145             {
146                 B[j][_k++]=':';
147             }//else cout<<'
';
148         }
149         B[j][_k]='';
150     //    cout<<B[j]<<'
';
151     }
152 
153     if(N<=2){
154         puts(B[0]);
155     }else{
156         char *s=B[0];
157         int mx=strlen(s);
158         for(int i=1;i<N;i++){
159             int t=strlen(B[i]);
160             if((t<mx)||t==mx&&(strcmp(s,B[i])>0))
161                 s=B[i],mx=t;
162         }
163         cout<<s<<'
';
164 
165     }
166 
167 }
168 int main()
169 {
170    // cout<<int(':')<<' ' <<int('a')<<' '<<int('9');
171     string s;
172     int t;
173     scanf("%d",&t);
174     for(int i=1; i<=t; i++)
175     {
176         cout<<"Case #"<<i<<": ";
177         cin>>s;
178         deal(s);
179     }
180 }

J.Upgrading Technology

思路:枚举填满的层数d,贪心选取technology向上延伸,由于此时不能填满d+1层,所以最多只能选n-1个technology向上延伸。

对于每个technology向上延伸多少利益最大?暴力枚举可能会TLE,因此维护一个前缀和,即使得$sum_{i,k}-sum_{i,j} kin {j+1,j+2,cdots,m}$最小,然后分为用单调栈维护当前$j$后最小的$sum_{i,k}$的位置。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 ll D[1005];
 5 ll C[1005][1005];
 6 ll L[1005];
 7 stack<int> st[1005];
 8 vector<ll>v;
 9 int main()
10 {
11     int T;
12     scanf("%d",&T);
13     int n,m;
14     for(int i=1; i<=T; i++)
15     {
16         memset(L,0,sizeof L);
17         scanf("%d%d",&n,&m);
18         for(int i=1; i<=n; i++)
19 
20         {
21             while(!st[i].empty())st[i].pop();
22             for(int j=1; j<=m; j++)
23             {
24                 scanf("%lld",&C[i][j]);
25                 L[j]+=C[i][j];
26 
27                 C[i][j]+=C[i][j-1];
28        //         cout<<C[i][j]<<' ';
29             }
30      //       cout<<"QWQ"<<'
';
31             for(int j=m; j>=1; j--)
32             {
33                 if(st[i].empty())
34                 {
35                     st[i].push(j);
36                 }
37                 else if(C[i][st[i].top()]>C[i][j])
38                 {
39                     st[i].push(j);
40                 }
41                 else ;
42             }
43         }
44         for(int i=1; i<=m; i++)
45         {
46             scanf("%lld",&D[i]);
47             D[i]-=L[i];
48             D[i]+=D[i-1];
49         }
50         ll t=0,ans=0;
51         //cout<<ans<<'
';
52         for(int i=0; i<=m; i++)
53         {
54             t=D[i];
55             v.clear();
56             for(int j=1;j<=n;j++){
57                 while(!st[j].empty()&&st[j].top()<=i)st[j].pop();
58                 if(!st[j].empty())
59                 v.push_back(C[j][st[j].top()]-C[j][i]);
60                 else v.push_back(0);
61             }
62             sort(v.begin(),v.end());
63             for(int i=0;i<n-1;i++){
64                 if(v[i]>0)break;
65                 else t-=v[i];
66             }
67             ans=max(ans,t);
68         }
69         cout<<"Case #"<<i<<": "<<ans<<'
';
70     }
71 }
72 /**
73 10
74 3 3
75 1 2 3
76 1 -3 -4
77 3  1 3
78 -2 -3 -4
79 */
原文地址:https://www.cnblogs.com/liulex/p/11296895.html