pat1051-1060

1051 自己写的非常麻烦
http://blog.csdn.net/biaobiaoqi/article/details/9338397
的算法比较好,我的就贴下吧,主要对入栈出栈不够理解

#include<cmath>
#include<map>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<vector>
#include<queue>
#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 m,n,l;
int a[N];
int vis[N];

int tree[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;
}


int main() {
  while(~scanf("%d %d %d", &m, &n, &l)) {
    a[0] = 0;
    for(int i = 1; i <= l; ++i) {
      for(int j = 1; j <= n; ++j) {
        scanf("%d", &a[j]);    
        vis[j] = 0;
        tree[j] = 0;
      }

      int fl = 1; 


//      for(int j = 1; j <= n; ++j) printf("%d ", a[j]); printf("\n");
      for(int j = 1; j <= n; ++j) {
      //  printf("%d ", vis[a[j]]);
      //  printf("hh\n");
    //    if(vis[a[j]]) continue;
    //    vis[a[j]] = 1;
        vector<int> vc; vc.clear();
        vc.push_back(a[j]);
      //  printf("hh\n");
        for(int k = j+1; k <= n; ++k) {
          if(a[k] < a[j]) {
          //  vis[a[k]] = 1;
            vc.push_back(a[k]);
          }
        }
      //  printf("hh\n");
      //  for(int k = 0; k < vc.size(); ++k) printf("%d ", vc[k]); printf("\n");
        for(int k = 0; k < (int)vc.size()-1; ++k) {
        //  printf("%d %d\n", vc[k], vc[k-1]);
          if(vc[k] < vc[k+1]) {
            fl = 0; break;
          }
        }
      //  printf("%d\n", fl);
      }
  //    for(int j = 1; j <= n; ++j) printf("%d ",a[j]); printf("\n"); 

    //  printf("%d\n", fl);
      for(int j = 1; j <= n; ++j) {
        int tt = a[j] - sum(a[j]);
      //  printf("%d\n", tt);
        add(a[j], 1);
        if(tt > m) {
        //  printf("%d %d\n", i, tt);
          fl = 0; break;
        }
      }


      if(fl) printf("YES\n");
      else printf("NO\n");
    }

  }
  return 0;
}

1052 这题其实也不难,但是错了n发以后心态就崩了。不考虑边界,代码改进没有做足,反正比较毛躁

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>

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, head;

struct Node{
  int pos, value;
}E[N];
int tot;
int cmp(Node a, Node b) {
  return a.value < b.value;
}



int key[N]; int nx[N];

int main() {
  while(~scanf("%d %d", &n, &head)) {
    tot = 0;
    memset(key, INF, sizeof(key));

    for(int i = 0; i < n; ++i) {
      int a, b, c; scanf("%d %d %d", &a, &b, &c);
      key[a] = b;  nx[a] = c;
    }
    if(head == -1) {
      printf("0 -1\n"); continue;
    }

    int x = head;
    while(x != -1) {
      E[++tot].pos = x;
      E[tot].value = key[x];

      x = nx[x];
    }


  // while(1);
    sort(E+1, E+tot+1, cmp);

    printf("%d %05d\n", tot, E[1].pos);
    for(int i = 1; i <= tot; ++i) {
      printf("%05d %d ", E[i].pos, E[i].value);

      if(i == tot) printf("-1\n");
      else printf("%05d\n", E[i+1].pos);
    }  
  }
  return 0;
}

1053

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 105;
typedef long long ll;
#define MP(x, y) make_pair(x, y);

int n, m, s;
int wei[N];

struct Node{
  int to, nx;
}E[N];
int head[N], tot;
void add(int fr, int to) {
  E[tot].to = to; E[tot].nx = head[fr]; head[fr] = tot++;
}
vector<int> vc;
vector<vector<int> > v2;
int leaf[N];

void dfs(int x, int Wei) {
  vc.push_back(wei[x]);
// printf("%d %d\n", x);
  if(Wei == s && !leaf[x]) {
  // printf("%d\n", x);
    v2.push_back(vc);
  }

// vc.push_back(wei[x]);
  for(int j = head[x]; ~j; j = E[j].nx) {
    int to = E[j].to;
    dfs(to, Wei+wei[to]);
  }

  vc.erase(--vc.end());
}
int cmp(vector<int> a, vector<int> b) {
  int l1 = a.size(); int l2 = b.size();

  for(int i = 0; i < min(l1, l2); ++i) {
    if(a[i] != b[i])
       return a[i] > b[i];  
  }
}
int main() {  
  while(~scanf("%d %d %d", &n, &m, &s)) {
    memset(leaf, 0, sizeof(leaf));
    memset(head, -1, sizeof(head)); tot = 0;

    for(int i = 0; i < n; ++i) scanf("%d", &wei[i]);

    for(int i = 0; i < m; ++i) {
      int a, b; scanf("%d %d", &a, &b);
      leaf[a] = 1;
      for(int j = 0; j < b; ++j) {
        int c; scanf("%d", &c);
        add(a, c);
      }  
    }  

    vc.clear(); v2.clear();
  // printf("hh\n");
    dfs(0, wei[0]);
  // printf("hh\n");

    sort(v2.begin(), v2.end(), cmp);
    for(int i = 0; i < v2.size(); ++i) {
      for(int j = 0; j < v2[i].size(); ++j) {
        if(j) printf(" ");
        printf("%d", v2[i][j]);
      }
      printf("\n");
    }
  }
  return 0;
}

1054

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 105;
typedef long long ll;
#define MP(x, y) make_pair(x, y);

map<int, int> mp;
map<int, int> ::iterator it;
int main() {
  int n, m;
  while(~scanf("%d %d", &n, &m)) {
    mp.clear();
    for(int i = 1; i <= n; ++i) {
      for(int j = 1; j <= m; ++j) {
        int a; scanf("%d", &a);
        mp[a] ++;
      }
    }

    int maxn = -1; int maxp;
    for(it = mp.begin(); it != mp.end(); ++it) {
      if(maxn < it->second) {
        maxn = it->second; maxp = it->first;
      }
    }

    printf("%d\n", maxp);
  }
  return 0;
}

1055 这题为什么正确率那么低,我是水过的喽 = =

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5+5;
typedef long long ll;
#define MP(x, y) make_pair(x, y);

char nam[N][10];  
struct Node{
  int namid, age, net;
  bool operator < (const Node &T) const {
    if(net != T.net) return net > T.net;
    else if(age != T.age) return age < T.age;
    else {
      int l1 = strlen(nam[namid]);
      int l2 = strlen(nam[T.namid]);
      for(int i = 0; i < min(l1, l2); ++i) {
        if( nam[namid][i] != nam[T.namid][i] ) {
          return nam[namid][i] < nam[T.namid][i];  
        }
      }
      return l1 < l2;
    }
  }
}E[N];

int main() {
  int n, k;
  while(~scanf("%d %d", &n, &k)) {
    for(int i = 0; i < n; ++i) {
      int a, b;
      scanf("%s %d %d", nam[i], &a, &b);
      E[i].namid = i; E[i].age = a; E[i].net = b;
    }
    sort(E, E+n);

    for(int i = 1; i <= k; ++i) {
      int a, b, c; scanf("%d %d %d", &a, &b, &c);
      printf("Case #%d:\n", i);
      int cnt = 0;
      for(int j = 0; j < n; ++j) {
        if(E[j].age >= b && E[j].age <= c) {
          printf("%s %d %d\n", nam[E[j].namid], E[j].age, E[j].net);
          cnt ++;
          if(cnt == a) break;
        }
      }
      if(cnt == 0) printf("None\n");
    }
  }
  return 0;
}

1056

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<algorithm>
#include<ctime>
#include<cstdlib>
#include<map>

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 w[N];
int order[N];
vector<int> vc;
struct Node{
  int gra; int pos;
}E[N];
int cmp(Node a, Node b) {
  return a.gra > b.gra;
}
int ans[N];
int np, ng;


void solve() {
  int flor = 0;
  while(vc.size() != 1) {
    ++flor;
    vector<int> tmp; vector<int> nx;
    for(int i = 0; i < vc.size(); ++i) {
      tmp.push_back(vc[i]);
      if(tmp.size() == ng) {
        int maxn = -1; int maxp;
        for(int j = 0; j < tmp.size(); ++j) {
          E[tmp[j]].gra = flor;
          if(maxn < w[tmp[j]]) {
            maxn = w[tmp[j]]; maxp = tmp[j];
          }
        }
        nx.push_back(maxp);
        tmp.clear();
      }
    }
    int maxn = -1; int maxp;
    if(tmp.size() > 0) {
      for(int j = 0; j < tmp.size(); ++j) {
        E[tmp[j]].gra = flor;
        if(maxn < w[tmp[j]]) {
          maxn = w[tmp[j]]; maxp = tmp[j];
        }
      }
      nx.push_back(maxp);
    }
    vc.clear();
    for(int i = 0; i < nx.size(); ++i) vc.push_back(nx[i]);
  }
  ++flor; E[vc[0]].gra = flor;
}
int grade[N];
int main() {
  while(~scanf("%d %d", &np, &ng)) {
    vc.clear();
    for(int i = 0; i < np; ++i) E[i].pos = i;
    for(int i = 0; i < np; ++i) scanf("%d", &w[i]);
    for(int i = 0; i < np; ++i) {
      scanf("%d", &order[i]);
      vc.push_back(order[i]);
    }

    solve();
    sort(E, E+np, cmp);

    for(int i = 0; i < np; ++i) {
      if(!i || E[i].gra != E[i-1].gra) ans[E[i].pos] = i+1;
      else ans[E[i].pos] = ans[E[i-1].pos];
    }
    for(int i = 0; i < np; ++i) {
      if(i) printf(" ");
      printf("%d", ans[i]);
    }
    printf("\n");
  }
  return 0;
}

1057 难点在于 中位数怎么做,我用了两个multiset维护,那个中位数会出现在第一个set的最后一个,就是说两个set保存前一半,后一半

#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> vc;
multiset<int> s1, s2;
multiset<int> ::iterator it;

void adjust() {
  if(s1.size() > 0 && s2.size() > 0) {
    int t1 = *(--s1.end());
    s1.erase(--s1.end());
    s2.insert(t1);
    t1 = *(s2.begin());
    s2.erase(s2.begin());
    s1.insert(t1);  
  }

  int cap = (vc.size()+1)/2;
// printf("cap:%d %d\n", vc.size(), cap);
  if(s1.size() > cap) {
    it = --s1.end();
    s2.insert(*it);
    s1.erase(it);
  }else if(s1.size() < cap) {
    it = s2.begin();
    s1.insert(*it);
    s2.erase(it);
  }

}
void pop(int x) {
  it = s1.find(x);
  if(it != s1.end()) {
    s1.erase(it);
  }else {
    it = s2.find(x);
    s2.erase(it);
  }
  adjust();
}
int main() {
  int n;
  while(~scanf("%d", &n)) {
    vc.clear(); s1.clear(); s2.clear();

    for(int i = 0; i < n; ++i) {
      char s[10];
      scanf("%s", s);
      if(s[1] == 'o') {
        if(vc.size() > 0) {
          int tt = vc[vc.size()-1];
          vc.erase(--vc.end());
          pop(tt);
          printf("%d\n", tt);

        }else printf("Invalid\n");
      }else if(s[1] == 'e') {
        if(vc.size() > 0) {
          printf("%d\n", *(--s1.end()));
        }else printf("Invalid\n");
      }else {
        int a; scanf("%d", &a);
        vc.push_back(a);
        s1.insert(a); 
        adjust();
      }

    // for(it = s1.begin(); it != s1.end(); ++it) printf("%d ", *it); printf("h1\n");
    // for(it = s2.begin(); it != s2.end(); ++it) printf("%d ", *it); printf("h2\n");
    }
  }
  return 0;
}

1058

#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 a[2][3];
int ans[3];
int main() {
  for(int i = 0; i < 2; ++i) {
    scanf("%d.%d.%d", &a[i][0], &a[i][1], &a[i][2]);
  }

  int pre = 0;
  for(int i = 2; i >= 0; --i) {
    int t1 = a[0][i] + a[1][i] + pre;
    if(i == 2) {
      ans[i] = t1%29; pre = t1/29;
    }else if(i == 1) {
      ans[i] = t1%17; pre = t1/17;
    }else ans[i] = t1;
  }

  printf("%d.%d.%d\n", ans[0], ans[1], ans[2]);
  return 0;
}

1059

#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 prime[N];
int isprime[N]; int tot;
int main() {
  tot = 0;
  for(int i = 2; i < N; ++i) {
    if(!prime[i]) {
      isprime[++tot] = i;
      for(int j = 2*i; j < N; j += i) {
        prime[j] ++;
      }
    }
  }

  ll n;
  while(~scanf("%lld", &n)) {
    if(n == 1) {
      printf("1=1\n");
      continue;
    }
    int fl = 1;
    printf("%lld=", n);
    for(int i = 1; i <= tot; ++i) {
      if(n%isprime[i] == 0) {
        int cnt = 0;
        while(n%isprime[i] == 0) {
          n /= isprime[i];
          cnt ++;
        }
        if(fl) fl = 0; else printf("*");
        printf("%d", isprime[i]);
        if(cnt > 1) printf("^%d", cnt);
      }
      if(n == 1 || isprime[i] > n) break;
    }
    if(n > 1) {
      if(fl) fl = 0; else printf("*");
      printf("%lld", n);
    }
    printf("\n");
  }
  return 0;
}

1060 大模拟,坑点,k有负数,k可能大于10,0似乎应用0.00*10^0表示(可能不是但是那个数据被我试出来似的),

#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);

char a[205], b[205];
char A[205]; char B[205];
int n;

void solve1() {
  int l1 = strlen(a); int cnt = 0;
  int stpoi = -1, edpoi = l1;
  for(int i = 0; i < l1; ++i) {
    if(a[i] == '.') edpoi = i;
    if(a[i] != '0' && a[i] != '.' && stpoi == -1) {
      stpoi = i;
    }
  }
  if(stpoi == -1) {
  // while(1);
    A[cnt++] = '0'; A[cnt++] = '.';
    for(int i = 0; i < n; ++i) A[cnt++] = '0';
    A[cnt++] = '*'; A[cnt++] = '1'; A[cnt++] = '0'; A[cnt++] = '^'; A[cnt++] = '0';
    A[cnt] = 0;
    return;
  }



  A[cnt++] = '0'; A[cnt++] = '.';
  for(int i = stpoi; i < l1; ++i) {
    if(i == edpoi) continue;
    A[cnt++] = a[i];
    if(cnt-2 == n) {
      break;
    }
  }  
  if(cnt-2 < n) {
    int ed = n-cnt+2;
    for(int i = 1; i <= ed; ++i) {
      A[cnt++] = '0';
    }
  }
  int tt = edpoi - stpoi;
  if(tt < 0) tt ++;
  if(1) { A[cnt++] = '*'; A[cnt++] = '1'; A[cnt++] = '0'; A[cnt++] = '^'; if(tt < 0) A[cnt++] = '-'; if(tt > 10) A[cnt++] = '0'+tt/10, tt %= 10; A[cnt++] = '0'+abs(tt); }
  A[cnt] = 0;
}
void solve2() {
  int l1 = strlen(b); int cnt = 0;
  int stpoi = -1, edpoi = l1;
  for(int i = 0; i < l1; ++i) {
    if(b[i] == '.') edpoi = i;
    if(b[i] != '0' && b[i] != '.' && stpoi == -1) {
      stpoi = i;
    }
  }
  if(stpoi == -1) {
    B[cnt++] = '0'; B[cnt++] = '.';
    for(int i = 0; i < n; ++i) B[cnt++] = '0';
    B[cnt++] = '*'; B[cnt++] = '1'; B[cnt++] = '0'; B[cnt++] = '^'; B[cnt++] = '0';
    B[cnt] = 0;
    return;
  }



  B[cnt++] = '0'; B[cnt++] = '.';
  for(int i = stpoi; i < l1; ++i) {
    if(i == edpoi) continue;
    B[cnt++] = b[i];
    if(cnt-2 == n) {
      break;
    }
  }  
  if(cnt-2 < n) {
    int ed = n-cnt+2;
    for(int i = 1; i <= ed; ++i) {
      B[cnt++] = '0';
    }
  }
  int tt = edpoi - stpoi;
  if(tt < 0) tt ++;
  if(1) { B[cnt++] = '*'; B[cnt++] = '1'; B[cnt++] = '0'; B[cnt++] = '^'; if(tt < 0) B[cnt++] = '-'; if(tt > 10) B[cnt++] = tt/10+'0', tt %= 10; B[cnt++] = '0'+abs(tt); }
  B[cnt] = 0;
}




int main() {
  while(~scanf("%d %s %s", &n, a, b)) {
    solve1(); solve2();

    int fl = 1;
    int l1 = strlen(A); int l2 = strlen(B);
    if(l1 != l2) fl = 0;
    for(int i = 0; i < l1 && fl; ++i) {
      if(A[i] != B[i]) {
        fl = 0; break;
      }
    }

    if(fl) printf("YES %s\n", A);
    else printf("NO %s %s\n", A, B);
  }
  return 0;
}

你可能感兴趣的:(pat1051-1060)