pat1021-1030

1021求树的直径网上一搜就有,但是我不太理解 只需要一共求两次的dfs的论调,好吧我收回这句话,好想脑补了下,第一次dfs有多个最长点,只需要搜一个就行QAQ。这么看来我写麻烦了

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
#define FI first
#define SE second
const int INF = 0x3f3f3f3f;
const int N = 1e4+5;

int n;
struct Node{
    int to, nx;
}E[N*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 vis[N], dis[N];
void dfs(int x) {
    for(int i = head[x]; ~i; i = E[i].nx) {
        int to = E[i].to;
        if(!vis[to]) {
            vis[to] = 1;
            dfs(to);
        }
    }
}

void bfs(int st) {
    queue<int> Q;
    memset(vis, 0, sizeof(vis));
    vis[st] = 1; dis[st] = 0;
    Q.push(st);
    while(!Q.empty()) {
        int x = Q.front(); Q.pop();

        for(int i = head[x]; ~i; i = E[i].nx) {
            int to = E[i].to;
            if(!vis[to]) {
                vis[to] = 1;
                dis[to] = dis[x]+1;
                Q.push(to);
            }
        }
    }
}
void solve() {
    bfs(1);
    map<int, int> mp;
    map<int, int>::iterator it;
    int mx = -1;
    for(int i = 1; i <= n; ++i) {
        if(dis[i] > mx) {
            mx = dis[i];
        }
    }
    vector<int> doo;
    for(int i = 1; i <= n; ++i) {
        if(dis[i] == mx) {
            doo.push_back(i);
        }
    }
   // printf("%d\n", mx);
   int cnt = 0;
    for(int i = 0; i < doo.size(); ++i) {
        cnt ++;
        if(cnt > 100) break;
        bfs(doo[i]);
        mp[doo[i]] ++;
        mx = -1;
        for(int j = 1; j <= n; ++j) {
            if(mx < dis[j]) {
                mx = dis[j];
            }
        }
        for(int j = 1; j <= n; ++j) {
            if(dis[j] == mx) {
                mp[j] ++;
            }
        }
    }

    for(it = mp.begin(); it != mp.end(); ++it) {
      //  if(it != mp.begin()) printf(" ");
        printf("%d\n", it->first);
    }
   // printf("\n");
}
int main() {
    while(~scanf("%d", &n)) {
        memset(head, -1, sizeof(head)); tot = 0;

        for(int i = 1; i < n; ++i) {
            int a,b; scanf("%d %d", &a, &b);
            add(a, b); add(b, a);
        }

        int cnt = 0;
        memset(vis, 0, sizeof(vis));
        for(int i = 1; i <= n; ++i) {
            if(!vis[i]) {
                vis[i] = 1;
                dfs(i);
                cnt ++;
            }
        }
        if(cnt > 1) {
            printf("Error: %d components\n", cnt); continue;
        }
     //   printf("%d\n", cnt);
        solve();
    }
    return 0;
}

1022 模拟一下就行

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define MP(x, y) make_pair(x, y)
#define FI first
#define SE second

map<string, vector<int> > title;
map<string, vector<int> > author;
map<string, vector<int> > publish;
map<string, vector<int> > keyword;
map<string, vector<int> > year;

int main() {
    int n;
    while(~scanf("%d", &n)) {
        title.clear();
        author.clear();
        publish.clear();
        keyword.clear();
        year.clear();

        char tmp[100];
        for(int i = 0; i < n; ++i) {
            int id; scanf("%d", &id);
            getchar();
            gets(tmp); title[tmp].push_back(id);
         //   printf("%s\n", tmp);

            gets(tmp); author[tmp].push_back(id);

            gets(tmp);
            int len = strlen(tmp);
            string tt; tt.clear();
            for(int j = 0; j <= len; ++j) {
                if( tmp[j] == '\n' || tmp[j] == ' ' || !tmp[j] ) {
                    keyword[tt].push_back(id);
                    tt.clear();
                }else tt += tmp[j];
            }

            gets(tmp); publish[tmp].push_back(id);
            gets(tmp); year[tmp].push_back(id);
        }

        int m; scanf("%d", &m);
        getchar();
        for(int i = 0; i < m; ++i) {
            gets(tmp);
            int len = strlen(tmp);
            int ch = tmp[0]-'0';
            for(int j = 0; j < len; ++j) {
                tmp[j] = tmp[j+3];
            }
            tmp[len-3] = 0;
            printf("%d: %s\n", ch, tmp);
            int fl = 0;
            if(ch == 1) {
                sort(title[tmp].begin(), title[tmp].end());
                for(int j = 0; j < title[tmp].size(); ++j) {
                    printf("%07d\n", title[tmp][j]); fl = 1;
                }
            }else if(ch == 2) {
                sort(author[tmp].begin(), author[tmp].end());
                for(int j = 0; j < author[tmp].size(); ++j) {
                    printf("%07d\n", author[tmp][j]); fl = 1;
                }
            }else if(ch == 3) {
                sort(keyword[tmp].begin(), keyword[tmp].end());
                for(int j = 0; j < keyword[tmp].size(); ++j) {
                    printf("%07d\n", keyword[tmp][j]); fl = 1;
                }
            }else if(ch == 4) {
                sort(publish[tmp].begin(), publish[tmp].end());
                for(int j = 0; j < publish[tmp].size(); ++j) {
                    printf("%07d\n", publish[tmp][j]); fl = 1;
                }
            }else {
                sort(year[tmp].begin(), year[tmp].end());
                for(int j = 0; j < year[tmp].size(); ++j) {
                    printf("%07d\n", year[tmp][j]); fl = 1;
                }
            }
            if(!fl) printf("Not Found\n");
        }
    }
    return 0;

1023 没啥好说

#include<bits/stdc++.h>
using namespace std;
#define sz(X) ((int)X.size())
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 1e4+5;
#define MP(x, y) make_pair<x, y>

char s[30];
char b[30];
char a[30];
int main() {
  while(~scanf("%s", s)) {
    int len = strlen(s);
    int pre = 0;
    for(int i = len-1; i >= 0; --i) {
      int tt = (s[i]-'0') * 2 + pre;
      if(tt >= 10) {
        int tmp = tt;
        tt = tmp%10;
        pre = tmp/10;
      }else pre = 0;
      b[i] = tt+'0';
    }

//    for(int i = 0; i < len; ++i) printf("%c", b[i]); printf("\n");
    int ok = 0;
    if(pre) printf("No\n");
    else {
      for(int i = 0; i < len; ++i) {
        a[i] = b[i];
      }
      sort(a, a+len);
      sort(s, s+len);
      int fl = 1;
      for(int i = 0; i < len; ++i) {
        if(a[i] != s[i]) {
          fl = 0; break;
        }
      }

      if(!fl) printf("No\n");
      else {
        printf("Yes\n"); ok = 1;
        for(int j = 0; j < len; ++j) printf("%c", b[j]);
        printf("\n");
      }
    }

    if(!ok) {
      if(pre) printf("%d",pre);
      for(int i = 0; i < len; ++i) printf("%c", b[i]);
      printf("\n");
    }

  }
  return 0;
}

1024 不敢乱讲,但是这题好想真的数据范围不对,按照题意不可能超ll的吧,,之后改了就对了

#include<bits/stdc++.h>
using namespace std;
#define MP(x, y) make_pair(x, y)
#define FI first
#define SE second
const int INF = 0x3f3f3f3f;
const int N = 1e4+5;
typedef long long ll;

int n[50]; int len;
char s[50];
int tmp[50];
void Print() {
    for(int i = 0; i < len; ++i) printf("%d", n[i]);
}
int main() {
    int k;
    while(~scanf("%s %d", s, &k)) {
        len = strlen(s);
        for(int i = 0; i < len; ++i) n[i] = s[i]-'0';

        int fl = 1;
        for(int i = 0; i < len/2; ++i) {
            if(n[i] != n[len-1-i]) {
                fl = 0; break;
            }
        }
        if(fl) {
            Print(); printf("\n0\n");
            continue;
        }

        for(int i = 1; i <= k; ++i) {
            int pre = 0;
            for(int j = len-1; j >= 0; --j) {
                tmp[j] = n[j]+n[len-1-j]+pre;
                if(tmp[j] >= 10) {
                    tmp[j] -= 10;
                    pre = 1;
                }else pre = 0;
            }

            int cnt = 0;
            if(pre) n[cnt++] = pre;
            for(int j = 0; j < len; ++j) {
                n[cnt++] = tmp[j];
            }
            len = cnt;

            int fl = 1;
            for(int j = 0; j < len/2; ++j) {
                if(n[j] != n[len-1-j]) {
                    fl = 0; break;
                }
            }
            if(fl || i == k) {
                Print(); printf("\n%d\n",i);
                break;
            }
        }
    }
    return 0;
}

1025 模拟

#include<bits/stdc++.h>
using namespace std;
#define MP(x, y) make_pair(x, y)
#define FI first
#define SE second
const int INF = 0x3f3f3f3f;
const int N = 1e4+5;
typedef long long ll;

struct Node{
    ll s; int grade; int loc; int locrank; int allrank;
}E[30005];
int tot;
int pretot;
int cmp(Node a, Node b) {
    if(a.grade != b.grade) return a.grade > b.grade;
    else return a.s < b.s;
}
int main() {
    int n;
    while(~scanf("%d", &n)) {
        pretot = 1; tot = 0;
        for(int i = 1; i <= n; ++i) {
            int k; scanf("%d", &k);
            for(int j = 0; j < k; ++j) {
                ++tot; scanf("%lld %d", &E[tot].s, &E[tot].grade);
            //    printf("%s\n", E[tot].s);
                E[tot].loc = i;
            }
            sort(E+pretot, E+tot+1, cmp);
            for(int j = pretot; j <= tot; ++j) {
                if(j != pretot && E[j].grade == E[j-1].grade) {
                    E[j].locrank = E[j-1].locrank;
                }else E[j].locrank = j-pretot+1;
            }
            pretot = tot+1;
        }
        sort(E+1, E+tot+1, cmp);
        for(int i = 1; i <= tot; ++i) {
            if(i != 1 && E[i].grade == E[i-1].grade) {
                E[i].allrank = E[i-1].allrank;
            }else E[i].allrank = i;
        }

        printf("%d\n", tot);
        for(int i = 1; i <= tot; ++i) {
            printf("%013lld %d %d %d\n", E[i].s, E[i].allrank, E[i].loc, E[i].locrank);
        }
    }
    return 0;
}

1026做了半天,因为之前一直想按照前面几个题目的做法,直接对人来的时间进行排序。这题我是按照时间点进行处理,桌子来,或者人来都是时间点,对于每个时间点单独处理。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <queue>
#include <cmath>
using namespace std;
const int N = 1e4+5;
const int INF = 0x3f3f3f3f;

struct Node{
  int come;
  int serve;
  int cost;
  int vip;  
  int vis;
  int id;
}E[N];
int cmp(Node a, Node b) {
  return a.come < b.come;
}
int cmp2(Node a, Node b) {
  return a.serve < b.serve;
}
int V[105];
int ans[105];
struct Pode{
  int pos, tim, type, vip;
  Pode(int a=0, int b=0, int c=0, int d=0):pos(a), tim(b), type(c), vip(d){}
  bool operator <(const Pode &T) const {
    if(tim != T.tim) return tim > T.tim;
    else if(type != T.type) return type > T.type;
    else if(vip != T.vip) return vip > T.vip;
    else return pos > T.pos;
  }
};


set<int> table;
set<int> vtable;
set<int> person;
set<int> vperson;
priority_queue<Pode> Q;

void solve(int nwtime) {
  if(vperson.size() > 0 && vtable.size() > 0) {
    int per = *vperson.begin();
    E[per].serve = nwtime;
    int ta = *vtable.begin();

    Q.push(Pode(ta, nwtime+E[per].cost, 0, V[ta]));

    vperson.erase(per); person.erase(per);
    vtable.erase(ta);  table.erase(ta);
    if(nwtime < 21*3600) ans[ta] ++;
    return;
  }

  int per = *person.begin();
  E[per].serve = nwtime;
  int ta = *table.begin();
  Q.push(Pode(ta, nwtime+E[per].cost, 0, V[ta]));

  vperson.erase(per); person.erase(per);
  vtable.erase(ta);   table.erase(ta);
  if(nwtime < 21*3600) ans[ta] ++;
}
void put(int x) {
  printf("%02d:%02d:%02d ", x/3600, (x%3600)/60, x%60);
}
int main() {
  int n;
  int k, m;
  while(~scanf("%d", &n)) {
    memset(ans, 0, sizeof(ans));
    memset(V, 0, sizeof(V));
    while(!Q.empty()) Q.pop();
    table.clear();
    vtable.clear();
    person.clear();
    vperson.clear();

    for(int i = 1; i <= n; ++i) {
      int a, b, c;
      scanf("%d:%d:%d %d %d", &a, &b, &c, &E[i].cost, &E[i].vip);
      E[i].come = a*3600 + b*60 + c;
      E[i].cost = min(120, E[i].cost);
      E[i].cost = E[i].cost*60;
      E[i].id = i;
      E[i].vis = 0;
    }  
    sort(E+1, E+n+1, cmp);
    scanf("%d %d", &k, &m);
    for(int i = 0; i < m; ++i) {
      int a; scanf("%d", &a);
      V[a] ++;
    }
    for(int i = 1; i <= k; ++i) Q.push(Pode(i, 8*3600, 0, V[i]));

    for(int i = 1; i <= n; ++i) Q.push(Pode(i, E[i].come, 1, E[i].vip));

    while(!Q.empty()) {
      Pode tt = Q.top(); Q.pop();
      int id = tt.pos; int nwtime = tt.tim; int ty = tt.type;
    //  printf("%d %d %d\n", id, nwtime, ty);
      if(tt.type == 0) {
        table.insert(id);
        if(V[id]) vtable.insert(id);
        if(table.size() > 0 && person.size() > 0) solve(nwtime);
      }else {
        person.insert(id);
        if(E[id].vip) vperson.insert(id);
        if(table.size() > 0 && person.size() > 0) solve(nwtime);
      }
    }
    sort(E+1, E+n+1, cmp2);

    for(int i = 1; i <= n; ++i) {
      if(E[i].serve >= 21*3600) break;
      put(E[i].come); put(E[i].serve);
    //  printf("%.3f\n", (E[i].serve-E[i].come)/60.0);
      printf("%.f\n", round((E[i].serve-E[i].come)/60.0));
    }
    for(int i = 1; i <= k; ++i) {
      if(i != 1) printf(" ");
      printf("%d", ans[i]);
    }
    printf("\n");
  }
  return 0;
}

1027

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

void put(int x) {
  int t[3]; int c = 0;
  memset(t, 0, sizeof(t));
  while(x) {
    t[c++] = x%13;
    x /= 13;  
  }

  if(t[1] >= 10) printf("%c", t[1]-10+'A');
  else printf("%d", t[1]);

  if(t[0] >= 10) printf("%c", t[0]-10+'A');
  else printf("%d", t[0]);
}

int main () {
  int a, b, c;
  while(~scanf("%d %d %d",&a,&b,&c)) {
    printf("#");

    put(a); put(b); put(c);

  }
  return 0;
}

1028

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

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

struct Node{
  int id;
  char name[20];
  int grade;
}E[N];
int n, c;
int cmp(Node a, Node b)  {  
  if(c == 1) return a.id < b.id;
  else if(c == 2) {
    int l1 = strlen(a.name); int l2 = strlen(b.name);

    for(int i = 0; i < min(l1, l2); ++i) {
      if(a.name[i] != b.name[i]) 
        return a.name[i] < b.name[i];
    }
    if(l1 != l2) return l1 < l2;
  }else if(a.grade != b.grade) return a.grade < b.grade;    
  return a.id < b.id;
}
int main() {
  while(~scanf("%d %d", &n, &c)) {
    for(int i = 1; i <= n; ++i) {
      scanf("%d %s %d", &E[i].id, E[i].name, &E[i].grade);
    }
    sort(E+1, E+n+1, cmp);

    for(int i = 1; i <= n; ++i) {
      printf("%06d %s %d\n", E[i].id, E[i].name, E[i].grade);
    }

  }
  return 0;
}

1029 查找中位数算法,算法导论上有

#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 = 2e6+5;
typedef long long ll;

ll a[N];


int rand_partion(int l, int r) {
  int randnum = (rand()%(r-l+1)) + l;
//  printf("%d\n", randnum);
  swap(a[r], a[randnum]);

  int fl = l-1;
  int tag = a[r];
  for(int i = l; i < r; ++i) {
    if(a[i] <= tag) {
      fl ++;
      swap(a[fl], a[i]);
    }
  }
  swap(a[fl+1], a[r]);
  return fl+1;
}
ll getMid(int l, int r, int tar) {
//  printf("%d %d %d\n", l, r, tar);
  if(l == r) return a[l];

  int po = rand_partion(l, r);
//  printf("%d:",po);
//  for(int i = l; i <= r; ++i) printf("%lld ", a[i]); printf("\n");

  int len = po-l+1;
  if(tar == len) return a[po];
  else if(tar < len) return getMid(l, po-1, tar);
  else return getMid(po+1, r, tar-len);
}
int main() {
  int n;
  srand(time(NULL));
  while(~scanf("%d", &n)) {
    for(int i = 1; i <= n; ++i) {
      scanf("%lld", &a[i]);
    }
    int m; scanf("%d", &m);
    for(int i = 1; i <= m; ++i) {
      scanf("%lld", &a[i+n]);
    }
    n = n+m;
  //  printf("%d\n", n);
    printf("%lld\n", getMid(1, n, (n+1)/2) );
  }
  return 0;
}

1030 双参数最短路,水一水

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

int n,m,s,d;
struct Node{
  int to, nx, dis, cost;
}E[N*N*2];
int head[N], tot;
void add(int fr, int to, int dis, int cost) {
  E[tot].to = to; E[tot].dis = dis; E[tot].cost = cost;
  E[tot].nx = head[fr]; head[fr] = tot++;
}
struct Hode{
  int pos, dis, cost;
  Hode(int a=0, int b=0, int c=0):pos(a),dis(b),cost(c){}
  bool operator < (const Hode &T) const {
    if(dis != T.dis) return dis > T.dis;
    else return cost > T.cost;
  }
};
int _dis[N], _cost[N], pre[N], vis[N];
void dijkstra(int x) {
  priority_queue<Hode> Q;
  memset(vis, 0, sizeof(vis));
  memset(_dis, INF, sizeof(_dis));
  memset(_cost, INF, sizeof(_cost));
  _dis[s] = 0, _cost[s] = 0;
  Q.push(Hode(s, _dis[s], _cost[s]));

  while(!Q.empty()) {
    Hode top = Q.top(); Q.pop();
    int x = top.pos; int dis = top.dis; int cost = top.cost;
    if(vis[x]) continue;
    vis[x] = 1;
    for(int i = head[x]; ~i; i = E[i].nx) {
      int to = E[i].to;

      if(_dis[to] > _dis[x] + E[i].dis) {
        _dis[to] = _dis[x] + E[i].dis;
        _cost[to] = _cost[x] + E[i].cost;
        Q.push(Hode(to, _dis[to], _cost[to]));
        pre[to] = x;
      }else if(_dis[to] == _dis[x] + E[i].dis && _cost[to] > _cost[x] + E[i].cost) {
        _cost[to] = _cost[x] + E[i].cost;
        Q.push(Hode(to, _dis[to], _cost[to]));
        pre[to] = x;
      }
    }
  }
}
void dfs(int x) {
  if(x == s) {
    printf("%d ", s); return;
  }
  dfs(pre[x]);
  printf("%d ", x);
}
int main() {
  while(~scanf("%d %d %d %d", &n, &m, &s, &d)) {
    memset(head, -1, sizeof(head)); tot = 0;
    for(int i = 0; i < m; ++i) {
      int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d);
      add(a, b, c, d); 
      add(b, a, c, d);
    }
  //  printf("hh\n");
    dijkstra(s);
    dfs(d);
    printf("%d %d\n", _dis[d], _cost[d]);
  }
  return 0;
}
技术选型 【后端】:Java 【框架】:springboot 【前端】:vue 【JDK版本】:JDK1.8 【服务器】:tomcat7+ 【数据库】:mysql 5.7+ 项目包含前后台完整源码。 项目都经过严格调试,确保可以运行! 具体项目介绍可查看博主文章或私聊获取 助力学习实践,提升编程技能,快来获取这份宝贵的资源吧! 在当今快速发展的信息技术领域,技术选型是决定一个项目成功与否的重要因素之一。基于以下的技术栈,我们为您带来了一份完善且经过实践验证的项目资源,让您在学习和提升编程技能的道路上事半功倍。以下是该项目的技术选型和其组件的详细介绍。 在后端技术方面,我们选择了Java作为编程语言。Java以其稳健性、跨平台性和丰富的库支持,在企业级应用中处于领导地位。项目采用了流行的Spring Boot框架,这个框架以简化Java企业级开发而闻名。Spring Boot提供了简洁的配置方式、内置的嵌入式服务器支持以及强大的生态系统,使开发者能够更高效地构建和部署应用。 前端技术方面,我们使用了Vue.js,这是一个用于构建用户界面的渐进式JavaScript框架。Vue以其易上手、灵活和性能出色而受到开发者的青睐,它的组件化开发思想也有助于提高代码的复用性和可维护性。 项目的编译和运行环境选择了JDK 1.8。尽管Java已经推出了更新的版本,但JDK 1.8依旧是一种成熟且稳定的选择,广泛应用于各类项目中,确保了兼容性和稳定性。 在服务器方面,本项目部署在Tomcat 7+之上。Tomcat是Apache软件基金会下的一个开源Servlet容器,也是应用最为广泛的Java Web服务器之一。其稳定性和可靠的性能表现为Java Web应用提供了坚实的支持。 数据库方面,我们采用了MySQL 5.7+。MySQL是一种高效、可靠且使用广泛的关系型数据库管理系统,5.7版本在性能和功能上都有显著的提升。 值得一提的是,该项目包含了前后台的完整源码,并经过严格调试,确保可以顺利运行。通过项目的学习和实践,您将能更好地掌握从后端到前端的完整开发流程,提升自己的编程技能。欢迎参考博主的详细文章或私信获取更多信息,利用这一宝贵资源来推进您的技术成长之路!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值