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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Round #506 (Div. 3) - C. Maximal Intersection (思维,模拟)

發(fā)布時(shí)間:2024/4/18 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #506 (Div. 3) - C. Maximal Intersection (思维,模拟) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接

題意

給你N個(gè)線段(一條直線上),問刪去一個(gè)之后,最長公共長度

AC

  • 先考慮不刪邊,有兩種情況
  • 所有直線在一起分布

    顯然公共部分是排序之后的(第一個(gè)右端點(diǎn) - 最后一個(gè)左端點(diǎn))
  • 直線不在一起分布

    這時(shí)按照(第一個(gè)右端點(diǎn) - 最后一個(gè)左端點(diǎn))得到的結(jié)果是負(fù)數(shù),最后可以加個(gè)判斷,可以解決這個(gè)情況
  • 知道上面的兩種情況后,可以在O(1)的時(shí)間求出最長的公共部分,將所有的線段按照左端點(diǎn)升序,右端點(diǎn)升序,然后模擬刪邊,O(n)
// 數(shù)組模擬 #include <iostream> #include <cmath> #include <map> #include <vector> #include <set> #include <algorithm> #define ll long long #define N 300005 using namespace std;int l[N], r[N]; struct seg{int l, r; }a[N]; int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endif int n;while (scanf("%d", &n) != EOF) {for (int i = 0; i < n; ++i) {scanf("%d%d", &a[i].l, &a[i].r);l[i] = a[i].l;r[i] = a[i].r;}sort(l, l + n);sort(r, r + n);int ans = 0; // 初始為0,避免負(fù)數(shù) for (int i = 0; i < n; ++i) { int end_l = l[n - 1]; // 最后一個(gè)左端點(diǎn) int begin_r = r[0]; // 第一個(gè)右端點(diǎn) if (end_l == a[i].l) end_l = l[n - 2]; // 如果刪邊為最后一個(gè)左端點(diǎn),左移一位 if (begin_r == a[i].r) begin_r = r[1]; // 如果刪邊為第一個(gè)右端點(diǎn),右移一位 int len = begin_r - end_l;ans = max(ans, len);}printf("%d\n", ans);}return 0; } // multiset #include <iostream> #include <cmath> #include <map> #include <vector> #include <set> #include <algorithm> #define ll long long #define N 300005 using namespace std;int l[N], r[N]; multiset<int> l_, r_; int main() { #ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin); #endif int n;while (scanf("%d", &n) != EOF) {for (int i = 0; i < n; ++i) {scanf("%d%d", &l[i], &r[i]);l_.insert(l[i]);r_.insert(r[i]);}int ans = 0;for (int i = 0; i < n; ++i) {// 刪除當(dāng)前邊 l_.erase(l_.find(l[i]));r_.erase(r_.find(r[i]));// 找到最大值 ans = max(ans, *(r_.begin()) - *(--l_.end()));// 撤銷刪除 l_.insert(l[i]);r_.insert(r[i]);}printf("%d\n", ans);l_.clear();r_.clear();}return 0; } 與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Codeforces Round #506 (Div. 3) - C. Maximal Intersection (思维,模拟)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。