• [BC冠军赛(online)]小结


    Movie

    题意:给你n个区间,判断能否选出3个不相交的区间。

    思路:令f(i)表示能否选出两个不相交区间并且以区间i为右区间的值,g(i)表示能否选出两个不相交区间并且以区间i为左区间的值,如果存在i,f(i) && g(i)== true,则存在这样的3个不相交区间。计算f数组的时候,只要从前往后和从后往前各扫一遍,表示对于i而言,另一个区间来自于它的前面(后面),同时维护右边界的最小值(因为只需关心前面(后面)的所有区间的右边界的最小值)。对于g数组类似处理。

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2 
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <set>
     16 #include <bitset>
     17 #include <functional>
     18 #include <numeric>
     19 #include <stdexcept>
     20 #include <utility>
     21 
     22 using namespace std;
     23 
     24 #define mem0(a) memset(a, 0, sizeof(a))
     25 #define mem_1(a) memset(a, -1, sizeof(a))
     26 #define lson l, m, rt << 1
     27 #define rson m + 1, r, rt << 1 | 1
     28 #define define_m int m = (l + r) >> 1
     29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     33 #define all(a) (a).begin(), (a).end()
     34 #define lowbit(x) ((x) & (-(x)))
     35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     38 #define pchr(a) putchar(a)
     39 #define pstr(a) printf("%s", a)
     40 #define sstr(a) scanf("%s", a)
     41 #define sint(a) scanf("%d", &a)
     42 #define sint2(a, b) scanf("%d%d", &a, &b)
     43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
     44 #define pint(a) printf("%d
    ", a)
     45 #define test_print1(a) cout << "var1 = " << a << endl
     46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
     48 
     49 typedef long long LL;
     50 typedef pair<int, int> pii;
     51 typedef vector<int> vi;
     52 
     53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
     54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
     55 const int maxn = 1e7 + 7;
     56 const int md = 10007;
     57 const int inf = 1e9 + 7;
     58 const LL inf_L = 1e18 + 7;
     59 const double pi = acos(-1.0);
     60 const double eps = 1e-6;
     61 
     62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     65 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     67 int make_id(int x, int y, int n) { return x * n + y; }
     68 
     69 struct Node {
     70     unsigned int l, r;
     71     bool operator < (const Node &that) const {
     72         return l < that.l || l == that.l && r < that.r;
     73     }
     74 };
     75 Node node[maxn];
     76 unsigned int n, l, r, a, b, c, d;
     77 bool f1[maxn], f2[maxn], g1[maxn], g2[maxn];
     78 int main() {
     79     //freopen("in.txt", "r", stdin);
     80     int T;
     81     cin >> T;
     82     while (T--) {
     83         cin >> n >> l >> r >> a >> b >> c >> d;
     84         node[1].l = l;
     85         node[1].r = r;
     86         for (int i = 2; i <= n; i++) {
     87             node[i].l = node[i - 1].l * a + b;
     88             node[i].r = node[i - 1].r * c + d;
     89         }
     90         rep_up1(i, n) {
     91             if (node[i].l > node[i].r) swap(node[i].l, node[i].r);
     92         }
     93         mem0(f1);
     94         mem0(f2);
     95         mem0(g1);
     96         mem0(g2);
     97         unsigned int min_r = 0xffffffff;
     98         rep_up1(i, n) {
     99             f1[i] = node[i].l > min_r;
    100             min_update(min_r, node[i].r);
    101         }
    102         min_r = 0xffffffff;
    103         rep_down1(i, n) {
    104             f2[i] = node[i].l > min_r;
    105             min_update(min_r, node[i].r);
    106         }
    107         unsigned int max_l = 0;
    108         rep_up1(i, n) {
    109             g1[i] = node[i].r < max_l;
    110             max_update(max_l, node[i].l);
    111         }
    112         max_l = 0;
    113         rep_down1(i, n) {
    114             g2[i] = node[i].r < max_l;
    115             max_update(max_l, node[i].l);
    116         }
    117         bool ok = false;
    118         rep_up1(i, n) {
    119             ok = ok || (f1[i] || f2[i]) && (g1[i] || g2[i]);
    120             if (ok) break;
    121         }
    122         puts(ok? "YES" : "NO");
    123     }
    124     return 0;
    125 }
    View Code

    Exploration

    题意:判断一个有有向边和无向边的图是否存在环。每条边只能走一次,可能有重边,但没有自环。

    思路:对于无向边连接的点可以缩为一个点,原来的点上的有向边连到缩成的点上。实际上就是判断用缩成的点建成的有向图是否存在环。并查集+dfs即可。

      1 #pragma comment(linker, "/STACK:1024000000,1024000000")
      2 
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <set>
     16 #include <bitset>
     17 #include <functional>
     18 #include <numeric>
     19 #include <stdexcept>
     20 #include <utility>
     21 
     22 using namespace std;
     23 
     24 #define mem0(a) memset(a, 0, sizeof(a))
     25 #define mem_1(a) memset(a, -1, sizeof(a))
     26 #define lson l, m, rt << 1
     27 #define rson m + 1, r, rt << 1 | 1
     28 #define define_m int m = (l + r) >> 1
     29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     33 #define all(a) (a).begin(), (a).end()
     34 #define lowbit(x) ((x) & (-(x)))
     35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     38 #define pchr(a) putchar(a)
     39 #define pstr(a) printf("%s", a)
     40 #define sstr(a) scanf("%s", a)
     41 #define sint(a) scanf("%d", &a)
     42 #define sint2(a, b) scanf("%d%d", &a, &b)
     43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
     44 #define pint(a) printf("%d
    ", a)
     45 #define test_print1(a) cout << "var1 = " << a << endl
     46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
     48 
     49 typedef long long LL;
     50 typedef pair<int, int> pii;
     51 typedef vector<int> vi;
     52 
     53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
     54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
     55 const int maxn = 1e6 + 7;
     56 const int md = 10007;
     57 const int inf = 1e9 + 7;
     58 const LL inf_L = 1e18 + 7;
     59 const double pi = acos(-1.0);
     60 const double eps = 1e-6;
     61 
     62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     65 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     67 int make_id(int x, int y, int n) { return x * n + y; }
     68 
     69 struct UnionFindSets {
     70     vi f;
     71     int N;
     72     UnionFindSets() { f.clear(); }
     73     void clear() { f.clear(); }
     74     void resize(int n) { N = n + 2; f.resize(n + 5); }
     75     void Init() { for (int i = 1; i <= N; i++) f[i] = i; }
     76     int get(int u) { if (u == f[u]) return u; return f[u] = get(f[u]); }
     77     void add(int u, int v) { f[get(u)] = get(v); }
     78     bool find(int u, int v) { return get(u) == get(v); }
     79 };
     80 
     81 
     82 template<class edge> struct Graph {
     83     vector<vector<edge> > adj;
     84     Graph(int n) { adj.clear(); adj.resize(n + 5); }
     85     Graph() { adj.clear(); }
     86     void resize(int n) { adj.resize(n + 5); }
     87     void add(int s, edge e){ adj[s].push_back(e); }
     88     void del(int s, edge e) { adj[s].erase(find(iter(adj[s]), e)); }
     89     void clear() { adj.clear(); }
     90     vector<edge>& operator [](int t) { return adj[t]; }
     91 };
     92 Graph<int> G;
     93 UnionFindSets ufs;
     94 int vis[maxn], mark[maxn];
     95 bool ok;
     96 
     97 void dfs(int pos) {
     98     vis[pos] = -1;
     99     int sz = G[pos].size();
    100     rep_up0(i, sz) {
    101         int u = G[pos][i];
    102         if (vis[u] == -1) {
    103             ok = true;
    104             return ;
    105         }
    106         if (!ok && !vis[u]) dfs(u);
    107     }
    108     vis[pos] = 1;
    109 }
    110 int main() {
    111     //freopen("in.txt", "r", stdin);
    112     int T, n, m1, m2;
    113     cin >> T;
    114     while (T--) {
    115         cin >> n >> m1 >> m2;
    116         G.clear();
    117         G.resize(n);
    118         ufs.clear();
    119         ufs.resize(n);
    120         ufs.Init();
    121         ok = false;
    122         rep_up0(i, m1) {
    123             int u, v;
    124             sint2(u, v);
    125             if (ufs.find(u, v)) {
    126                 ok = true;
    127             }
    128             ufs.add(u, v);
    129         }
    130         rep_up0(i, m2) {
    131             int u, v;
    132             sint2(u, v);
    133             G.add(ufs.get(u), ufs.get(v));
    134         }
    135         mem0(mark);
    136         rep_up1(i, n) {
    137             if (mark[ufs.get(i)]) continue;
    138             mark[ufs.get(i)] = 1;
    139             G.add(0, ufs.get(i));
    140         }
    141         mem0(vis);
    142         if (!ok) dfs(0);
    143         puts(ok? "YES" : "NO");
    144     }
    145     return 0;
    146 }
    View Code

    GCD

    题意:给m个询问Li, Ri, Pi,表示[Li, Ri]的所有数的gcd等于Pi,求原来的数组(所有数的和小的优先)。

    思路:对于数组中某个位置上的数a[x],考虑所有覆盖位置x的区间,那么a[x]是所有这些区间的Pi的最小公倍数的倍数,这是显然的,因为a[x]必须是每个Pi的倍数,不妨先把a数组取个最小值,令a[x] = lcm(Pi)(Li<=x<=Ri)。另一方面,对于某一个区间[Li, Ri]而言,因为这里的a[Li]~a[Ri]都是Pi的倍数了,所以gcd(a[Li], a[Li + 1] ,..., a[Ri])>=Pi,而如果gcd(a[Li]~a[ri])>Pi,则无论怎样调整a数组的值(注意:调整只能整倍的放大),gcd(a[Li]~R[i])始终大于Pi,故无解。如果gcd(a[Li]~a[Ri])=Pi了,a数组里的值就是答案,因为不能再小了。

      1 #pragma comment(linker, "/STACK:10240000,10240000")
      2 
      3 #include <iostream>
      4 #include <cstdio>
      5 #include <algorithm>
      6 #include <cstdlib>
      7 #include <cstring>
      8 #include <map>
      9 #include <queue>
     10 #include <deque>
     11 #include <cmath>
     12 #include <vector>
     13 #include <ctime>
     14 #include <cctype>
     15 #include <set>
     16 #include <bitset>
     17 #include <functional>
     18 #include <numeric>
     19 #include <stdexcept>
     20 #include <utility>
     21 
     22 using namespace std;
     23 
     24 #define mem0(a) memset(a, 0, sizeof(a))
     25 #define mem_1(a) memset(a, -1, sizeof(a))
     26 #define lson l, m, rt << 1
     27 #define rson m + 1, r, rt << 1 | 1
     28 #define define_m int m = (l + r) >> 1
     29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
     30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
     31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
     32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
     33 #define all(a) (a).begin(), (a).end()
     34 #define lowbit(x) ((x) & (-(x)))
     35 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
     36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
     37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
     38 #define pchr(a) putchar(a)
     39 #define pstr(a) printf("%s", a)
     40 #define sstr(a) scanf("%s", a)
     41 #define sint(a) scanf("%d", &a)
     42 #define sint2(a, b) scanf("%d%d", &a, &b)
     43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
     44 #define pint(a) printf("%d
    ", a)
     45 #define test_print1(a) cout << "var1 = " << a << endl
     46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
     47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
     48 
     49 typedef long long LL;
     50 typedef pair<int, int> pii;
     51 typedef vector<int> vi;
     52 
     53 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
     54 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
     55 const int maxn = 3e4 + 7;
     56 const int md = 10007;
     57 const int inf = 1e9 + 7;
     58 const LL inf_L = 1e18 + 7;
     59 const double pi = acos(-1.0);
     60 const double eps = 1e-6;
     61 
     62 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
     63 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
     64 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
     65 template<class T>T condition(bool f, T a, T b){return f?a:b;}
     66 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
     67 int make_id(int x, int y, int n) { return x * n + y; }
     68 
     69 LL lcm(LL a, LL b) {
     70     return a / gcd(a, b) * b;
     71 }
     72 int L[1007], R[1007], P[1007], a[1007];
     73 
     74 int main() {
     75     //freopen("in.txt", "r", stdin);
     76     int T, n, q;
     77     cin >> T;
     78     while (T--) {
     79         cin >> n >> q;
     80         rep_up0(i, q) {
     81             sint3(L[i], R[i], P[i]);
     82         }
     83         bool ok = true;
     84         rep_up1(i, n) {
     85             LL x = 1;
     86             rep_up0(j, q) {
     87                 if (L[j] <= i && R[j] >= i) {
     88                     x = lcm(x, P[j]);
     89                     if (x > 1e9) {
     90                         ok = false;
     91                         break;
     92                     }
     93                 }
     94             }
     95             a[i] = x;
     96             if (!ok) break;
     97         }
     98         if (!ok) {
     99             puts("Stupid BrotherK!");
    100             continue;
    101         }
    102         rep_up0(i, q) {
    103             int x = a[L[i]];
    104             for (int j = L[i] + 1; j <= R[i]; j ++) {
    105                 x = gcd(x, a[j]);
    106             }
    107             if (x != P[i]) {
    108                 ok = false;
    109                 break;
    110             }
    111         }
    112         if (!ok) {
    113             puts("Stupid BrotherK!");
    114             continue;
    115         }
    116         rep_up1(i, n) {
    117             printf("%d%c", a[i], i == n? '
    ' : ' ');
    118         }
    119     }
    120     return 0;
    121 }
    View Code

    PS:

    因为太弱,只看了这三题,其它题等题目挂出来尽量补上。

    后来发现别人A题是暴力水过的,由于区间是随机的,所以当n很大的时候,基本可以断定答案为yes了。

    -----------------------------  UPDATE:5月8日  ----------------------------------

    由于最近比较忙,自己又水平不够,补题进度太慢。。。。。

    做了一下B,C,E,详见最近的随笔记录。

    ---------------------------- update: 5.9-----------------------------

    补完短时间内可以出的D题,其它三个题暂时不想看了,以后有时间再慢慢搞吧=.=

  • 相关阅读:
    LeetCode链表解题模板
    c++中的new、operator new、placement new
    树的前序、中序、后续、层次遍历的递归和非递归解法
    c++Volatile关键词
    南大算法设计与分析课程OJ答案代码(5)--割点与桥和任务调度问题
    c++右值引用以及使用
    c++选择重载函数
    从4行代码看右值引用
    被遗忘的C结构体打包技术
    南大算法设计与分析课程OJ答案代码(4)--变位词、三数之和
  • 原文地址:https://www.cnblogs.com/jklongint/p/4472378.html
Copyright © 2020-2023  润新知