Codeforces Beta Round #18 (Div. 2 Only)

Codeforces Beta Round #18 (Div. 2 Only)

http://codeforces.com/contest/18

A

暴力

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 500005
 7 typedef long long ll;
 8 /*#ifndef ONLINE_JUDGE
 9         freopen("1.txt","r",stdin);
10 #endif */
11 
12 int a[6];
13 
14 void Check(char *s){
15     int aa=sqr(a[0]-a[2])+sqr(a[1]-a[3]);
16     int bb=sqr(a[2]-a[4])+sqr(a[3]-a[5]);
17     int cc=sqr(a[4]-a[0])+sqr(a[5]-a[1]);
18     if((aa&&bb&&cc)==0) return;
19   //  cout<<aa<<" "<<bb<<" "<<cc<<endl;
20     if(aa+bb==cc||aa+cc==bb||bb+cc==aa){
21         cout<<s<<endl;
22         exit(0);
23     }
24 }
25 
26 int main(){
27     #ifndef ONLINE_JUDGE
28        // freopen("1.txt","r",stdin);
29     #endif
30    // std::ios::sync_with_stdio(false);
31     for(int i=0;i<6;i++) cin>>a[i];
32     Check("RIGHT");
33     for(int i=0;i<6;i++){
34       //  cout<<a[i]<<"g"<<endl;
35         a[i]--;
36         Check("ALMOST");
37         a[i]+=2;
38         Check("ALMOST");
39         a[i]--;
40      //   cout<<a[i]<<"h"<<endl;
41 
42     }
43     cout<<"NEITHER"<<endl;
44 
45 }
View Code

B

水题

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 500005
 7 typedef long long ll;
 8 /*#ifndef ONLINE_JUDGE
 9         freopen("1.txt","r",stdin);
10 #endif */
11 
12 
13 int main(){
14     #ifndef ONLINE_JUDGE
15   //      freopen("1.txt","r",stdin);
16     #endif
17     std::ios::sync_with_stdio(false);
18     int n,m,l,d;
19     cin>>n>>d>>m>>l;
20     ll x=0,L=-m,R=-m+l;
21     for(int i=0;i<n;i++){
22         L+=m;
23         R+=m;
24         if(x+d<L) break;
25         ll o=(R-x)/d;
26         x+=d*o;
27     }
28     cout<<x+d<<endl;
29 }
View Code

C

水题

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 #include<iostream>
 5 using namespace std;
 6 typedef long long ll;
 7 
 8 int n;
 9 ll a[100005]; 
10  
11 int main(){
12     cin>>n;
13     ll ans=0;
14     int b;
15     for(int i=1;i<=n;i++){
16         cin>>b;
17         a[i]=a[i-1]+b;
18     }
19     for(int i=1;i<=n-1;i++){
20         if(a[n]-a[i]==a[i]) ans++;
21     }
22     cout<<ans<<endl;
23 }
View Code

D

贪心,因为有2^n>2^(n-1)+2^(n-2)+...+1的规律,所以可以优先挑选大的。挑选的时候是从后往前选比较好写

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define lson l,mid,rt<<1
  4 #define rson mid+1,r,rt<<1|1
  5 #define sqr(x) ((x)*(x))
  6 #define maxn 500005
  7 typedef long long ll;
  8 /*#ifndef ONLINE_JUDGE
  9         freopen("1.txt","r",stdin);
 10 #endif */
 11 
 12 #define MAXN 9999
 13 #define MAXSIZE 5007//位数
 14 #define DLEN 4
 15 
 16 class BigNum
 17 {
 18 private:
 19     int a[5007];    //可以控制大数的位数
 20     int len;       //大数长度
 21 public:
 22     BigNum()
 23     {
 24         len = 1;    //构造函数
 25         memset(a,0,sizeof(a));
 26     }
 27     BigNum(const int);       //将一个int类型的变量转化为大数
 28     BigNum(const char*);     //将一个字符串类型的变量转化为大数
 29     BigNum(const BigNum &);  //拷贝构造函数
 30     BigNum &operator=(const BigNum &);   //重载赋值运算符,大数之间进行赋值运算
 31 
 32     friend istream& operator>>(istream&,  BigNum&);   //重载输入运算符
 33     friend ostream& operator<<(ostream&,  BigNum&);   //重载输出运算符
 34 
 35     BigNum operator+(const BigNum &) const;   //重载加法运算符,两个大数之间的相加运算
 36     BigNum operator-(const BigNum &) const;   //重载减法运算符,两个大数之间的相减运算
 37     BigNum operator*(const BigNum &) const;   //重载乘法运算符,两个大数之间的相乘运算
 38     BigNum operator/(const int   &) const;    //重载除法运算符,大数对一个整数进行相除运算
 39 
 40     BigNum operator^(const int  &) const;    //大数的n次方运算
 41     int    operator%(const int  &) const;    //大数对一个int类型的变量进行取模运算
 42     bool   operator>(const BigNum & T)const;   //大数和另一个大数的大小比较
 43     bool   operator>(const int & t)const;      //大数和一个int类型的变量的大小比较
 44 
 45     void print();       //输出大数
 46 };
 47 BigNum::BigNum(const int b)     //将一个int类型的变量转化为大数
 48 {
 49     int c,d = b;
 50     len = 0;
 51     memset(a,0,sizeof(a));
 52     while(d > MAXN)
 53     {
 54         c = d - (d / (MAXN + 1)) * (MAXN + 1);
 55         d = d / (MAXN + 1);
 56         a[len++] = c;
 57     }
 58     a[len++] = d;
 59 }
 60 BigNum::BigNum(const char*s)     //将一个字符串类型的变量转化为大数
 61 {
 62     int t,k,index,l,i;
 63     memset(a,0,sizeof(a));
 64     l=strlen(s);
 65     len=l/DLEN;
 66     if(l%DLEN)
 67         len++;
 68     index=0;
 69     for(i=l-1; i>=0; i-=DLEN)
 70     {
 71         t=0;
 72         k=i-DLEN+1;
 73         if(k<0)
 74             k=0;
 75         for(int j=k; j<=i; j++)
 76             t=t*10+s[j]-'0';
 77         a[index++]=t;
 78     }
 79 }
 80 BigNum::BigNum(const BigNum & T) : len(T.len)  //拷贝构造函数
 81 {
 82     int i;
 83     memset(a,0,sizeof(a));
 84     for(i = 0 ; i < len ; i++)
 85         a[i] = T.a[i];
 86 }
 87 BigNum & BigNum::operator=(const BigNum & n)   //重载赋值运算符,大数之间进行赋值运算
 88 {
 89     int i;
 90     len = n.len;
 91     memset(a,0,sizeof(a));
 92     for(i = 0 ; i < len ; i++)
 93         a[i] = n.a[i];
 94     return *this;
 95 }
 96 istream& operator>>(istream & in,  BigNum & b)   //重载输入运算符
 97 {
 98     char ch[MAXSIZE*4];
 99     int i = -1;
100     in>>ch;
101     int l=strlen(ch);
102     int count=0,sum=0;
103     for(i=l-1; i>=0;)
104     {
105         sum = 0;
106         int t=1;
107         for(int j=0; j<4&&i>=0; j++,i--,t*=10)
108         {
109             sum+=(ch[i]-'0')*t;
110         }
111         b.a[count]=sum;
112         count++;
113     }
114     b.len =count++;
115     return in;
116 
117 }
118 ostream& operator<<(ostream& out,  BigNum& b)   //重载输出运算符
119 {
120     int i;
121     cout << b.a[b.len - 1];
122     for(i = b.len - 2 ; i >= 0 ; i--)
123     {
124         cout.width(DLEN);
125         cout.fill('0');
126         cout << b.a[i];
127     }
128     return out;
129 }
130 
131 BigNum BigNum::operator+(const BigNum & T) const   //两个大数之间的相加运算
132 {
133     BigNum t(*this);
134     int i,big;      //位数
135     big = T.len > len ? T.len : len;
136     for(i = 0 ; i < big ; i++)
137     {
138         t.a[i] +=T.a[i];
139         if(t.a[i] > MAXN)
140         {
141             t.a[i + 1]++;
142             t.a[i] -=MAXN+1;
143         }
144     }
145     if(t.a[big] != 0)
146         t.len = big + 1;
147     else
148         t.len = big;
149     return t;
150 }
151 BigNum BigNum::operator-(const BigNum & T) const   //两个大数之间的相减运算
152 {
153     int i,j,big;
154     bool flag;
155     BigNum t1,t2;
156     if(*this>T)
157     {
158         t1=*this;
159         t2=T;
160         flag=0;
161     }
162     else
163     {
164         t1=T;
165         t2=*this;
166         flag=1;
167     }
168     big=t1.len;
169     for(i = 0 ; i < big ; i++)
170     {
171         if(t1.a[i] < t2.a[i])
172         {
173             j = i + 1;
174             while(t1.a[j] == 0)
175                 j++;
176             t1.a[j--]--;
177             while(j > i)
178                 t1.a[j--] += MAXN;
179             t1.a[i] += MAXN + 1 - t2.a[i];
180         }
181         else
182             t1.a[i] -= t2.a[i];
183     }
184     t1.len = big;
185     while(t1.a[t1.len - 1] == 0 && t1.len > 1)
186     {
187         t1.len--;
188         big--;
189     }
190     if(flag)
191         t1.a[big-1]=0-t1.a[big-1];
192     return t1;
193 }
194 
195 BigNum BigNum::operator*(const BigNum & T) const   //两个大数之间的相乘运算
196 {
197     BigNum ret;
198     int i,j,up;
199     int temp,temp1;
200     for(i = 0 ; i < len ; i++)
201     {
202         up = 0;
203         for(j = 0 ; j < T.len ; j++)
204         {
205             temp = a[i] * T.a[j] + ret.a[i + j] + up;
206             if(temp > MAXN)
207             {
208                 temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
209                 up = temp / (MAXN + 1);
210                 ret.a[i + j] = temp1;
211             }
212             else
213             {
214                 up = 0;
215                 ret.a[i + j] = temp;
216             }
217         }
218         if(up != 0)
219             ret.a[i + j] = up;
220     }
221     ret.len = i + j;
222     while(ret.a[ret.len - 1] == 0 && ret.len > 1)
223         ret.len--;
224     return ret;
225 }
226 BigNum BigNum::operator/(const int & b) const   //大数对一个整数进行相除运算
227 {
228     BigNum ret;
229     int i,down = 0;
230     for(i = len - 1 ; i >= 0 ; i--)
231     {
232         ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
233         down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
234     }
235     ret.len = len;
236     while(ret.a[ret.len - 1] == 0 && ret.len > 1)
237         ret.len--;
238     return ret;
239 }
240 int BigNum::operator %(const int & b) const    //大数对一个int类型的变量进行取模运算
241 {
242     int i,d=0;
243     for (i = len-1; i>=0; i--)
244     {
245         d = ((d * (MAXN+1))% b + a[i])% b;
246     }
247     return d;
248 }
249 BigNum BigNum::operator^(const int & n) const    //大数的n次方运算
250 {
251     BigNum t,ret(1);
252     int i;
253     if(n<0)
254         exit(-1);
255     if(n==0)
256         return 1;
257     if(n==1)
258         return *this;
259     int m=n;
260     while(m>1)
261     {
262         t=*this;
263         for( i=1; i<<1<=m; i<<=1)
264         {
265             t=t*t;
266         }
267         m-=i;
268         ret=ret*t;
269         if(m==1)
270             ret=ret*(*this);
271     }
272     return ret;
273 }
274 bool BigNum::operator>(const BigNum & T) const   //大数和另一个大数的大小比较
275 {
276     int ln;
277     if(len > T.len)
278         return true;
279     else if(len == T.len)
280     {
281         ln = len - 1;
282         while(a[ln] == T.a[ln] && ln >= 0)
283             ln--;
284         if(ln >= 0 && a[ln] > T.a[ln])
285             return true;
286         else
287             return false;
288     }
289     else
290         return false;
291 }
292 bool BigNum::operator >(const int & t) const    //大数和一个int类型的变量的大小比较
293 {
294     BigNum b(t);
295     return *this>b;
296 }
297 
298 void BigNum::print()    //输出大数
299 {
300     int i;
301     cout << a[len - 1];
302     for(i = len - 2 ; i >= 0 ; i--)
303     {
304         cout.width(DLEN);
305         cout.fill('0');
306         cout << a[i];
307     }
308     cout << endl;
309 }
310 BigNum num1[5007];
311 char str[5007][10];
312 int num2[5007],num3[5007];
313 BigNum Jud(int n,int m)
314 {
315     BigNum sum,mul;
316     sum=BigNum(1);
317     mul=BigNum(2);
318     while(m)
319     {
320         if(m&1)
321          sum=sum*mul;
322          mul=mul*mul;
323          m>>=1;
324     }
325     return sum;
326 }
327 
328 int n;
329 struct sair{
330     string str;
331     int num;
332     int pos;
333 }a[5005];
334 
335 int book[5005];
336 
337 bool cmp(sair a,sair b){
338     return a.num==b.num?a.pos>b.pos:a.num>b.num;
339 }
340 
341 bool Check(int x,int y){
342     for(int i=x;i<=y;i++){
343         if(book[i]) return false;
344     }
345     for(int i=x;i<=y;i++){
346         book[i]=1;
347     }
348     return true;
349 }
350 
351 int main(){
352     #ifndef ONLINE_JUDGE
353         freopen("1.txt","r",stdin);
354     #endif
355     //std::ios::sync_with_stdio(false);
356     cin>>n;
357     for(int i=1;i<=n;i++){
358         cin>>a[i].str>>a[i].num;
359         a[i].pos=i;
360     }
361     sort(a+1,a+n+1,cmp);
362     vector<int>ve;
363     for(int i=1;i<=n;i++){
364         if(a[i].str=="sell"){
365             for(int j=i+1;j<=n;j++){
366                 if(a[j].str=="win"&&a[j].num==a[i].num){
367                     if(Check(a[j].pos,a[i].pos)){
368                         ve.push_back(a[i].num);
369                     }
370                 }
371             }
372         }
373     }
374     BigNum ans=0;
375     for(int i=0;i<ve.size();i++){
376         ans=ans+Jud(2,ve[i]);
377     }
378     ans.print();
379 
380 }
View Code

E

dp

先预处理出每个格子各个颜色的花费,然后开始DP

参考博客:https://blog.csdn.net/c20190102/article/details/81775186

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define maxn 505
 7 #define maxc 35
 8 typedef long long ll;
 9 /*#ifndef ONLINE_JUDGE
10         freopen("1.txt","r",stdin);
11 #endif */
12 
13 int n,m;
14 int dp[maxn][maxc][maxc];
15 char color[maxn][maxn];
16 int cost[maxn][maxc][maxc];
17 pair<int,int>pre[maxn][maxc][maxc];
18 
19 void print(int floor,int ii,int jj){
20     if(floor>1){
21         print(floor-1,pre[floor][ii][jj].first,pre[floor][ii][jj].second);
22     }
23     for(int i=1;i<=m;i++){
24         if(i&1) printf("%c",'a'-1+ii);
25         else printf("%c",'a'-1+jj);
26     }
27     printf("\n");
28 }
29 
30 int main(){
31     #ifndef ONLINE_JUDGE
32         freopen("1.txt","r",stdin);
33     #endif
34     //std::ios::sync_with_stdio(false);
35     scanf("%d %d",&n,&m);
36     for(int i=1;i<=n;i++){
37         scanf("%s",color[i]+1);
38     }
39     memset(dp,0x3f,sizeof(dp));
40     memset(cost,0x3f,sizeof(dp));
41     for(int i=1;i<=n;i++){
42         for(int j=1;j<=26;j++){
43             for(int k=1;k<=26;k++){
44                 if(j!=k){
45                     cost[i][j][k]=0;
46                     for(int l=1;l<=m;l+=2){
47                         cost[i][j][k]+=(color[i][l]!=('a'-1+j));
48                     }
49                     for(int l=2;l<=m;l+=2){
50                         cost[i][j][k]+=(color[i][l]!=('a'-1+k));
51                     }
52                 }
53             }
54         }
55     }
56     for(int i=1;i<=26;i++){
57         for(int j=1;j<=26;j++){
58             dp[1][i][j]=cost[1][i][j];
59         }
60     }
61     for(int i=1;i<=n;i++){
62         for(int j=1;j<=26;j++){
63             for(int k=1;k<=26;k++){
64                 for(int p=1;p<=26;p++){
65                     for(int q=1;q<=26;q++){
66                         if(p!=j&&q!=k){
67                             if(dp[i][j][k]>dp[i-1][p][q]+cost[i][j][k]){
68                                 pre[i][j][k]=make_pair(p,q);
69                                 dp[i][j][k]=dp[i-1][p][q]+cost[i][j][k];
70                             }
71                         }
72                     }
73                 }
74             }
75         }
76     }
77     int ans=0x3f3f3f3f;
78     int ansi,ansj;
79     for(int i=1;i<=26;i++){
80         for(int j=1;j<=26;j++){
81             if(ans>dp[n][i][j]){
82                 ans=dp[n][ansi=i][ansj=j];
83             }
84         }
85     }
86     printf("%d\n",ans);
87     print(n,ansi,ansj);
88 }
View Code

猜你喜欢

转载自www.cnblogs.com/Fighting-sh/p/10360119.html