Codeforces Round #239 (Div. 2)

做了三个题,先贴一下代码。。。终于涨分了

A. Line to Cashier

水题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 using namespace std;
 8 const int INF = (1<<28);
 9 
10 int main()
11 {
12     int Min, n, a[110], ans;
13     int i, j;
14     while(cin>>n)
15     {
16         Min = INF;
17         for(i = 0; i < n; i++)
18         cin>>a[i];
19         for(i = 0; i < n; i++)
20         {
21              ans = a[i]*15;
22             for(j = 0; j < a[i]; j++)
23             {
24                int b;
25               cin>>b;
26                ans += b*5;
27             }
28             if(ans<Min)
29             Min = ans;
30         }
31         cout<<Min<<endl;
32     }
33     return 0;
34 }

B. Garland

两个字符串 代表两串颜色。

下面的要构造的颜色 上面的必须有,求上面的能构造的最大的值。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 using namespace std;
 8 const int maxn = 1000+10;
 9 
10 char a[maxn], b[maxn];
11 int f_a[40], f_b[40];
12 int main()
13 {
14     int i, j;
15     int len_a, len_b, f, ans;
16     while(cin>>a>>b)
17     {
18         f = 0; ans = 0;
19         memset(f_a, 0, sizeof(f_a));
20         memset(f_b, 0, sizeof(f_b));
21         len_a = strlen(a);
22         len_b = strlen(b);
23         for(i = 0; i < len_a; i++)
24         f_a[a[i]-'a']++;
25         for(j = 0; j < len_b; j++)
26         f_b[b[j]-'a']++;
27         for(i = 0; i < 39; i++)
28         {
29             if(f_b[i]>0&&f_a[i]==0)
30             f = 1;
31         }
32         if(f)
33         cout<<ans-1<<endl;
34         else
35         {
36             for(i = 0; i < 39; i++)
37             {
38                 if(f_a[i]>f_b[i])
39                 ans += f_b[i];
40                 else
41                 ans += f_a[i];
42             }
43             cout<<ans<<endl;
44         }
45     }
46     return 0;
47 }

C. Triangle

题意:一个直角三角形,任何边都不和坐标轴平行,给定两个腰长,顶点的坐标为整数。求这个三角形的三个顶点坐标。

可能有很多符合条件的。。

思路:以给定的两个腰长,分别做圆,从左到右枚举每个x整数点, 若y点也为整数,就保存。

最后, 让两个保存的数组,任意组合。找互相垂直,而且不与坐标轴平行的输出。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 using namespace std;
 8 const int maxn = 2000+10;
 9 const double eps = 1e-8;
10 struct node
11 {
12     int x, y;
13 } a[maxn], b[maxn];
14 
15 int main()
16 {
17     int a1, b1, i, j;
18     int aa, bb, cnt_a, cnt_b;
19     double m;
20     int n, f;
21     while(cin>>a1>>b1)
22     {
23         f = 0;
24         aa = a1*a1;
25         bb = b1*b1;
26         cnt_a = 0;
27         cnt_b = 0;
28         for(i = -a1+1; i < a1; i++)
29         {
30             if(i==0)
31                 continue;
32             n = sqrt(aa-i*i);
33             m = (double)(sqrt(aa-i*i));
34             if(fabs(n-m)<eps)
35             {
36                 a[cnt_a].x = i;
37                 a[cnt_a++].y = n;
38             }
39         }
40         for(i = -b1+1; i < b1; i++)
41         {
42             if(i==0)
43                 continue;
44             n = sqrt(bb-i*i);
45             m = (double)(sqrt(bb-i*i));
46             if(fabs(n-m)<eps)
47             {
48                 b[cnt_b].x = i;
49                 b[cnt_b++].y = n;
50             }
51         }
52         //for(i = 0; i < cnt_b; i++)
53         //printf("%d %d
", b[i].x, b[i].y);
54         for(i = 0; i < cnt_a; i++)
55         {
56             for(j = 0; j < cnt_b; j++)
57             {
58                 if(a[i].x*b[j].x+a[i].y*b[j].y==0&&a[i].x!=b[j].x&&a[i].y!=b[j].y)
59                 {
60                     cout<<"YES"<<endl;
61                     printf("%d %d
", a[i].x, a[i].y);
62                     printf("%d %d
", b[j].x, b[j].y);
63 
64                     a1 = 0;
65                     b1 = 0;
66                     printf("%d %d
", a1, b1);
67                     f = 1;
68                     break;
69                 }
70             }
71             if(f)
72                 break;
73         }
74         if(f==0)
75             cout<<"NO"<<endl;
76     }
77     return 0;
78 }

D. Long Path

题意:有n+1个房间,现在人在1号房间,问到 n+1个房间,需要多少步。

每个房间都有两个门,当你进入这个房间的次数是 奇数次的时候走第1个门 进入房间p[i].

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <algorithm>
 7 using namespace std;
 8 const int mo = 1000000000+7;
 9 const int maxn = 1000+10;
10 int p[maxn], d[maxn];
11 
12 int main()
13 {
14     int n, i, j;
15     int ans;
16     while(cin>>n)
17     {
18         memset(d, 0, sizeof(d));
19         for(i = 1; i <= n; i++)
20             cin>>p[i];
21         d[1] = 2;
22         for(i = 2; i <= n; i++)
23         {
24             for(j = p[i]; j < i; j++)
25             {
26                 d[i] += d[j];
27                 d[i] %= mo;
28             }
29             d[i] += 2;
30             d[i] %= mo;
31         }
32         ans = 0;
33         for(i = 1; i <= n; i++)
34         ans = (ans + d[i])%mo;
35         cout<<ans<<endl;
36     }
37     return 0;
38 }
原文地址:https://www.cnblogs.com/bfshm/p/3634384.html