pat1031-1040

1031

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>

using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e6+5;
const int M = 3e5+5;

char s[85];
int n1,n2,n3;

char mp[100][100];
int main() {
  while(~scanf("%s", s)) {
    memset(mp, 0, sizeof(mp));
    int len = strlen(s);

    int t1 = len-2;
    int t2 = t1/3; int t3 = t1%3;

    n1 = n2 = n3 = t2;
    if(t3 == 1) n2 ++;
    else if(t3 == 2) n1 ++, n3 ++;

  //  printf("%d %d %d\n", n1, n2, n3);
    int x = 1, y = 1; int cn = 0;
    for(int i = 1; i <= n1+1; ++i) {
      mp[x][y] = s[cn++];
      x ++;
    }
    x--; y++;  
    for(int i = 1; i <= n2+1; ++i) {
      mp[x][y] = s[cn++];
      y ++;
    }
    y--; x--;
    for(int i = 1; i <= n3; ++i) {
      mp[x][y] = s[cn++];
      x --;
    }


    for(int i = 1; i <= n1+1; ++i) {
      for(int j = 1; j <= n2+2; ++j) {
        if(!mp[i][j]) printf(" ");
        else printf("%c", mp[i][j]);
      }
      printf("\n");
    }
  }
  return 0;
}

1032 这题如果是 字符相等不一定是后缀,必须指针相等

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
const int N = 1e6+5;



char Data[N]; int nx[N];
vector<char> ans[2];
vector<int> indx[2];



void dfs(int x, int tag) {
  if(x == -1) return;
  ans[tag].push_back(Data[x]);
  indx[tag].push_back(x);
  dfs(nx[x], tag);
}
int main() {
  int s, t, n;
  while(~scanf("%d %d %d", &s, &t, &n)) {
    ans[0].clear(); ans[1].clear();  
    indx[0].clear(); indx[1].clear();

    for(int i = 1; i <= n; ++i) {
      int a; char b; int c; 
      scanf("%d %c %d", &a, &b, &c);
      Data[a] = b; nx[a] = c;
    }


    dfs(s, 0);
    dfs(t, 1);


    int Ans = -1;
    for(int i = ans[0].size()-1, j = ans[1].size()-1; i >= 0 && j >= 0; --j, --i) {
      if(indx[0][i] != indx[1][j]) {
        break;
      }else {
        Ans = indx[0][i];
      }
    } 
    if(Ans == -1) printf("-1\n");
    else printf("%05d\n", Ans);
  }
  return 0;
}

1033这题有人水的吧!一点不好做。这个贪心我是想了一晚上,具体看代码吧,我无力吐槽,x是上次加油的点,premm和mm是这次我上次加油的点会加多少油的区间

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

using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 505;
typedef long long ll;


struct Node{  
  double p; int d;
}E[N];

int cmp(Node a, Node b) {
  return a.d < b.d;
}
int main() {
  int cap, dis, v, n;
    while(~scanf("%d %d %d %d", &cap, &dis, &v, &n)) {
    for(int i = 1; i <= n; ++i) {
      scanf("%lf %d", &E[i].p, &E[i].d);
    }

    E[0].p = INF; E[0].d = 0;
    sort(E+1, E+n+1, cmp);

    if(E[1].d != 0) {
      printf("The maximum travel distance = 0.00\n");
      continue;
    }

    int x = 0;
    int premm = 0;
    int mm = 0;  double ans = 0;
    while(1) {
      int fr = x+1, to;
  //    printf("%d %d %d %.2f\n", x, premm, mm, ans);
      for(int i = x+1; i <= n; ++i) {
        if(E[i].d <= mm) {
          to = i; 
        }else break;
      }
      double minn = INF*1.0; int minp = 0;
      for(int i = fr; i <= to; ++i) {
        if(E[i].p < E[x].p) {
          minp = i; minn = E[i].p;
          break;
        }
      }

  //    printf("%d to %d: %d\n", fr, to, minp);  
      if(!minp) {
        for(int i = fr; i <= to; ++i) {
          if(minn > E[i].p) {
            minp = i; minn = E[i].p;
          }
        }
    //    printf("chpos: %d\n", minp);
        if(mm >= dis) {
          ans += (dis - premm)*1.0 /v *E[x].p;
          break;
        }else if(!minp) break;
        else {
    //      printf("hh %d %d\n", E[minp].d, E[x].d);
          ans += (mm - premm)*1.0 /v * E[x].p;
          premm = mm;
          mm += E[minp].d -E[x].d;
          x = minp;
    //      printf("%d\n", mm);
        }

      }else {
        ans += (E[minp].d - premm)*1.0 /v *E[x].p;
        premm = E[minp].d;
        x = minp;
        mm = E[minp].d + cap * v;
      }

    }

    if(mm < dis) printf("The maximum travel distance = %.2f\n", mm*1.0 );
    else printf("%.2f\n", ans);
  }
  return 0;
}

1034 并查集一下,无法理解正确率和上一题一样,我觉得我做到现在最难的是1033

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<cstdio>
using namespace std;
const int N = 2e3+5;
#define mp(A,B) make_pair(A,B)


map<string, int> mp;
char name[N][10];
int tot = 0;

int pre[N];
int fa(int x) {
  return pre[x] == x? x: pre[x] = fa(pre[x]);
}
int cnt[N];
vector<int> gang[N];


struct Node{
  char nam[5];
  int per;
}E[N];
int alltot;
int cmp(Node a,Node b) {
  for(int i = 0; i < 3; ++i) {
    if(a.nam[i] != b.nam[i]) {
      return a.nam[i] < b.nam[i];
    }
  }
}

int main() {
  int n, k;
  while(~scanf("%d %d", &n, &k)) {
    alltot = 0;
    memset(cnt, 0, sizeof(cnt));
    tot = 0; mp.clear();

    for(int i = 1; i <= 2*n; ++i) pre[i] = i;
    for(int i = 1; i <= 2*n; ++i) gang[i].clear();

    for(int i = 0; i < n; ++i) {
      char a[10]; char b[10]; int c;
      scanf("%s %s %d", a, b, &c);    

      if(mp.find(a) == mp.end()) {
        tot ++; mp[a] = tot;
        for(int j = 0; j < 3; ++j) name[tot][j] = a[j];
      }
      if(mp.find(b) == mp.end()) {
        tot ++; mp[b] = tot;
        for(int j = 0; j < 3; ++j) name[tot][j] = b[j];
      }
      int a1 = mp[a]; int b1 = mp[b];

      int t1 = fa(a1); int t2 = fa(b1);      
      if(t1 != t2) pre[t1] = t2;
      cnt[a1] += c; cnt[b1] += c;  
    }

    for(int i = 1; i <= tot; ++i) {
      gang[fa(i)].push_back(i);
    }
    for(int i = 1; i <= tot; ++i) {
      if(gang[i].size() > 2) {
        int maxx = -1; int maxp;
        int all = 0;
        for(int j = 0; j < gang[i].size(); ++j) {
          if(maxx < cnt[gang[i][j]] ) {
            maxx = cnt[gang[i][j]]; maxp =  j;
          }
          all += cnt[gang[i][j]];
        }
        all /= 2;
        if(all <= k) continue;

        alltot ++;
        E[alltot].per = gang[i].size();
        for(int j = 0; j < 3; ++j) {
          E[alltot].nam[j] = name[gang[i][maxp]][j];
        }
      }
    }
    if(alltot == 0) {
      printf("0\n"); continue;
    }

    sort(E+1, E+alltot+1, cmp);
    printf("%d\n", alltot);

    for(int i = 1; i <= alltot; ++i) {
      for(int j = 0; j < 3; ++j) printf("%c", E[i].nam[j]); printf(" ");
      printf("%d\n", E[i].per);
    }
  }
  return 0;
}

1035 there is /are 注意下

#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 = 505;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)

vector<pair<string, string> > vc;
int main() {
  int n;
  while(~scanf("%d", &n)) {
    vc.clear();
    for(int i = 0; i < n; ++i) {
      char a[20]; char b[20];
         scanf("%s %s", a, b);
      int len = strlen(b);
      int fl = 0;
      for(int j = 0; j < len; ++j) {
        if(b[j] == '1') {
          fl = 1; b[j] = '@';
        }else if(b[j] == '0') {
          fl = 1; b[j] = '%';  
        }else if(b[j] == 'l') {
          fl = 1; b[j] = 'L';
        }else if(b[j] == 'O') {
          fl = 1; b[j] = 'o';
        }
      }  

      if(fl) vc.push_back(MP(a, b));
    }

    if(vc.size() == 0) {
      if(n == 1) printf("There is %d account and no account is modified\n", n);
      else       printf("There are %d accounts and no account is modified\n", n);
    }else {
      printf("%d\n", vc.size());
      for(int i = 0; i < vc.size(); ++i) {
        printf("%s %s\n", vc[i].first.c_str(), vc[i].second.c_str());
      }
    }
  }
  return 0;
}

1036

#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 = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)

struct Node{
  char name[20];
  char sex;
  char Id[20];
  int grade;
}E[N];
int main() {
  int n;
  while(~scanf("%d", &n)) {
    int minn = INF; int minp = 0;
    int maxn = -1; int maxp = 0;

    for(int i = 1; i <= n; ++i) {
      scanf("%s %c %s %d", E[i].name, &E[i].sex, E[i].Id, &E[i].grade);
      if(E[i].sex == 'M') {
        if(E[i].grade < minn) {
          minn = E[i].grade; minp = i;
        }
      }else {
        if(E[i].grade > maxn) {
          maxn = E[i].grade; maxp = i;
        }
      }
    }

    if(!maxp) printf("Absent\n");
    else printf("%s %s\n", E[maxp].name, E[maxp].Id);

    if(!minp) printf("Absent\n");
    else printf("%s %s\n", E[minp].name, E[minp].Id);

    if(!minp || !maxp) printf("NA\n");
    else printf("%d\n", E[maxp].grade - E[minp].grade);
  }
  return 0;
}

1037

#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 = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)

int A[N];
int B[N];
int main() {
  int n, p;
  while(~scanf("%d",  &n)) {
    for(int i = 1; i <= n; ++i) scanf("%d", &A[i]);
    scanf("%d", &p);
    for(int i = 1; i <= n; ++i) scanf("%d", &B[i]);

    sort(A+1, A+n+1);
    sort(B+1, B+n+1);

    ll ans = 0;
    for(int i = 1; i <= n; ++i) {
      ll tmp = 1ll *A[i] *B[i];

      if(tmp > 0) ans += tmp;
    }
    printf("%lld\n", ans);
  }
  return 0;
}

1038 我瞎想的排序方法,不过觉得很有道理

#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 = 1e5+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)

struct Node{
  char num[10];
}s[N];

int cmp(Node A, Node B) {
  char a[20]; char b[20];
  int cnt;
  int l1 = strlen(A.num); int l2 = strlen(B.num);
  cnt = 0;
  for(int i = 0; i < l1; ++i) a[cnt++] = A.num[i];
  for(int i = 0; i < l2; ++i) a[cnt++] = B.num[i];

  cnt = 0;
  for(int i = 0; i < l2; ++i) b[cnt++] = B.num[i];
  for(int i = 0; i < l1; ++i) b[cnt++] = A.num[i];

  int l = strlen(a);


  for(int i = 0; i < l; ++i) {
    if(a[i] != b[i])  
      return a[i] < b[i];
  }
  return 1;
}

int main() {
  int n;
  while(~scanf("%d", &n)) {
    for(int i = 1; i <= n; ++i) {
      scanf("%s", s[i].num);
    }
    sort(s+1, s+n+1, cmp);

    int fl = 0;
    for(int i = 1; i <= n; ++i) {
      for(int j = 0; s[i].num[j]; ++j) {
        if(s[i].num[j] != '0') {
          fl = 1; printf("%c", s[i].num[j]);
        }else if(fl) {
          printf("0");
        }
      }
    }
    if(!fl) printf("0");
    printf("\n");  
  }
  return 0;
}

1039 加了一点小优化卡过去的,有大神能进100ms吗,我才170,

#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 = 4e4+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)

int n, k;
set<int> stu[N];
char nam[N][20];
map<int, int> ask;
vector<int> vc[3000];
set<int> ::iterator it;

int change(char a[]) {
//  for(int i = 0; i < 4; ++i) printf("%c", a[i]); printf("\n");
  int tt = 0;
  for(int i = 0; i < 3; ++i) {
    tt = tt*100+ a[i]-'A';
  }

  tt = tt*100 + 30+a[3]-'0';
  return tt;
}
int main() {
//  printf("%d\n", change("ZZZ9"));
  while(~scanf("%d %d", &n, &k)) {
  //  ask.clear();
  //  for(int i = 1; i <= k; ++i) vc[i].clear();
  //  for(int i = 1; i <= n; ++i) stu[i].clear();

    for(int i = 1; i <= k; ++i) {
      int a,b; scanf("%d %d", &a, &b);
      char s[10];
         for(int j = 0; j < b; ++j) {
        scanf("%s" , s);
        vc[a].push_back(change(s));
      }      
    }
    for(int i = 1; i <= n; ++i) {
      scanf("%s", nam[i]);
      ask[change(nam[i])] = i;
    }

    for(int i = 1; i <= k; ++i) {
      for(int j = 0; j < vc[i].size(); ++j) {
        int tt = vc[i][j];
        if(ask.find(tt) != ask.end()) {
          stu[ask[tt]].insert(i);
        }
      }
    }

    for(int i = 1; i <= n; ++i) {

      printf("%s %lu", nam[i], stu[i].size());

    //  sort(stu[i].begin(), stu[i].end());
      for(it = stu[i].begin(); it != stu[i].end(); ++it) {
        printf(" %d", *it);
      //  int tt = stu[i][j];
      //  printf(" %d", tt);
      }
      printf("\n");
    }  
  }
  return 0;
}

1040 manacher 搞一搞

#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 = 1e4+5;
const int INF = 0x3f3f3f3f;
#define MP(x, y) make_pair(x, y)

char s[N];
char Ma[N*2];
int Mp[N*2];
void Manacher() {
  int len = strlen(s);
  int l = 0;
  Ma[l++] = '$';
  Ma[l++] = '#';
  for(int i = 0; i < len; ++i) {
    Ma[l++] = s[i];
    Ma[l++] = '#';
  }
  Ma[l] = 0;
  int mx = 0, id = 0;
  for(int i = 0; i < l; ++i) {
    Mp[i] = mx>i? min(Mp[2*id-1], mx-i) : 1;
    while(Ma[i+Mp[i]] == Ma[i-Mp[i]]) Mp[i] ++;
    if(i + Mp[i] > mx) {
      mx = i + Mp[i];
      id = i;
    }
  }
}   
int main() {
  gets(s);
  int len = strlen(s);
  Manacher();
  int ans = 0;
  for(int i = 0; i < 2*len+2; ++i) {
    ans = max(ans, Mp[i]-1);
  }
  printf("%d\n", ans);
  return 0;
}
posted @ 2017-06-03 15:15  basasuya  阅读(154)  评论(0编辑  收藏  举报