码迷,mamicode.com
首页 > 其他好文 > 详细

Educational Codeforces Round 70 (Rated for Div. 2)

时间:2019-08-10 21:49:16      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:添加   rate   最大   cat   位置   char   namespace   ret   bit   

A:http://codeforces.com/contest/1202/problem/A

思路:找出第二个串最后一个1的位置  在第一个串的这个位置及之前找 第一个1的位置,两个位置相减就是答案.

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e5 + 5;
 4 const int INF = 0x3f3f3f3f;
 5 int n;
 6 int main()
 7 {
 8     int t;
 9     string a, b;
10     cin >> t;
11     while(t--)
12     {
13         cin >> a >> b;
14         reverse(a.begin(),a.end());
15         reverse(b.begin(),b.end());
16         int p = 0;
17         for(int i = 0;i < b.size();i++)
18         {
19             if(b[i] == 1)
20             {
21                 p = i;break;
22             }
23         }
24         int v = 0;
25         for(int i = p;i <= a.size();i++)
26         {
27             if(a[i] == 1)
28             {
29                 v = i;
30                 break;
31             }
32         }
33         cout << v - p <<endl;
34     }
35     return 0;
36 }

B:http://codeforces.com/contest/1202/problem/B

题意:给你一个数字串,问你在相邻两个数字之间最少需要添加几个数字才能使得这个数字串完整;x-y计数器,就是你每次可以让前一个数字加上x或者y,输出10*10的计数器,最少需要添加多少次;

思路:看了大佬的代码和题解 理解了挺久。

转化为最短路来做,每次选择x,y,代表每次可以走x或y的距离,那么可以用弗洛伊德算法处理一下0~9之间的最短路,

再遍历一次数字串,计算每两个数字的最短路,如果走不到就说明在x-y计数器下不能走到,就输出-1;

否者就是全部两两数字之间的和;

AC代码:

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 2e6 + 10;
 4 typedef long long ll;
 5 const int inf = 0x3f3f3f3f;
 6 int dis[10][10];
 7 char s[maxn];
 8 int n;
 9 int fun(int x, int y)
10 {
11     memset(dis, inf, sizeof(dis));
12     for(int i = 0; i < 10;i++)
13     {
14         dis[i][(i + x)%10] = dis[i][( i+ y)% 10]  = 1;
15     }
16     for(int k = 0;k < 10;k++)
17     {
18         for(int i = 0;i < 10;i ++)
19         {
20             for(int j = 0; j < 10;j++)
21             {
22                 dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
23             }
24         }
25     }
26     int ans = 0;
27     for(int i  = 0;i < n - 1;i ++)
28     {
29         if(dis[s[i] - 0][s[i+1] - 0] == inf) return -1;
30         ans += dis[s[i] - 0][s[i+1] - 0] - 1;
31     }
32     return ans;
33 }
34 int main()
35 {
36     cin >> s;
37     n = strlen(s);
38     for(int i = 0;i <10;i ++)
39     {
40         for(int j = 0;j < 10;j++)
41         {
42             cout << fun(i, j) << " ";
43         }
44         cout << endl;
45     }
46     return 0;
47 }

 

C:http://codeforces.com/contest/1202/problem/C

思路:每走一步记录一下当前最大 最小的横坐标,纵坐标,由此得出当前向左向右向上向下都最多移动了多少距离,当相反方向的移动的距离都不为0的时候,且不相等的情况下,可以减去该方向上的面积。

一开始就是这么想的 但是实现的时候出了问题 orz。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        string s;
        cin >> s;
        int cw, cd, ma, md, mw, ms, sa, sd, sw, ss;
        cw = cd = ma = md = mw = ms = sa =sd = sw = ss = 0;
        for(int i = 0;i < s.size(); i++ )
        {
            if(s[i] == W) cw += 1;
            if(s[i] == S) cw -= 1;
            if(s[i] == D) cd += 1;
            if(s[i] == A) cd -= 1;
            mw = max(mw, cw);sw = max(sw, cw - ms);
            ms = min(ms, cw);ss = max(ss, mw - cw);
            md = max(md, cd);sd = max(sd, cd - ma);
            ma = min(ma, cd);sa = max(sa, md - cd);
        }
        ll x[2], y[2];
        x[0] = max(sw, ss);
        x[1] = max(ll(sw or ss), x[0] - !(sw == ss));
        y[0] = max(sd, sa);
        y[1] = max(ll(sd or sa), y[0] - !(sd == sa));
        ll ans = min((x[0] + 1) * (y[1] + 1), (x[1] + 1) * (y[0] + 1));
        cout << ans << endl;
    }
    return 0;
}

D:http://codeforces.com/contest/1202/problem/D

思路:只要考虑在 3 上面加就行 不够就在 3  后面一直加 1 ,然后首尾加个1 和 7。

AC代码:

+

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     int t;
 6     scanf("%d", &t);
 7     while(t--) {
 8         int n;
 9         scanf("%d", &n);
10         string s = "1";
11         int x = 1;
12         while(x * (x - 1) / 2 <= n) {
13             ++x;
14         }
15         --x;
16         s += string(x,3);
17         n -= x * (x - 1) / 2;
18         if(n) {
19             s.pop_back();
20             s.pop_back();
21             s += string(n, 1);
22             s += "33";
23         }
24         s += "7";
25         cout<< s << endl;
26     }
27     return 0;
28 }

 

Educational Codeforces Round 70 (Rated for Div. 2)

标签:添加   rate   最大   cat   位置   char   namespace   ret   bit   

原文地址:https://www.cnblogs.com/Carered/p/11332938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!