• pat1101-1110


    1101

    #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 tree[N];
    int n;
    void add(int pos, int num) {
      for(int i = pos; i <= n; i += i&-i) {
        tree[i] += num;
      }
    }
    int sum(int pos) {
      int ans = 0;
      for(int i = pos; i; i-= i&-i) {
        ans += tree[i];
      }
      return ans;
    }
    struct Node{
      int val, po, hh;
    }E[N];
    int cmp(Node a, Node b) {
      return a.val < b.val;
    }
    int cmp2(Node a, Node b) {
      return a.po < b.po;
    }
    int main() {
      while(~scanf("%d", &n)) {
        memset(tree, 0, sizeof(tree));
    
        for(int i = 1; i <= n; ++i) {
          scanf("%d", &E[i].val); E[i].po = i;
        }
        sort(E+1, E+n+1, cmp);
        for(int i = 1; i <= n; ++i) {
          E[i].hh = i;
        } 
        sort(E+1, E+n+1, cmp2);
    
        int ans = 0;
        vector<int> vc;
        for(int i = 1; i <= n; ++i) {
          int tt = sum(E[i].hh);
        //  printf("%d %d
    ", tt, E[i].val);
          if(tt == E[i].hh-1 && E[i].hh == i) ans ++, vc.push_back(E[i].val);
          add(E[i].hh, 1);
        }
        printf("%d
    ", ans);
        for(int i = 0; i < vc.size(); ++i) {
          if(i) printf(" ");
          printf("%d", vc[i]);
        } printf("
    ");
      }
      return 0;
    }

    1102

    #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 = 15;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int L[N];
    int R[N];
    int has[N];
    void bfs(int x) {
      queue<int> Q;
      Q.push(x);
      int fl = 0;
      while(!Q.empty()) {
        int tt = Q.front(); Q.pop();
        if(!fl) fl = 1;
        else printf(" ");
        printf("%d", tt);
        if(R[tt] != INF) Q.push(R[tt]);
        if(L[tt] != INF) Q.push(L[tt]);
      }
    }
    vector<int> vc;
    int ff;
    void dfs(int x) {
    //  printf("%d
    ", x);
      if(L[x] != INF) dfs(L[x]);
      vc.push_back(x);
      if(R[x] != INF) dfs(R[x]);
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        ff = 0;
        for(int i = 0; i < n; ++i) {
          L[i] = INF; R[i] = INF;
        }
        memset(has, 0, sizeof(has));
    
        for(int i = 0; i < n; ++i) {
          char a[10], b[10];
          scanf("%s %s", a, b);
        //  printf("%d %d
    ", a[0]-'0', b[0]-'0');
          if(a[0] != '-') {
            L[i] = a[0]-'0'; has[a[0]-'0'] ++;
          }
          if(b[0] != '-') {
            R[i] = b[0]-'0'; has[b[0]-'0'] ++;
          }
        }
    
        for(int i = 0; i < n; ++i) {
          if(!has[i]) {
            bfs(i); 
            printf("
    ");
            dfs(i);
            break;
          }
        }
        for(int i = vc.size()-1; i >= 0; --i) {
          if(i != vc.size()-1) printf(" ");
          printf("%d", vc[i]);
        } printf("
    ");
    
      }
      return 0;
    }

    1103爆搜,没怎么剪

    #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 = 15;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int Pow(int x, int y) {
      int ans = 1; 
      while(y) {
        if(y & 1)  ans *= x;
        x *= x;
        y >>= 1;
      }
      return ans;
    }
    vector<int> tmp;
    vector<int> ans;
    
    void jud() {
    
      if(ans.size() == 0) {
        for(int i = 0; i < tmp.size(); ++i) ans.push_back(tmp[i]);
      } else {
        int t1 = 0;
        for(int i = 0; i < ans.size(); ++i) t1 += ans[i];
        int t2 = 0;
        for(int i = 0; i < tmp.size(); ++i) t2 += tmp[i];
    
        if(t1 < t2) {
          ans.clear();
          for(int i = 0; i < tmp.size(); ++i) ans.push_back(tmp[i]);
        }else {
          int fl = 0;
          for(int i = 0; i < min( tmp.size(), ans.size() ); ++i) {
            if(tmp[i] != ans[i]) {
              if(tmp[i] > ans[i]) {
                fl = 1;
              }
              break;
            }
          }
          if(fl) {
            ans.clear();
            for(int i = 0; i < tmp.size(); ++i) ans.push_back(tmp[i]);
          }
        }
      }
    }
    void dfs(int x, int y, int z, int maxx) {
    
      if(y == 1) {
        if(x) {
          int num = pow(x, 1.0/z);
          if( pow(num, z) == x) {
            int num = pow(x, 1.0/z);
            tmp.push_back(num);
            jud();
            tmp.erase(--tmp.end());
          }
        }
        return;
      }
      else if(maxx * y == x) {
        int num = pow(maxx, 1.0/z); 
        for(int i = 1; i <= y; ++i) tmp.push_back(num);
        jud();
        for(int i = 1; i <= y; ++i) tmp.erase(--tmp.end());
        return;
      }
    
      for(int i = min((int)pow(x, 1.0/z), maxx) ; i >= 1; --i) {
        int tt = pow(i, z);
        if(tt*y < x) continue;
        tmp.push_back(i);
        dfs(x-tt, y-1, z, i);
        tmp.erase(--tmp.end());  
      }
    }
    int main() {
      int n, k, p;
      while(~scanf("%d %d %d", &n, &k, &p)) {
        tmp.clear(); ans.clear();
        dfs(n, k, p, INF);
    
        if(ans.size() == 0) {
          printf("Impossible
    ");
        }else {
          printf("%d =", n);
          for(int i = 0; i < ans.size(); ++i) {
            if(i) printf(" +");
            printf(" %d^%d", ans[i], p);  
          }
          printf("
    ");
        }
      }
      return 0;
    }

    1104

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<ctime>
    #include<cstdlib>
    #include<map>
    #include<set>
    
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int MOD = 1e9+7;
    const int N = 1e5+5;
    typedef long long ll;
    #define MP(x, y) make_pair(x, y)
    
    double a[N];
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        double ans = 0;
        for(int i = 0; i < n; ++i) {
          scanf("%lf", &a[i]);
          ans += a[i] * (i+1) * (n-i);
        }
        printf("%.2f
    ", ans);
    
    
      }
      return 0;
    }

    1105

    #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 a[N];
    int wid, hei;
    map<pair<int, int>, int> mp;
    int cmp(int a, int b) {
      return a > b;
    }
    void solve(int id, int stx, int sty, int W, int H) {
      if(W == 1) {
        for(int i = sty; i <= sty+H-1; ++i) {
          mp[MP(stx, i)] = a[id++];
        }
        return;
      }else if(H == 1) {
        for(int i = stx; i <= stx+W-1; ++i) {
          mp[MP(i, sty)] = a[id++];
        }
        return;
      }
    
      int X = stx; int Y = sty; 
      for(; X <= stx + W-1; ++X) {
        mp[MP(X, Y)] = a[id ++];  
      //  printf("%d %d
    ", X, Y, a[id-1]);
      } 
      X--; Y++;
    
      for(; Y <= sty + H-1; ++Y) {
        mp[MP(X, Y)] = a[id ++];
      } 
      Y--; X--;
      for(; X >= stx; --X) {
        mp[MP(X, Y)] = a[id ++];
      } 
      X++; Y--;
      for(; Y > sty; --Y) {
        mp[MP(X, Y)] = a[id ++];
      }
      if(W-2 > 0 && H-2 > 0) solve(id, ++X, ++Y, W-2, H-2);
    }
    
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        mp.clear();
        for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
        sort(a, a+n, cmp);
    
        for(int i = 1; i <= sqrt(n); ++i) {
          if(n % i == 0) {
            wid = i;
          }
        }
    
        hei = n/wid;
      //  printf("%d %d
    ", wid, hei);
        solve(0, 0,0, wid, hei);  
        for(int i = 0; i < hei; ++i) {
          for(int j = 0; j < wid; ++j) {
            if(j) printf(" ");
            printf("%d", mp[MP(j, i)]);
          }
          printf("
    ");
        }
      }
      return 0;
    }

    1106

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<ctime>
    #include<cstdlib>
    #include<map>
    #include<set>
    
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int N = 1e5+5;
    typedef long long ll;
    #define MP(x, y) make_pair(x, y)
    
    int n; double p, r;
    struct Node{
      int to, nx;
    }E[N*1000];
    int has[N];
    int head[N]; int tot;
    void add(int fr, int to) {
      E[tot].to = to; E[tot].nx = head[fr]; head[fr] = tot++;
    }
    double ans; 
    vector<double> vc;
    void dfs(int x, double price) {
      if(!has[x]) {
        if(ans > price) {
          ans = price;
        }
        vc.push_back(price);
        return;
      } 
      for(int i = head[x]; ~i; i = E[i].nx) {
        int to = E[i].to;
        dfs(to, price*(1+ r/100));
      }
    }
    int main() {
      while(~scanf("%d %lf %lf", &n, &p, &r)) {
        ans = INF;
        vc.clear();
        memset(head, -1, sizeof(head));
        memset(has, 0, sizeof(has));
        tot = 0; 
        int root;
        for(int i = 0; i < n; ++i) {
          int a; scanf("%d", &a);
          for(int j = 0; j < a; ++j) {
            int b; scanf("%d", &b);
            add(i, b);
            has[i] ++;
          }
        }
    
        dfs(0, p);
        int all = 0;
        for(int i = 0; i < vc.size(); ++i) {
          if(vc[i] == ans) {
            all ++;
          }
        }
        printf("%.4f %d
    ", ans , all);
      }
      return 0;
    }

    1107 题意不清,,,我猜是并查集,反正对了

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<ctime>
    #include<cstdlib>
    #include<map>
    #include<set>
    
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int N = 1e3+5;
    typedef long long ll;
    #define MP(x, y) make_pair(x, y)
    
    vector<int> hob[N];
    int f[N];
    int cnt[N];
    int find(int x) {
      return x==f[x]? x : f[x] = find(f[x]); 
    }
    int cmp(int a, int b) {
      return a > b;
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        for(int i = 0; i < n; ++i) f[i] = i;
    
        for(int i = 0; i < n; ++i) {
          int a; scanf("%d:", &a);
          for(int j = 0; j < a; ++j) {
            int b; scanf("%d", &b);
            hob[b].push_back(i);
          }
        }
    
        for(int i = 1; i <= 1000; ++i) {
          if(hob[i].size() > 1) {
            for(int j = 1; j < hob[i].size(); ++j) {
              int t1 = find(hob[i][0]); int t2 = find(hob[i][j]);
              if(t1 != t2) f[t2] = t1;
            }
          }
        } 
    
        for(int i = 0; i < n; ++i) {
          int t1 = find(i);
          cnt[t1] ++;
        }
        sort(cnt, cnt+n, cmp);
        int all = 0;
        for(int i = 0; i < n; ++i) {
          if(cnt[i]) {
            all ++;
          }
        }
        printf("%d
    ", all);
        for(int i = 0; i < n; ++i) {
          if(!cnt[i]) break;
          if(i) printf(" ");
          printf("%d", cnt[i]);
        } printf("
    ");
    
    
      }
      return 0;
    }

    1108

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<ctime>
    #include<cstdlib>
    #include<map>
    #include<set>
    
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int N = 1e3+5;
    typedef long long ll;
    #define MP(x, y) make_pair(x, y)
    
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        double ans = 0; int ansp = 0;
        for(int i = 0; i < n; ++i) {
          char s[30]; scanf("%s", s);
    
          int fl = 1; int zhen = 1;
          if(s[0] == '-') zhen *= -1;
    
          int len = strlen(s);
          for(int cnt = 0, j = (zhen==-1) ; j < len; ++j) {
            if(s[j] == '.') {
              if(len-j > 3 || j == (zhen==-1) ) {
                fl = 0;
                break;
              }
              cnt ++;
              if(cnt > 1) {
                fl = 0; break;
              }
            } else if( s[j] >= '0' && s[j] <= '9');
            else {
              fl = 0; break;
            }
          }
    
          if(fl) {
            double tt;
            sscanf(s, "%lf", &tt);
            if(tt < -1000 || tt > 1000) {
              fl = 0; 
            }else ansp ++, ans += tt;
          }
          if(!fl) printf("ERROR: %s is not a legal number
    ", s);
        }
    
        if(ansp == 0) printf("The average of 0 numbers is Undefined
    ");
        else if(ansp == 1) printf("The average of %d number is %.2f
    ", ansp, ans/ansp);
        else printf("The average of %d numbers is %.2f
    ", ansp, ans/ansp);
    
      }
      return 0;
    }

    1109

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<vector>
    #include<cmath>
    #include<queue>
    #include<algorithm>
    #include<ctime>
    #include<cstdlib>
    #include<map>
    #include<set>
    
    using namespace std;
    const int INF = 0x3f3f3f3f;
    const int N = 1e4+5;
    typedef long long ll;
    #define MP(x, y) make_pair(x, y)
    
    char nam[N][10];
    struct Node{
      int id, grade;
    }E[N];
    int cmp(Node a, Node b) {
      if(a.grade != b.grade) return a.grade > b.grade;
      else {
        char *A = nam[a.id]; char *B = nam[b.id]; 
        int l1 = strlen(A); int l2 = strlen(B);
    
        for(int i = 0; i < min(l1, l2); ++i) {
          if(A[i] != B[i])
            return A[i] < B[i];
        }
        return l1 < l2;
      }
    }
    int value[N];
    int main() {
      int n, p;
      while(~scanf("%d %d", &n, &p)) {
        for(int i = 0; i < n; ++i) {
          scanf("%s %d", nam[i], &E[i].grade);
          E[i].id = i;
        }
        sort(E, E+n, cmp);
      //  for(int i = 0; i < n; ++i) printf("%s ", nam[E[i].id]); printf("
    ");
    
        int rownum = n/p;
    
        for(int i = 0; i < n; i += rownum) {
          int l = i; int r = i+rownum-1;
        //  printf("%d %d
    ", l, r);
          if(i == 0) r += n%p;
        //  printf("%d %d
    ", l, r);
    
          int L = (r-l+1)/2 + l; int R = L;
        //  printf("%d %d
    ", L, R);
          int cnt = l;
          while(1) {
            if(L == R) value[L] = cnt++;
               else {
              if(L >= l) value[L] = cnt++;
              if(R <= r) value[R] = cnt++;
            }
            L --;  R ++;
            if(L < l && R > r) break;
          }      
          for(int i = l; i <= r; ++i) {
            if(i != l) printf(" ");
            printf("%s", nam[E[value[i]].id]);
          }
          printf("
    ");
          if(i == 0) i += n%p;
        }
      }
      return 0;
    }

    1110原来傻逼错误是 字符串转数字那里,醉了

    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 = 25;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int L[N];
    int R[N];
    int has[N];
    int all;
    int ok[N];
    int cnt[N];
    
    int ansdep; int ans;
    
    void bfs(int x) {
      queue<pair<int ,int> > Q;
      Q.push(MP(x, 0));
      while(Q.front().first != INF) {
        int po = Q.front().first; int dep = Q.front().second; Q.pop();
      //  printf("%d
    ", po);
        all ++;
        cnt[dep] ++;  
        if(dep >= ansdep) {
          ansdep = dep;
          ans = po;
        }
        Q.push(MP(L[po], dep+1)); 
        Q.push(MP(R[po], dep+1));   
      }
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        all = 0;
        memset(ok, 0, sizeof(ok));
        memset(cnt, 0, sizeof(cnt));
    
        for(int i = 0; i < n; ++i) {
          L[i] = INF; R[i] = INF;
        }
        memset(has, 0, sizeof(has));
    
        for(int i = 0; i < n; ++i) {
          char a[10], b[10];
          scanf("%s %s", a, b);
          int tt;
          //  printf("%d %d
    ", a[0]-'0', b[0]-'0');
          if(a[0] != '-') {
            sscanf(a,"%d", &tt);
            L[i] = tt; has[tt] ++;
          }
          if(b[0] != '-') {
            sscanf(b,"%d", &tt);
            R[i] = tt; has[tt] ++;
          }
        }
    
        int root;
        for(int i = 0; i < n; ++i) {
          if(!has[i]) {
            all = 0; ansdep = -1;
            bfs(i);
            root = i;
            break;
          }
        }
      //  printf("%d
    ", all);
    
        if(all == n) printf("YES %d
    ", ans);
        else { printf("NO %d
    ", root); }
    
      }
      return 0;
    }
  • 相关阅读:
    struct tm->time() localtime() gmtime()
    解决VS2013中“This function or variable may be unsafe”的问题
    vm虚拟机三种网络模式
    vulnhub靶机-Raven解题思路
    vulnhub靶机-VulnOs:v2解题思路
    信息收集
    vulnhub靶机-SickOs1.2解题思路
    vulnhub靶机-SickOs解题思路
    vulnhub靶机-SkyTower解题思路
    vulnhub靶场-Stapler解题思路
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433702.html
Copyright © 2020-2023  润新知