Codeforces Round #547 (Div. 3)
生活随笔
收集整理的這篇文章主要介紹了
Codeforces Round #547 (Div. 3)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
E. Superhero Battle
題意
有一個血量,N次循環攻擊,每次可以加血見血。問第幾次可以把血量減到0
思路
如果存在解,必定是N次攻擊中的一次終結,所以可以枚舉終結點的情況,取一個最小值`
#include <bits/stdc++.h> #define LL long long #define P pair<int, int> #include <time.h> #define lowbit(x) (x & -x) #define mem(a, b) memset(a, b, sizeof(a)) #define rep(i, a, n) for (int i = a; i <= n; ++i) const int maxn = 200005; #define mid ((l + r) >> 1) #define lc rt<<1 #define rc rt<<1|1 using namespace std;LL dp[maxn], sum[maxn];int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);LL n, h;while (cin >> h >> n) {for (int i = 1; i <= n; ++i) {cin >> dp[i];sum[i] = dp[i] + sum[i-1];}LL ans = 1e18;for (LL i = 1; i <= n; ++i) {LL last = h + sum[i];if (last <= 0) {ans = min(ans, i);}else {if (sum[n] >= 0) continue;LL cnt = last / (abs(sum[n]));if (last % abs(sum[n])) cnt++;ans = min(ans, cnt * n + i);}}ans = (ans == 1e18) ? -1 : ans;cout << ans << endl;}return 0; }F Same Sum Blocks
題意
長度為n的數組,找一個最多的區間個數,使得每個區間不想交而且區間和相同
思路
暴力枚舉每個子區間,將和相同的區間加到一個vectorvectorvector中,由貪心的思想枚舉的時候從右端點向前,這樣和為相同值,在之前出現過它就是最優的,不用考慮子區間和別的數字結合的情況,最后輸出最長的即可
#include <bits/stdc++.h> #define LL long long #define P pair<int, int> #include <time.h> #define lowbit(x) (x & -x) #define mem(a, b) memset(a, b, sizeof(a)) #define rep(i, a, n) for (int i = a; i <= n; ++i) const int maxn = 2250005; #define mid ((l + r) >> 1) #define lc rt<<1 #define rc rt<<1|1 using namespace std;map<int, vector<P> > all; int a[maxn];int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int n;while (cin >> n) {for (int i = 1; i <= n; ++i) {cin >> a[i];a[i] += a[i-1];}for (int i = 1; i <= n; ++i) {for (int j = i; j >= 1; --j) {int tmp = a[i] - a[j-1];if (all[tmp].size() && all[tmp].back().second >= j) continue;all[tmp].emplace_back(P(j, i));}}vector<P> ans;for (auto it : all) {if (it.second.size() > ans.size()) ans = it.second;}cout << ans.size() << endl;for (auto it : ans) {cout << it.first << " " << it.second << endl; }}return 0; }G Privatization of Roads in Treeland
題意
給樹上的邊染色,一個點上的邊染成相同的顏色這個點就是不合法,最多可以錯K個點,問最少染幾種顏色
思路
- 按照度數從小到大排序,假設當我們滿足度數為1(sum個)的節點不發生沖突,這時我們只需要1種顏色(因為它們度數為1),剩下的還有 n - sum個節點存在沖突。
我們枚舉需要染色的個數,直到剩下的節點≤K\le K≤K,這時所需要的顏色數最小。 - 當我們確定需要的顏色數量,然后dfs進行染色即可(color = (color + 1) % D)
總結
以上是生活随笔為你收集整理的Codeforces Round #547 (Div. 3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2-SAT
- 下一篇: Matrix-Tree (生成树计数)