Codeforces Round #786 (Div. 3)

A. Number Transformation

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
//const int N = 

int t;
int x, y;

int main()
{
    cin >> t;
    while (t --)
    {
        int a, b;
        cin >> x >> y;
        if (x > y) cout << "0" << " 0" << endl;
//        else if (x == 0 || y == 0) cout << "0" << " 0" << endl;
        else if (x == y) cout << "1 " << "1" << endl;
        else if (y % x == 0)
        {
            int s = y / x;
            a = 1;
            b = s;
            cout << a << " " << b << endl;
        }
        else cout << "0" << " 0" << endl;
    }
    
    return 0;
}

B. Dictionary

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int t;

int main()
{
    cin >> t;
    while (t --)
    {
        char a[4];
        cin >> a;
        int s = a[0] - 'a';
        int sum = 0;
        sum = s * 25;
        if (a[1] > a[0])
            s = a[1] - 'a';
        else s = a[1] - 'a' + 1;
        sum = sum + s;
        cout << sum << endl;
    }
    
    return 0;
}

C. Infinite Replacement

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long int ll;

int t;

void add()
{
    string a, b;
        cin >> a >> b;
        int l = a.length();
        int s = 0;
        for (auto c:b)
        {
            if (c == 'a') s = 1;
        }
        if (s && b.size() == 1)
        {
            cout << "1" << endl;
            return; 
        }
        if (s && b.size() != 1)
        {
            cout << "-1" << endl;
            return ;
        } 
        ll ans = (1ll << l);
        cout << ans << endl;
}

int main()
{
    cin >> t;
    while (t --)
    {
        add();
    }
    
    return 0;
}

D. A-B-C Sort

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#define int long long 
using namespace std;
const int N = 2e5 + 20;

int t, n;
int a[N];

void solve()
{
    cin >> n;
    int minn = 0x3f3f3f3f;
    for (int i = 1; i <= n; i ++) cin >> a[i], minn = min(minn, a[i]);
    
    int s = 1;
    if (n & 1) if (a[1] != minn) s = 0;
    for (int i = n - 1; i >= 1; i -= 2)
    {
        if (a[i] < a[i - 1] || a[i] < a[i - 2]) s = 0;
        if (a[i + 1] < a[i - 1] || a[i + 1] < a[i - 2]) s = 0;
        if (!s) break;
    }
    if (s) cout << "YES" << endl;
    else cout << "NO" << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> t;
    while (t -- )
    {
        solve();
    }
    
    return 0;
}

E. Breaking the Wall

思路:

1.直接攻击血量最小的两块围墙

2.对于相邻的两块围墙 , 设 max 为其中血量较多的那一块, min为血量较少的那一块,

如果 max≥min×2 则我们只要攻击 [max/2] 次即可

否则 我们要攻击 [(max+min+2)/3] 次即可

3.对于两块相隔 1 的围墙, 只要攻击中间的围墙 即可

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#define int long long 
using namespace std;
const int N = 1e6 + 20;

int t, n, ans;
int a[N];

void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    
    ans = 0x3f3f3f3f;
    for (int i = 1; i < n; i ++)
    {
        // 两个挨着的情况 :  
        int x = (a[i] + 1) / 2, y = (a[i + 1] + 1) / 2;
        if (x >= a[i + 1] || y >= a[i]) ans = min(ans, max(x, y));
        else{
            if ((a[i] + a[i + 1]) % 3 == 0) ans = min(ans, (a[i] + a[i + 1]) / 3);
            else ans = min(ans, (a[i] + a[i + 1]) / 3 + 1);
        }
        
        
    }
    
    // 中间隔一个的情况 :
    for (int i = 1; i + 2 <= n; i ++)
    {
        ans = min(ans, min(a[i], a[i + 2]) + (max(a[i] , a[i + 2]) - min(a[i], a[i + 2]) + 1) / 2);
    }
    
//    cout << ans << endl;
    
    sort(a + 1, a + 1 + n);
    ans = min(ans, ((a[1] + 1) / 2) + (a[2] + 1) / 2);
    
    cout << ans << endl;
}

signed main()
{
//    cin >> t;
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    t = 1;
    while (t -- )
    {
        solve();
    }
    return 0;
}

F. Desktop Rearrangement

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define int long long
using namespace std;
const int N = 1e3 + 20;

int t, n, m, q;
string s[N];
int cnt[N];

void solve()
{
    cin >> n >> m >> q;
    memset(cnt, 0, sizeof cnt);
    for (int i = 0; i < n; i ++) cin >> s[i];
    int sum = 0;
    for (int i = 0; i < n; i ++)
    {
        for (int j = 0;j < m; j ++)
        {
            if (s[i][j] == '*')
            {
                cnt[j] ++;
                sum ++;
            }
        }
    }
    
    while (q -- )
    {
        int x, y;
        cin >> x >> y;
        x --, y --;
        if (s[x][y] == '*') sum --, cnt[y] --, s[x][y] = '.';
        else sum ++, cnt[y] ++, s[x][y] = '*';
        
        int l = sum / n, r = sum - n * l;
        int res = 0;
        for (int i = 0; i < l; i ++) res += cnt[i];
        for (int i = 0; i < r; i ++)
        {
            if (s[i][l] == '*') res ++;
        }
        
        cout << sum - res << endl;
    }
}

signed main()
{
//    cin >> t;
    t = 1;
    while (t -- )
    {
        solve();
    }
    
    return 0;
}
/*
    原题链接:(1800)
    https://codeforces.com/problemset/problem/1674/F
*/

G. Remove Directed Edges

思路:

使用记忆化搜索和DP。有分叉的两个叶子结点必然不会满足条件。所以S中只会有一条链。问题就变成了DAG图中求最长链了。不过中途转移时要判断一下子节点的入度是否大于等于2,如果等于1的话我们就需要删除这条边(存在的话就需要至少删除一条边)那么就不能从这个子节点转移过来了。

代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#define int long long 
using namespace std;
const int N = 2e5 + 20; 

int t, ans = 1, n, m;
int in[N], out[N], dp[N];
int h[N], ne[N], e[N], idx;
vector<int> q[N + 1];

void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}

void dfs(int x)
{
    if (dp[x]) return ;
    dp[x] = 1;
    if (out[x] < 2) return ;
    for (int i = h[x]; ~i; i = ne[i])
    {
        int j = e[i];
        dfs(j);
        if (in[j] >= 2) dp[x] = max(dp[x], dp[j] + 1);
    }
}

void solve()
{
    cin >> n >> m;
    
    memset(h, -1, sizeof h);
    for (int i = 1; i <= m; i ++)
    {
        int x, y;
        cin >> x >> y;
        //        q[x].push_back(y);
        add(x, y);
        in[y] ++, out[x] ++;
    }
    
    for (int i = 1; i <= n; i ++) dfs(i);
    //    cout << "+++" << endl;
    for (int i = 1; i <= n; i ++) ans = max(ans, dp[i]);
    
    cout << ans << endl;
}

signed main()
{
    //    cin >> t;
    t = 1;
    while (t -- )
    {
        solve();
    }
    return 0;
}
/*
    原题链接:
    https://codeforces.com/contest/1674/problem/G
*/

提交记录

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值