• pat1091-1100


    1091bfs傻逼题,dfs会爆栈

    #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 mp[65][1300][130];
    int vis[65][1300][130];
    int cnt;
    int m, n, l, t;
    
    int ok(int x, int y, int z) {
      if(x >= 1 && x <= l && y >= 1 && y <= m && z >= 1 && z <= n && !vis[x][y][z] && mp[x][y][z] == 1) return 1;
      else return 0;
    } 
    void bfs(int xx, int yy, int zz) {
      queue< pair<pair<int,int>,int> > Q;
      Q.push(MP(MP(xx, yy), zz));
    
      while(!Q.empty()) {
        int x = Q.front().first.first; int y = Q.front().first.second; int z = Q.front().second; Q.pop();
        for(int i = -1; i <= 1; i += 2) {
          if(ok(x+i, y, z)) {
            cnt ++; vis[x+i][y][z] = 1;
            Q.push(MP(MP(x+i, y), z));
          }
          if(ok(x, y+i, z)) {
            cnt ++; vis[x][y+i][z] = 1;
            Q.push(MP(MP(x, y+i), z));
          }
          if(ok(x, y, z+i)) {
            cnt ++; vis[x][y][z+i] = 1;
            Q.push(MP(MP(x, y), z+i));
          }
        }
      }
    }
    
    int main() {
      while(~scanf("%d %d %d %d", &m, &n, &l, &t)) {
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= l; ++i) {
          for(int j = 1; j <= m; ++j) {
            for(int k = 1; k <= n; ++k) {
              scanf("%d", &mp[i][j][k]);
            }
          }
        }
        if(n > 128 || m > 1286 || l > 60) while(1);
        //  printf("hh
    ");  
        int ans = 0;
        for(int i = 1; i <= l; ++i) {
          for(int j = 1; j <= m; ++j) {
            for(int k = 1; k <= n; ++k) {
              if(!vis[i][j][k] && mp[i][j][k] == 1) {
                cnt = 1; vis[i][j][k] = 1;
                bfs(i, j, k);
                if(cnt >= t) ans += cnt;
              }
            }
          }
        }
        printf("%d
    ", ans);
      }  
      return 0;
    }

    1092

    #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 a[1005];
    char b[1005];
    map<char, int> mp;
    map<char, int> m2;
    map<char, int>::iterator it;
    
    int main() {
      while(~scanf("%s %s", a, b)) {
        mp.clear(); m2.clear();
    
        for(int i = 0; i < strlen(b); ++i) {
          mp[b[i]] ++;
        }
        for(int i = 0; i < strlen(a); ++i) {
          m2[a[i]] ++;
        }
        int fl = 1; int ans = 0;
        for(it = mp.begin(); it != mp.end(); ++it) {
          if(m2[it->first] < it->second) {
            fl = 0; ans += it->second - m2[it->first];
          }
        }
        if(fl) printf("Yes %d
    ", strlen(a)-strlen(b));
        else printf("No %d
    ", ans);
      }
      return 0;
    }

    1093

    #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)
    
    char s[N];
    int P[N];
    int T[N];
    int main() {
      while(~scanf("%s", s+1)) {
        int len = strlen(s+1);
        for(int i = 1; s[i]; ++i) {
          P[i] = P[i-1] + (s[i] == 'P');
          T[i] = T[i-1] + (s[i] == 'T');
        }
      //  for(int i = 1; i <= len; ++i) printf("%d ", P[i]); printf("
    ");
      //  for(int i = 1; i <= len; ++i) printf("%d ", T[i]); printf("
    ");  
    
        int ans = 0;
        for(int i = 1; s[i]; ++i) {
          if(s[i] == 'A') {
            ans = (ans + 1ll*P[i]*(T[len]-T[i])) % MOD;
          }
        }
        printf("%d
    ", ans);
      }
      return 0;
    }

    1094

    #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 = 105;
    typedef long long ll;
    #define MP(x, y) make_pair(x, y)
    
    struct Node{
      int to, nx;
    }E[N*2];
    int head[N],tot;
    void add(int fr, int to) {
      E[tot].to = to; E[tot].nx = head[fr]; head[fr] = tot++;
    }
    int has[N];
    void dfs(int x, int dep) {
      has[dep] ++;
      for(int i = head[x]; ~i; i = E[i].nx) {
        dfs(E[i].to, dep+1);
      }
    }
    int main() {
      int n, m;
      while(~scanf("%d %d", &n, &m)) {
        int ans = -1; int ansp;
        memset(head, -1, sizeof(head)); tot = 0;
        memset(has, 0, sizeof(has));
    
        for(int i = 0; i < m; ++i)  {
          int a, b; scanf("%d %d", &a, &b);
          for(int j = 0; j < b; ++j) {
            int c; scanf("%d", &c);
            add(a, c);
          }
        }
        dfs(1, 0);
    
        for(int i = 0; i < n; ++i) {
          if(has[i] > ans) {
            ans = has[i]; ansp = i+1;
          }
        }
        printf("%d %d
    ", ans, ansp);
      }
      return 0;
    }

    1095

    #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;
      int cost;
    }E[N];
    vector<pair<int, int> > vc[N];
    int cmp(Node a, Node b) {
      if(a.cost != b.cost) return a.cost > b.cost;
      else {
        for(int i = 0; i < strlen(nam[a.id]); ++i) {
          if(nam[a.id][i] != nam[b.id][i]) 
            return nam[a.id][i] < nam[b.id][i];
        }
      }
    }
    int tot = 0;
    map<string, int> mp;
    
    int has[86400];
    void put(int x) {
      printf("%02d:%02d:%02d
    ", x/3600, (x%3600)/60, x%60);
    }
    int main() {
      int n, k;
      while(~scanf("%d %d", &n, &k)) {
        mp.clear(); tot = 0;
        for(int i = 1; i <= n; ++i) vc[i].clear();
    
        memset(has, 0, sizeof(has));
    
        for(int i = 0; i < n; ++i) {
          char a[10]; char b[5]; int t1,t2,t3;
          scanf("%s %d:%d:%d %s", a, &t1, &t2, &t3, b);
          int Time = t1*3600 + t2*60 + t3;
    
          if(mp.find(a) == mp.end()) {
            ++tot;
            mp[a] = tot;
            for(int j = 0; j <= strlen(a); ++j) nam[tot][j] = a[j];
            E[tot].id = tot; E[tot].cost = 0;
            vc[tot].push_back(MP(Time, b[0] == 'i'?1:-1 ));
          }else {
            int id = mp[a];
            vc[id].push_back(MP(Time, b[0] == 'i'?1:-1));  
          }  
        }
        for(int i = 1; i <= tot; ++i) {
          sort(vc[i].begin(), vc[i].end());
      //    printf("%s ", nam[E[i].id]); for(int j = 0; j < vc[i].size(); ++j) printf("%d:%d ", vc[i][j].first, vc[i][j].second); printf("
    ");
          for(int j = 0; j < vc[i].size(); j += 2) {
            if( vc[i][j].second < 0 || j+1 >= vc[i].size() || (vc[i][j+1].second > 0 && vc[i][j].second > 0) ) --j;
            else {
              has[vc[i][j].first] ++; has[vc[i][j+1].first] --;
                 E[i].cost += vc[i][j+1].first - vc[i][j].first;
            }
          }
      //    printf("%d
    ", E[i].cost);
        }
    
        for(int i = 1; i <= 86399; ++i) has[i] += has[i-1];
        sort(E+1, E+tot+1, cmp);
    
        for(int i = 0; i < k; ++i)  {
          int a, b, c;
          scanf("%d:%d:%d", &a, &b, &c);
          int Time = a*3600 + b*60 + c;
          printf("%d
    ", has[Time]);
        }
      //  for(int i = 1; i <= tot; ++i) printf("%d ", E[i].cost); printf("
    ");
    
        for(int i = 1; i <= tot; ++i) {
          if( E[i].cost != E[1].cost) {
            break;
          }else printf("%s ", nam[E[i].id]);
    
        }
        put(E[1].cost);
    
      }
      return 0;
    }

    1096 一个暴力

    #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)
    
    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)) {
        if(prime(n)) {
          printf("1
    ");
          printf("%d
    ", n);
          continue;
        }
        for(int i = 12; i >= 1; --i) {
          int suc = 0;
          ll tt = 1;
          for(int j = 1; j <= i; ++j) {
            tt *= j; 
          }
          int cnt = 0;
          for(int j = 1; ; ++j) {
            tt /= j; tt *= (j+i);
          //  if(++cnt > 10000) break; 
            if(tt > n) break;
            if(n % tt == 0) {
              suc = 1;
              printf("%d
    ", i);
              for(int k = j+1; k <= j+i; ++k) {
                if(k != j+1) printf("*");
                printf("%d", k);
              } printf("
    ");
              break;
            }
    
          }
          if(suc) break;  
        }
    
      }
      return 0;
    }

    1097

    #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 key[N], nx[N];
    int E[N], T[N];
    int has[N];
    int tot, tol;
    int Abs(int x) {
      if(x < 0) return -x;
      else return x;
    }
    void dfs(int x) {
      if(x == -1) return;
      if(!has[Abs(key[x])]) E[++tot] = x, has[Abs(key[x])] = 1;
      else T[++tol] = x;
    
      dfs(nx[x]);
    }
    int main() {
      int head, n;
      while(~scanf("%d %d", &head, &n)) {
        tot = 0; tol = 0;
        for(int i = 0; i < n; ++i) {
          int a,b,c; scanf("%d %d %d", &a, &b, &c);
          key[a] = b; nx[a] = c;
        }
        dfs(head);
    
        for(int i = 1; i <= tot; ++i) {
          printf("%05d %d ", E[i], key[E[i]]);
          if(i == tot) printf("-1
    ");
          else printf("%05d
    ", E[i+1]);
        }
        for(int i = 1; i <= tol; ++i) {
          printf("%05d %d ", T[i], key[T[i]]);
          if(i == tol) printf("-1
    ");
          else printf("%05d
    ", T[i+1]);
        }
    
      }
      return 0;
    }

    1098 堆排序的定义,需要熟悉数据结构

    #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 = 205;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int init[N];
    int temp[N];
    int a[N];
    int n;
    void insert(int x) {
      int po = x; int tmp = a[x];
      for(int j = 1; j < x; ++j) {
        if(a[x] <= a[j]) {
          po = j;
          break;
        }
      }
      for(int j = x; j >= po+1; --j) {
        a[j] = a[j-1];
      }
      a[po] = tmp;
    }
    int main() {
      while(~scanf("%d", &n)) {
        for(int i = 1; i <= n; ++i) scanf("%d", &init[i]);
        for(int i = 1; i <= n; ++i) scanf("%d", &temp[i]);
        for(int i = 1; i <= n; ++i) a[i] = init[i];
    
        int ju = 0;
        for(int i = 1; i <= n; ++i) {
          insert(i);
          //  for(int j = 1; j <= n; ++j) printf("%d ", a[j]); printf("
    ");
          int nn = 1;
          for(int j = 1; j <= n; ++j) {
            if(temp[j] != a[j]) {
              nn = 0; break;
            }
          }
          int mx = 1;
          for(int j = 2; j <= n; ++j) {
            if(temp[j] > temp[j-1]) {
              mx = j;
            }else break;
          }
          if(mx > i) continue;
    
          if(nn) {
            printf("Insertion Sort
    ");
            ju = 1;
            insert(i+1);
            for(int j = 1; j <= n; ++j) {
              if(j != 1) printf(" ");
              printf("%d", a[j]);
            } printf("
    ");
            break;
          }
        }
        //  ju = 0;
      //  printf("hh
    ");
        if(!ju) {
          int last ;
          for(int i = n; i >= 1; --i) {
            if(temp[i] < temp[1]) {
              last = i; break;
            }
          }
          for(int i = 1; i <= n; ++i) a[i] = temp[i];
          swap(a[1], a[last]);
          int pos = 1, max;
    
          while(pos<last)      //修正最大堆  
          {  
            max=pos;  
            if(pos*2<last && a[pos*2]>a[max])  
              max=pos*2;  
            if(pos*2+1<last&& a[pos*2+1]>a[max])  
              max=pos*2+1;  
            if(max==pos)  
              break;  
            swap(a[pos], a[max]);  
            pos=max;  
          }  
    
          printf("Heap Sort
    ");
    
          for(int j = 1; j <= n; ++j) {
            if(j != 1) printf(" ");
            printf("%d", a[j]);
          } printf("
    ");
        }
      }
      return 0;
    }

    1099

    #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 = 205;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    int L[N], R[N];
    int num[N];
    int a[N];
    vector<int> vc;
    void dfs(int x) {
      if(~L[x]) dfs(L[x]);
      vc.push_back(x);
      if(~R[x]) dfs(R[x]);
    }
    void bfs(int x) {
      queue<int> Q;
      Q.push(x);
      int fl = 0;
      while(!Q.empty()) {
        int x = Q.front(); Q.pop();
        if(~L[x]) Q.push(L[x]);
        if(~R[x]) Q.push(R[x]);
        if(!fl) fl = 1;
        else printf(" ");
        printf("%d", num[x]);
      }
      printf("
    ");
    }
    int main() {
      int n;
      while(~scanf("%d", &n)) {
        for(int i = 0; i < n; ++i) {
          scanf("%d %d", &L[i], &R[i]);
        }
        dfs(0);
        for(int i = 0; i < n; ++i) {
          scanf("%d", &a[i]);
        }
        sort(a, a+n);
        for(int i = 0; i < n; ++i) {
          num[vc[i]] = a[i];
        }
    
        bfs(0);
      } 
      return 0;
    }

    1100 cat = 0没有加一开始,真是愚蠢

    #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 = 205;
    const int INF = 0x3f3f3f3f;
    #define MP(x, y) make_pair(x, y)
    
    char A[20][10] ={"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
    char B[20][10] ={"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
    
    void solve(int x) {
      if(x/13) {
        printf("%s",B[x/13]);
        if(x%13) printf(" %s
    ", A[x%13]);
        else printf("
    ");
      }else {
        printf("%s
    ", A[x%13]);
      }
    }
    int main() {
      int n;
      char s[20];
    
      while(~scanf("%d", &n)) {
        getchar();
        for(int i = 0; i < n; ++i) {
          gets(s);
        //  printf("%s
    ", s);
          int nn = 1;
          int tt = 0;
          for(int j = 0; s[j]; ++j) {
            if(s[j] >= '0' && s[j] <= '9') {
              tt = tt*10 + s[j]-'0';
            }else {
              nn = 0; 
            }
          }
    
          if(nn) {
            solve(tt);
          }else  {
          //  while(1);
            char tmp[10]; int cnt = 0;
            tt = 0;
            int fl = 0;
          //  printf("tt %c
    ", s[strlen(s)]);
            for(int j = 0; j <= strlen(s); ++j) {
              if(s[j] >= 'a' && s[j] <= 'z') {
            //    printf(" tt%d:%c", j, s[j]);
                tmp[cnt ++] = s[j];
              }else if(cnt){
                tmp[cnt] = 0;
                for(int k = 0; k <= 12; ++k) {
                  if(strcmp(tmp, A[k]) == 0) {
                    tt = tt*13 + k; fl += 2;
                  }else if (strcmp(tmp, B[k]) == 0)  {
                    tt = tt*13 + k; fl += 1;
                  }
                }
                cnt = 0;
              }
            }/*
            if(cnt) {
              for(int k = 0; k <= 12; ++k) {
                if(strcmp(tmp, A[k]) == 0) {
                  tt = tt*13 + k; fl += 2;
                }else if (strcmp(tmp, B[k]) == 0)  {
                  tt = tt*13 + k; fl += 1;
          <      }
              }
    
            }*/
            if(fl == 1) tt *= 13;
    
            printf("%d
    ", tt);
          }
    
        }
      }
      return 0;
    }
  • 相关阅读:
    大规模机器学习
    机器学习之推荐系统
    SVM实现邮件分类
    机器学习之异常检测
    降维算法学习
    手写数字识别实现
    动态规划训练之十七
    概率期望训练之三
    数据结构训练之四
    绵阳东辰国际test201910.25
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433703.html
Copyright © 2020-2023  润新知