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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

Codeforces Round #345 (Div. 2)

發布時間:2024/9/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #345 (Div. 2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

DFS?A - Joysticks

嫌麻煩直接DFS暴搜吧,有坑點是當前電量<=1就不能再掉電,直接結束。

#include <bits/stdc++.h>typedef long long ll; const int N = 1e5 + 5; int ans = 0;void DFS(int a, int b, int step) {if (a <= 0 || b <= 0) {ans = std::max (ans, step); return ;}if (a < b && b - 2 >= 0) {DFS (a + 1, b - 2, step + 1);} else if (a >= b && a - 2 >= 0) {DFS (a - 2, b + 1, step + 1);} }int main() {int a, b; std::cin >> a >> b;ans = 0;DFS (a, b, 0);std::cout << ans << '\n';return 0; }

構造 + 貪心?B - Beautiful Paintings

每次取出不重復的遞增序列,直到集合為空

#include <bits/stdc++.h>typedef long long ll; const int N = 1e5 + 5; std::vector<int> vec;int main() {int n; std::cin >> n;for (int x, i=0; i<n; ++i) {std::cin >> x; vec.push_back (x);}std::sort (vec.begin (), vec.end ());int ans = 0;while (vec.size () > 0) {std::vector<int> tmp;int pre = 0, num = 0;for (auto x: vec) {if (x > pre) {num++; pre = x;} else {tmp.push_back (x);}}ans += num - 1; vec = tmp;}std::cout << ans << '\n';return 0; }

數學 + 容斥?C - Watchmen

化簡公式得到找到pair (i, j) xi == xj || yi == yj。找兩次再容斥一下。map做更好。

#include <bits/stdc++.h>typedef long long ll; const int N = 2e5 + 5; const int INF = 1e9 + 7; std::pair<int, int> point[N];ll calc(int n) {return 1ll * n * (n - 1) / 2; }bool cmpx(std::pair<int, int> a, std::pair<int, int> b) {return a.first < b.first; }bool cmpy(std::pair<int, int> a, std::pair<int, int> b) {return a.second < b.second; } int main() {int n; scanf ("%d", &n);for (int x, y, i=0; i<n; ++i) {scanf ("%d%d", &x, &y);point[i] = std::make_pair (x, y);}std::sort (point, point+n, cmpx);ll ans = 0;int x = INF, y = INF, num = 1;for (int i=0; i<n; ++i) {if (x == point[i].first) {num++;} else {x = point[i].first;if (num > 1) {ans += calc (num);}num = 1;}}if (num > 1) {ans += calc (num);}std::sort (point, point+n, cmpy);x = INF; y = INF; num = 1;for (int i=0; i<n; ++i) {if (y == point[i].second) {num++;} else {y = point[i].second;if (num > 1) {ans += calc (num);}num = 1;}}if (num > 1) {ans += calc (num);}std::sort (point, point+n);x = INF; y = INF; num = 1;for (int i=0; i<n; ++i) {if (x == point[i].first && y == point[i].second) {num++;} else {x = point[i].first, y = point[i].second;if (num > 1) {ans -= calc (num);}num = 1;}}if (num > 1) {ans -= calc (num);}printf ("%I64d\n", ans);return 0; }

two points?D - Image Preview

題意:瀏覽圖片,瀏覽,滑動以及反轉需要時間,問在T時間內最多瀏覽多少圖片。

分析:定義兩個指針from,to,可行的方案是n+1->from,from->n+1,n+1->to或者n+1->to,to->n+1,n+1->from,還有from->to。前兩個重復滑動的可以選擇距離小的,第三個只要定義to=n+1就對了。其實可以用二分做的。

#include <bits/stdc++.h>typedef long long ll; const int N = 5e5 + 5; char str[2*N]; ll tim[2*N]; int n, a, b, T;ll get_time(int from, int to) {ll ret = tim[from] - tim[to-1];int move = from - to + std::min (from-(n+1), (n+1)-to);return ret + 1ll * a * move; }int main() {scanf ("%d%d%d%d", &n, &a, &b, &T);scanf ("%s", str + 1);for (int i=1; i<=n; ++i) {tim[i] = (str[i] == 'w' ? (b+1) : 1);}for (int i=n+1; i<=2*n; ++i) {tim[i] = tim[i-n];}for (int i=1; i<=2*n; ++i) {tim[i] += tim[i-1];}int ans = 0;int from = n + 1;for (int to=2; to<=n+1; ++to) {while (from < to+n-1 && get_time (from+1, to) <= T) from++;if (get_time (from, to) <= T) ans = std::max (ans, from - to + 1);}printf ("%d\n", ans);return 0; }

圖論 + 并查集?E - Table Compression

題意:給一個矩陣,求新的矩陣,使得原矩陣的同行,同列的大小關系不變,且使得新矩陣的最大值最小。即離散化

分析:找好順序,一個一個從小到大填充就行了。因為有相同數字的存在,使用并查集使得將相同的數字賦值離散化后相同的數字。因為有行和列共同影響一個數字,所以建立一個大數到小數的圖,類似樹形DP,選擇當前行或列的最大值+1為離散化后的數字

#include <bits/stdc++.h>const int N = 1e6 + 5; std::pair<int, std::pair<int, int> > A[N]; int val[N], ans[N]; int mr[N], mc[N]; int rt[N]; std::vector<int> edge[N]; std::vector<int> num[N];int Find(int x) {return rt[x] == x ? x : rt[x] = Find (rt[x]); }int main() {int n, m; scanf ("%d%d", &n, &m);int tot = n * m;for (int i=0; i<n; ++i) {for (int j=0; j<m; ++j) {scanf ("%d", &val[i*m+j]);A[i*m+j].first = val[i*m+j];A[i*m+j].second = std::make_pair (i, j);rt[i*m+j] = i * m + j;}}std::fill (mr, mr+n, -1);std::fill (mc, mc+m, -1);std::sort (A, A+tot);for (int i=0; i<tot; ++i) {int r = A[i].second.first, c = A[i].second.second;int pos = r * m + c;if (mr[r] != -1) {if (val[mr[r]] == A[i].first) {rt[pos] = Find (mr[r]);} else {edge[pos].push_back (mr[r]);}}if (mc[c] != -1) {if (val[mc[c]] == A[i].first) {rt[Find (pos)] = Find (mc[c]);} else {edge[pos].push_back (mc[c]);}}mr[r] = mc[c] = pos;}for (int i=0; i<tot; ++i) {num[Find (i)].push_back (i);}for (int i=0; i<tot; ++i) {int r = A[i].second.first, c = A[i].second.second;int id = r * m + c;if (Find (id) == id) {int _max = 0;for (auto u: num[id]) {for (auto v: edge[u]) {_max = std::max (_max, ans[v]);}}for (auto u: num[id]) {ans[u] = _max + 1;}}}for (int i=0; i<n; ++i) {for (int j=0; j<m; ++j) {printf ("%d ", ans[i*m+j]);}puts ("");}return 0; }

  

?

轉載于:https://www.cnblogs.com/Running-Time/p/5258127.html

總結

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

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