Codeforces Round #787 (Div. 3)

A. Food for Animals(模拟)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;

int main()
{
    int T; cin >> T;
    while (T --)
    {
        int a, b, c, x, y;
        cin >> a >> b >> c >> x >> y;
        if (x > a) c -= (x - a);
        if (y > b) c -= (y - b);
        if (c >= 0) puts ("YES");
        else puts ("NO");
    }
    return 0;
}

B.Make It Increasing(贪心)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n;

int main()
{
    int T; cin >> T;
    while (T --)
    {
        cin >> n;
        for (int i = 1; i <= n; i ++) cin >> a[i];
        int cnt = 0;
        int flag = 0;
        for (int i = n - 1; i >= 1; i --)
        {
            if (a[i] < a[i + 1]) continue;
            while (a[i] >= a[i + 1])
            {
                a[i] /= 2;
                cnt ++;
                if (a[i] == 0 && a[i] >= a[i + 1])
                {
                    flag = 1;
                    break;
                }
            }
        }
        if (flag) puts ("-1");
        else cout << cnt << "\n";
    }
    return 0;
}

C. Detective Task(思维)

说谎的那个人满足的条件是:说谎的人的前面的人不会出现0,后面的人不会出现1,用两个数组维护一下位置i前是否出现0,和位置i后是否出现1即可。

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
 
const int N = 2e5 + 10;
int pre[N], las[N];
char s[N];
 
int main ()
{
    int T; cin >> T;
    while (T --)
    {
        cin >> s + 1;
        int len = strlen (s + 1);
        memset (pre, 0, 4 * (len + 3));
        memset (las, 0, 4 * (len + 3));
        int fir = 0;
        for (int i = 1; i <= len; i ++)
        {
            pre[i] = fir | pre[i - 1];
            if (s[i] == '0')
                fir = 1;
        }
        int back = 0;
        for (int i = len; i >= 1; i --)
        {
            las[i] = back | las[i + 1];
            if (s[i] == '1')
                back = 1;
        }
        int res = 0;
        for (int i = 1; i <= len; i ++)
            if (pre[i] == 0 && las[i] == 0)
                res ++;

        cout << res << "\n";
    }
    return 0;
}

D. Vertical Paths(贪心 + 搜索)

每次从父亲节点搜到叶节点即可

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;
const int N = 2e5 +10, M = N * 2;
int h[N], e[M], ne[M], idx;
int st[N];
int n;
vector <int> q[N];
int cnt;
void add(int a, int b)  // 添加一条边a->b
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
void dfs (int u, int fa)
{
    if (fa == -1 || st[fa])
        q[++ cnt].push_back (u);
    else
    {
        q[cnt].push_back (u);
        st[fa] = 1;
    }
    for (int i = h[u]; i != -1; i = ne[i])
        dfs (e[i], u);
}

int main ()
{
    int T; cin >> T;
    while (T --)
    {
        cin >> n;
        cnt = 0;
        idx = 0;
        memset(h, -1, 4 * (n + 2));
        memset (st, 0, 4 * (n + 2));
        for (int i = 1; i <= n; i ++) q[i].clear ();
        int root;
        for (int i = 1; i <= n; i ++)
        {
            int x; cin >> x;
            if (x == i) root = x;
            else add (x, i);
        }
        dfs (root, -1);
        cout << cnt << "\n";
        for (int i = 1; i <= cnt; i ++) 
        {
            cout << q[i].size () << "\n";
            for (int j = 0; j < (int)q[i].size (); j ++)
                cout << q[i][j] << " ";
            cout << "\n";
        }
        cout << "\n";
    }
}

E. Replace With the Previous, Minimize (贪心)

从前往后枚举,优先让前面的字典序最小即可(如果X可以变为a,那么比X小的字符都能变成a)
存在两种情况具体见代码:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 2e5 +10;
char s[N];

int main ()
{
    int T; cin >> T;
    while (T --)
    {
        int n, k; cin >> n >> k;
        cin >> s;
        int maxm = 0;
        for (int i = 0; i < n; i ++)
        {
        	//情况2
            if (s[i] - 'a' > k)
            {
                k = k - maxm;
                char r = s[i];
                char l = s[i] - k;
                for (int j = 0; j < n; j ++)
                    if (s[j] >= l && s[j] <= r) s[j] = l;
                break;
            }
            //情况1
            maxm = max (maxm, s[i] - 'a');
        }
        for (int i = 0; i < n; i ++)
            if (s[i] <= 'a' + maxm) s[i] = 'a';
        cout << s << '\n';
    }
}

F. Vlad and Unfinished Business (贪心)

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
vector<int> a[N], q;
int parent[N], st[N], v[N];
 
int main()
{
    int T; cin >> T;
    while (T --)
    {
        int n, k, x, y;
        cin >> n >> k >> x >> y;
        for (int i = 1; i <= k; i ++) cin >> v[i];
        for (int i = 0; i <= n; i ++) a[i].clear ();
        
        for (int i = 1; i < n; i ++)
        {
            int xx, yy; cin >> xx >> yy;
            a[xx].push_back (yy);
            a[yy].push_back (xx);
        }
        q.clear();
        q.push_back(x);
        memset (st, 0, 4 * (n + 1));
        st[x] = 1;
        parent[x] = -1;
        
        for (int i = 0; i < (int)q.size (); i ++)
        {
            int xx = q[i];
            for (int j = 0; j < (int)a[xx].size (); j ++)
            {
                int yy = a[xx][j];
                if (!st[yy])
                {
                    st[yy] = 1;
                    q.push_back (yy);
                    parent[yy] = xx;
                }
            }
        }
        memset(st, 0, 4 * (n + 1));
        int res = 0;
        st[y] = 1;
        while (y!=x)
        {
            y = parent[y];
            st[y] = 1;
            res ++;
        }
       for (int i = 1; i <= k; i ++)
        {
            int tmp = v[i];
            while (st[tmp] == 0)
            {
                st[tmp] = 1;
                res += 2;
                tmp = parent[tmp];
            }
        }
        cout << res << "\n";
    }
 return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值