日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Round #547 (Div. 3)

發布時間:2024/4/18 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 KK,這時所需要的顏色數最小。
  • 當我們確定需要的顏色數量,然后dfs進行染色即可(color = (color + 1) % D)
#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;int D; int degree[maxn]; vector<int> g[maxn]; map<int, int> cnt; map<P, int> mp; int ans[maxn];void dfs(int x, int fa, int f) {int color = 0;for (auto it : g[x]) {if (it == fa) continue;if (color == f) {color = (color + 1) % D;}ans[mp[P(x, it)]] = color;dfs(it, x, color);color = (color + 1) % D;} }int main() {ios::sync_with_stdio(false);cin.tie(0); cout.tie(0);int n, k;cin >> n >> k;for (int i = 1, l, r; i < n; ++i) {cin >> l >> r;degree[l]++;degree[r]++;g[l].push_back(r);g[r].push_back(l);mp[P(l, r)] = i; // 記錄編號mp[P(r, l)] = i;}for (int i = 1; i <= n; ++i) {cnt[degree[i]]++; // 統計相同度數的節點個數}int unfair = n; // 需要的顏色個數for (auto it : cnt) {if (unfair > k) {D = it.first;unfair -= it.second;}}dfs(1, 0, -1); cout << D << endl;for (int i = 1; i < n; ++i) {cout << ans[i]+1 << " ";}cout << endl;return 0; }

總結

以上是生活随笔為你收集整理的Codeforces Round #547 (Div. 3)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。