Codeforces Round #387 (Div. 2)

这次比赛的感受:一个字,燃!

许久没打CF了,比赛的时候一下子出了4道题,还是挺激动。。。尽管C题最后挂了TAT。

以下是题解:

—————————————————————————————————分割线——————————————————————————————

第一题: Display Size (http://codeforces.com/contest/747/problem/A)

签到题,找到根号为中间数然后往两边找即可,代码:

#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;


int main() {
    int n;
    cin >> n;
    int temp = (int)sqrt(n * 1.0);
    int i = temp, j = temp;
    while (i >= 1) {
        if (i * j == n) {
            cout << i << " " << j << endl;
            break;
        } else if (i * j > n){
            --i;
        } else {
            ++j;
        }
    }
    return 0;
}


第二题: Mammoth's Genome Decoding (http://codeforces.com/problemset/problem/747/B)

感觉比第一题还要水啊。。。

代码:

#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;


int main() {
    int n;
    cin >> n;
    string str;
    cin >> str;
    if (n % 4 != 0) {
        cout << "===" << endl;
        return 0;
    }
    int subWant = n / 4;
    int a[5] = {0};
    for (auto ch : str) {
        if (ch == 'A') {
            ++a[0];
        } else if (ch == 'C') {
            ++a[1];
        } else if (ch == 'T') {
            ++a[2];
        } else if (ch == 'G') {
            ++a[3];
        } else {
            ++a[4];
        }
    }
    if (a[0] > subWant || a[1] > subWant || a[2] > subWant || a[3] > subWant) {
        cout << "===" << endl;
    } else {
        string ans = "";
        int num1 = subWant - a[0];
        int num2 = subWant - a[1];
        int num3 = subWant - a[2];
        int num4 = subWant - a[3];
        for (auto ch : str) {
            if (ch != '?') {
                ans += ch;
            } else {
                if (num1) {
                    ans += 'A';
                    --num1;
                } else if (num2) {
                    ans += 'C';
                    --num2;
                } else if (num3) {
                    ans += 'T';
                    --num3;
                } else if (num4) {
                    ans += 'G';
                    --num4;
                }
            }
        }
        cout << ans << endl;
    }
    return 0;
}


第三题: Servers (http://codeforces.com/contest/747/problem/C)

最后挂了。。。代码还是不够严谨啊!

坑在于查找的时候要先查找再更新servers,有一定几率不更新severs。

代码:

#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int findServer(vector<int>& servers, int t, int k, int d) {
    int size = (int)servers.size();
    if (size < k) {
        return -1;
    } else {
        int ans = 0;
        int counts = 0;
        for (int i=0; i<size && counts < k; ++i) {
            if (servers[i] <= t) {
            	// You need to update the servers when you are sure you need to update
                ++counts;
            }
        }
        if (counts == k) {
        	counts = 0;
        	for (int i=0; i<size && counts < k; ++i) {
        		if (servers[i] <= t) {
        			ans += i + 1;
            		servers[i] = t + d;
            		++counts;
            	}
            }
        	return ans;
        }
        else return -1;
    }
}

int main() {
    int n, q;
    cin >> n >> q;
    vector<int> servers(n, 0);
    while (q--) {
        int t, k, d;
        cin >> t >> k >> d;
        int ans = findServer(servers, t, k, d);
        cout << ans << endl;
    }
    return 0;
}


第四题: Winter Is Coming (http://codeforces.com/problemset/problem/747/D)

本质也是水题,

First, 我们要明白小于零下的肯定要换成冬天轮胎。

我的思路是:

先省着点用冬天轮胎,能用夏天轮胎一定用夏天轮胎,得到一个初步的answer。

然后剩余的冬天轮胎去插空那些温度大于0的日子,没插空完全一次,把answer - 2。

需要注意的是,如果最后的日子天气全部都是大于0,我们把后面的日子全部插空完全,也只能得到answer-1。

所以这里要分类讨论是否去插空后面的天气。

代码如下:

#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;

int main() {
    int n, k;
    cin >> n >> k;
    int status = 0;
    int ans = 0;
    int coldCounts = 0;
    vector<int> whether(n);
    int index = 0;
    int size = n;
    while (size--) {
        int temp;
        cin >> temp;
        whether[index++] = temp;
        if (temp < 0) ++coldCounts;
        
        if (temp < 0 && status == 0) {
            ++ans;
            status = 1;
        } else if (temp >= 0 && status == 1) {
            ++ans;
            status = 0;
        }
    }
    if (coldCounts == 0) cout << 0 <<endl;
    else if (k == n) cout << 1 << endl;
    else if (coldCounts > k) cout << -1 << endl;
    else {
        int remain = k - coldCounts;
        if (remain == 0) {
            cout << ans << endl;
            return 0;
        }
        vector<int> next(n+1, -1);
        whether.push_back(-1);
        int lastIndex = -1;
        for (int i=0; i<n+1; ++i) {
            if (whether[i] >= 0) {
                continue;
            } else {
                if (lastIndex == -1) {
                    lastIndex = i;
                } else {
                    next[i] = i - lastIndex - 1;
                    lastIndex = i;
                }
            }
        }
        int ans1 = ans, ans2 = ans;
        int tempremain = remain;
        if (next[n] > 0 && tempremain >= next[n]) {
            tempremain -= next[n];
            ans1 -= 1;
            sort(next.begin(), next.end()-1);
            for (int i=0; i<n; ++i) {
                if (next[i] == -1 || next[i] == 0) {
                    continue;
                } else {
                    if (tempremain >= next[i]) {
                        ans1 = ans1 - 2;
                        tempremain -= next[i];
                    } else {
                        break;
                    }
                }
            }
        }
        sort(next.begin(), next.end()-1);
        for (int i=0; i<n; ++i) {
            if (next[i] == -1 || next[i] == 0) {
                continue;
            } else {
                if (remain >= next[i]) {
                    ans2 = ans2 - 2;
                    remain -= next[i];
                } else {
                    break;
                }
            }
        }
        ans = min(ans1, ans2);
        cout << ans << endl;
    }
    return 0;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值