• pat1111-1120


    1111 比较麻烦的最短路

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 505;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    struct Node{
      int to, nx, di, ti;
    }E[N*N*2];
    int head[N], tot;
    void add(int fr, int to, int di, int ti) {
      E[tot].to = to; E[tot].di = di; E[tot].ti = ti; E[tot].nx = head[fr];
      head[fr] = tot ++;
    }
    vector<int> ans1; int Ans1;
    vector<int> ans2; int Ans2;
    
    struct Hode{
      int po, di, ti;
      Hode(int a=0, int b=0, int c=0):po(a), di(b), ti(c){}
      bool operator < (const Hode & T) const {
        if(di != T.di) return di > T.di;
        else return ti > T.ti;
      }
    };
    struct Tode{
      int po, di, ti;
      Tode(int a=0, int b=0, int c=0):po(a), di(b), ti(c){}
      bool operator < (const Tode & T) const {
        if(ti != T.ti) return ti > T.ti;
        else return di > T.di;
      }
    };
    
    int pre[N], vis[N];
    int dis[N], tim[N];
    void dfs1(int x, int tag) {
      if(x == tag) return;
      dfs1(pre[x], tag);
      ans1.push_back(x);
    }
    void dfs2(int x, int tag) {
      if(x == tag) return;
      dfs2(pre[x], tag);
      ans2.push_back(x);
    }
    
    
    void dij1(int s, int t) {
      priority_queue<Hode> Q;
      memset(dis, INF, sizeof(dis));
      memset(tim, INF, sizeof(tim));
      dis[s] = 0; tim[s] = 0;
      memset(vis, 0, sizeof(vis));
      Q.push(Hode(s, dis[s], tim[s]));
    
      while(!Q.empty()) {
        int po = Q.top().po; Q.pop();
        if(vis[po]) continue;
        vis[po] =  1;
        for(int i = head[po]; ~i; i = E[i].nx) {
          int to = E[i].to;
          if(dis[to] > dis[po] + E[i].di) {
            dis[to] = dis[po] + E[i].di;
            tim[to] = tim[po] + E[i].ti;
            pre[to] = po;
            Q.push(Hode(to, dis[to], tim[to]));
          }else if(dis[to] == dis[po] + E[i].di && tim[to] > tim[po] + E[i].ti) {
            tim[to] = tim[po] + E[i].ti;
            pre[to] = po;
            Q.push(Hode(to, dis[to], tim[to]));
          } 
        }
      }
    //  printf("Distance = %d: %d", dis[t], s);
      ans1.push_back(s); Ans1 = dis[t];
      dfs1(t, s);
    //  printf("
    ");
    }
    void dij2(int s, int t) {
      priority_queue<Tode> Q;
      memset(dis, INF, sizeof(dis));
      memset(tim, INF, sizeof(tim));
      dis[s] = 0; tim[s] = 0;
      memset(vis, 0, sizeof(vis));
      Q.push(Tode(s, dis[s], tim[s]));
    
      while(!Q.empty()) {
        int po = Q.top().po; Q.pop();
        if(vis[po]) continue;
        vis[po] =  1;
        for(int i = head[po]; ~i; i = E[i].nx) {
          int to = E[i].to;
          if(tim[to] > tim[po] + E[i].ti) {
            dis[to] = dis[po] + 1;
            tim[to] = tim[po] + E[i].ti;
            pre[to] = po;
            Q.push(Tode(to, dis[to], tim[to]));
          }else if(tim[to] == tim[po] + E[i].ti && dis[to] > dis[po] + 1) {
            dis[to] = dis[po] + 1;
            pre[to] = po;
            Q.push(Tode(to, dis[to], tim[to]));
          } 
        }
      }
      ans2.push_back(s); Ans2 = tim[t];
      dfs2(t, s); 
    }
    int main() {
      int n, m;
      while(~scanf("%d %d", &n, &m)) {
        ans1.clear(); ans2.clear();
        memset(head, -1, sizeof(head));
        tot = 0;
    
        for(int i = 0; i < m; ++i) {
          int a, b, c, d, e; scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
          add(a, b, d, e);
          if(!c) add(b, a, d, e);
        }
        int s,t; scanf("%d %d", &s, &t);
    
        dij1(s, t);
        dij2(s, t);
      //  if(s == t) while(1);
    
        int fl = 1;
        for(int i = 0; i < min(ans1.size(), ans2.size()); ++i) {
          if(ans1[i] != ans2[i]) {
            fl = 0; break;
          }
        }
    
        if(fl) {
          printf("Distance = %d; Time = %d: ", Ans1, Ans2);
          for(int i = 0; i < ans1.size(); ++i) {
            if(i) printf(" -> ");
            printf("%d", ans1[i]);
          } printf("
    ");
        }else {
          printf("Distance = %d: ", Ans1);
          for(int i = 0; i < ans1.size(); ++i) {
            if(i) printf(" -> ");
            printf("%d", ans1[i]);
          }
          printf("
    Time = %d: ", Ans2);
          for(int i = 0; i < ans2.size(); ++i) {
            if(i) printf(" -> ");
            printf("%d", ans2[i]);
          }
          printf("
    ");
        }
      }
      return 0;
    }

    1112 gcd一下

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 505;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    char s[1005];
    map<char, int> mp; int tot;
    char table[40];
    map<char, int> suc;
    vector<int> vc[40];
    
    int gcd(int a, int b) {
      if(b == 0) return a;
      else return gcd(b, a%b);
    }
    int main() {
      int n;
      tot = 0;
      for(char i = 'a'; i <= 'z'; ++i) mp[i] = ++tot, table[tot] = i;
      for(char i = '0'; i <= '9'; ++i) mp[i] = ++tot, table[tot] = i;
      mp['_'] = ++tot; table[tot] = '_';
    
      while(~scanf("%d", &n)) {
        suc.clear();
    
        for(int i = 0; i < 40; ++i) vc[i].clear();
        scanf("%s", s);
        int len = strlen(s);
        int tmp = 0;
        for(int i = 0; i <= len; ++i) {
          if(i && s[i] != s[i-1]) {
            vc[mp[s[i-1]]].push_back(tmp);
          //  printf("%c %d
    ", s[i-1], tmp);
            tmp = 0;
          }
          tmp ++;
        }
    
        for(int i = 1; i <= tot; ++i) {
          if(vc[i].size() > 0) {
          //  printf("%c ", table[i]); for(int j = 0; i < vc[i].size(); ++j) printf("%d ", vc[i][j]); printf("
    ");
            int tt = vc[i][0];
            for(int j = 1; j < vc[i].size(); ++j) {
              tt = gcd(tt, vc[i][j]);
            }
    
            if(tt % n == 0) {
          //    printf("%c
    ",table[i]);
              suc[table[i]] = 1;
            }
          }
        }
    
        tmp = 0;
        for(int i = 0; i < len; ++i) {
          if(suc[s[i]] > 0) {
            suc[s[i]] = -1;
            printf("%c", s[i]);
          }
        }  
        printf("
    ");
    
        for(int i = 0; i <= len; ++i) {
          if(i && s[i] != s[i-1]) {
            if(suc[s[i-1]] == -1) {
              for(int j = 0; j < tmp/n; ++j) printf("%c", s[i-1]);
            }
            else for(int j = 0; j < tmp; ++j) printf("%c", s[i-1]);
            tmp = 0;
          }
          tmp ++;
        }
        printf("
    ");
      }
      return 0;
    }

    1113

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e5+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int A[N];
    
    ll Abs(ll x) {
      if(x < 0) return -x;
      else return x;
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        ll all = 0;
        for(int i = 0; i < n; ++i) {
          scanf("%d", &A[i]);
          all += A[i];
        }
        sort(A, A+n);
        ll tmp = 0;
        for(int i = 0; i < n/2; ++i) {
          tmp += A[i];
        }
        ll ans = Abs(tmp - (all-tmp));
        if(n % 2) {
          tmp += A[n/2];
          ans = max(ans, Abs(tmp - (all-tmp)) );
        }
    
        printf("%d %lld
    ", n%2, ans);
    
    
      }
      return 0;
    }

    1114 题意不好理解 其实就是并查集

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e5+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int f[N];
    int find(int x) { return x == f[x]? x : f[x] = find(f[x]); }
    void merge(int a, int b) {
      if(b == -1 || a == -1) return;
      int t1 = find(a); int t2 = find(b);
    
      if(t1 != t2) {
        if(t1 > t2) f[t1] = t2; else f[t2] = t1;
      }
    }
    int has[N];
    int sett[N], area[N];
    int cnt[N]; double avg_set[N], avg_area[N]; 
    int ord[N];
    int cmp(int x, int y) {
      if(avg_area[x] != avg_area[y]) return avg_area[x] > avg_area[y];
      else return x < y;
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        for(int i = 0; i < 1e5; ++i) f[i] = i;
    
        for(int i = 0; i < n; ++i) {
          int a,b,c; scanf("%d %d %d", &a, &b, &c);
          has[a] ++; has[b] ++; has[c] ++;
          merge(a, b); merge(a, c);
          int d; scanf("%d", &d);
          for(int j = 0; j < d; ++j) {
            int l; scanf("%d", &l);
            has[l] ++;
            merge(a, l);
          }
          scanf("%d %d", &sett[a], &area[a]);
        }
    //    printf("hh
    ");
    
        for(int i = 0; i < 1e5; ++i) {
          if(has[i]) {
            int t1 = find(i);
            cnt[t1] ++; avg_set[t1] += sett[i]; avg_area[t1] += area[i];
          }  
        }
        int tot = 0;
        for(int i = 0; i < 1e5; ++i) {
          if(cnt[i]) {
            avg_set[i] /= cnt[i]; avg_area[i] /= cnt[i];
            ord[tot ++] = i;  
          }
        }
        sort(ord, ord + tot, cmp);
        printf("%d
    ", tot);
        for(int i = 0; i < tot; ++i) {
          printf("%04d %d %.3f %.3f
    ", ord[i], cnt[ord[i]], avg_set[ord[i]], avg_area[ord[i]]);
        }
      }
      return 0;
    }

    1115

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e3+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int num[N];
    int L[N];
    int R[N];
    int root = 0;
    void insert(int x, int nw) {
      if(root == -1) {
        root = x; return;
      }
      if(num[x] <= num[nw]) {
        if(L[nw]) insert(x, L[nw]);
        else L[nw] = x;
      }else {
        if(R[nw]) insert(x, R[nw]);
        else R[nw] = x;
      }
    }
    int cnt[N];
    int Deep;
    void dfs(int x, int dep) {
      Deep = max(Deep, dep);
      cnt[dep] ++;
    
      if(L[x]) dfs(L[x], dep+1);
      if(R[x]) dfs(R[x], dep+1);
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        root = -1;
        for(int i = 0; i < n; ++i) {
          scanf("%d", &num[i]);
          insert(i, root);
        }
        Deep = -1;
        dfs(root, 0);
    
        printf("%d + %d = %d
    ", cnt[Deep], cnt[Deep-1], cnt[Deep] + cnt[Deep-1]);
      }
      return 0;
    }

    1116

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e5+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int Rank[N];
    int has[N];
    int prime(int x) {
      if(x == 1) return 0;
      if(x == 2) return 1;
      for(int i = 2; i <= sqrt(x); ++i) {
        if(x % i == 0) 
          return 0;
      }
      return 1;
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        memset(has, 0, sizeof(has));
        for(int i = 0; i < n; ++i) {
          int a; scanf("%d", &a);
          Rank[a] = i+1;
        }
        int k; scanf("%d", &k);
        while(k--) {
          int a; scanf("%d", &a);
          if(has[a]) printf("%04d: Checked
    ", a);
          else if(Rank[a] == 0) printf("%04d: Are you kidding?
    ", a);
          else if(Rank[a] == 1) printf("%04d: Mystery Award
    ", a);
          else if(prime(Rank[a])) printf("%04d: Minion
    ", a);
          else printf("%04d: Chocolate
    ", a);
          if(Rank[a]) has[a] = 1;
        }
      }
      return 0;
    }

    1117这题题意也是扯淡啊,,,,

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e5+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int a[N];
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
        sort(a, a+n);
        a[n] = INF;
    
        int ans = -1;
        for(int i = 0; i < n; ++i) {
          if(a[i] != a[i+1]) {
            ans = max(ans, min(n-i-1, a[i+1]-1) );
          }
        }
        ans = max(ans, min(n, a[0]-1));
        printf("%d
    ", ans);
      }
      return 0;
    }

    1118

    include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e4+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int fa[N];
    int cnt[N];
    int find(int x) { return x == fa[x]? x : fa[x] = find(fa[x]); }
    
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= 1e4; ++i) fa[i] = i;
    
        int m = -1;
        for(int i = 1; i <= n; ++i) {
          int a; scanf("%d", &a);
          int pre;
          if(a) {
            scanf("%d", &pre);
            m = max(m, pre);
          }
          for(int j = 1; j < a; ++j) {
            int b; scanf("%d", &b);
            m = max(m, b);
            int t1 = find(pre); int t2 = find(b);
            if(t1 != t2) fa[t2] = t1;
          }
        }
        for(int i = 1; i <= m; ++i) {
          cnt[find(i)] ++;
        }
        int all = 0; int maxx = -1;
        for(int i = 1; i <= m; ++i) {
          if(cnt[i]) {
            all ++;
            maxx = max(maxx, cnt[i]);
          }
        }
        printf("%d %d
    ", all, m);
    
        int k; scanf("%d", &k);
        for(int i = 0; i < k; ++i) {
          int a; int b; scanf("%d %d", &a, &b);
          int t1 = find(a); int t2 = find(b);
          if(t1 == t2) printf("Yes
    ");
          else printf("No
    ");
        }
    
      }
      return 0;
    }

    1119 主要对于先后序的概念需要掌握

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e5+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int pre[N];
    int post[N];
    int L[N], R[N];
    int root;
    
    int fl = 1;
    
    void solve(int _l, int _r, int l, int r, int rt) {
    //  printf("%d %d %d %d %d
    ", _l,_r,l,r,rt);
      if(l > r) return;
    
      int ro = pre[_l];
      int pos = 0;
      for(int i = l; i <= r; ++i) {
        if(post[i] == ro) {
          pos = i; break;
        }
      }
    
      if(pos == l && pos != r) {
        L[rt] = post[pos];
        R[rt] = pre[_l+1];
        solve(_l+2, _r, l+1, r-1, pre[_l+1]);
      }else if(pos == r){
        fl = 0;
        L[rt] = post[pos];
        solve(_l+1, _r, l, r-1, post[pos]);
      }else {
      //  printf("hh %d
    ", rt);
        L[rt] = post[pos];
        int _pos = (pos-l) + _l;
      //  printf("%d
    ", _pos, pre[_pos]);
        R[rt] = pre[_pos+1];
        solve(_l+1, _pos, l, pos-1, post[pos]);
        solve(_pos+2, _r, pos+1, r-1, pre[_pos+1]);
      }
    
    }
    
    
    int nn = 0;
    void dfs(int x) {
      if(L[x]) dfs(L[x]);
      if(!nn) nn = 1; else printf(" ");
      printf("%d", x);
      if(R[x]) dfs(R[x]);
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        memset(L, 0, sizeof(L));
        memset(R, 0, sizeof(R));
        nn = 0;
    
        for(int i = 1; i <= n; ++i) {
          scanf("%d", &pre[i]);
        }
        for(int i = 1; i <= n; ++i) {
          scanf("%d", &post[i]);
        }
        root = pre[1];
        solve(2, n, 1, n-1, root);
    
        if(fl) printf("Yes
    ");
        else printf("No
    ");
        dfs(root);
        printf("
    ");
      }
      return 0;
    }

    1120

    #include<cmath>
    #include<map>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<set>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1e4+5;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    map<int, int> mp;
    map<int, int>::iterator it;
    
    void solve(int x) {
      int tt = 0;
      while(x) {
        tt += x%10;
        x /= 10;
      }
      mp[tt] ++;
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        mp.clear();
    
        for(int i = 0; i < n; ++i) {
          int a; scanf("%d", &a);
          solve(a);
        }
    
        int cnt = 0;
        for(it = mp.begin(); it != mp.end(); ++it) {
          if(it->second > 0) {
            cnt ++;
          }
        }
        printf("%d
    ", cnt);
        int fl = 0;
        for(it = mp.begin(); it != mp.end(); ++it) {
          if(it->second > 0) {
            if(!fl) fl = 1; else printf(" ");
            printf("%d", it->first);
          }
        } printf("
    ");
    
    
      }
      return 0;
    }
  • 相关阅读:
    UVa10036
    矩阵链乘法(动态规划)
    Codeforces 230A
    iOS 界面开发
    iOS 自动布局
    iOS 自动布局过程
    iOS 界面布局,设置约束
    iOS + UIWebView 实践
    iOS 参考 网络书籍
    iOS 框架 Nimbus
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433701.html
Copyright © 2020-2023  润新知