pat 1001-1010

最近有点神志无知
命运中很多事情真是奇妙
我必须改变自己的状态
1001 简单的模拟

#include<bits/stdc++.h>
using namespace std;

int main() {
  int a,b;
  while(~scanf("%d %d",&a,&b)) {
    int tt = a+b;
    if(tt == 0) {
      printf("0\n"); continue;
    }
    int fl = 1;
    if(tt < 0) fl = 0;
    vector<int> ans;
    tt = abs(tt);
    int cnt = 0;
  //  printf("%d\n", tt);
    while(tt) {
      cnt ++;

      if(cnt %3 == 0) ans.push_back(100+(tt%10)); 
      else ans.push_back(tt%10);

      tt /= 10;
    }
    if(!fl) printf("-");
    for(int i = ans.size()-1; i >= 0; --i) {
      if(ans[i] >= 100 && (i != ans.size()-1) ) printf(",");
      if(ans[i] >= 100) ans[i] -= 100;
      printf("%d",abs(ans[i]));
    }
    printf("\n");
  }
  return 0;
}

1002 同样模拟

#include<bits/stdc++.h>
using namespace std;

double has[1005];
int main() {
  int k;
  memset(has, 0, sizeof(has));
  scanf("%d", &k);
  for(int i = 1; i <= k; ++i) {
    int a; double b; scanf("%d %lf",&a,&b);
    has[a] += b;
  }
  scanf("%d", &k);
  for(int i = 1; i <= k; ++i) {
    int a; double b; scanf("%d %lf",&a,&b);
    has[a] += b;
  }
  int all = 0;
  for(int i = 0; i <= 1000; ++i) {
    if(has[i] != 0) all ++;
  }
  printf("%d",all);
  for(int i = 1000; i >= 0; --i) {
  //  double tt = ( (int)(has[i]*10) )*1.0 / 10;
    if(has[i]) printf(" %d %.1f", i, has[i]);
  }
  printf("\n");
}

1003 我写的是改动的dijstra,我想想dij+dfs也可以

#include<bits/stdc++.h>
using namespace std;
#define sz(X) ((int)X.size())
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 505;

int rescue[N];
struct Edge{
  int nx, to, dis;
}E[N*N*2];
int head[N], tot;
void add(int fr, int to, int dis) {
  E[tot].to = to; E[tot].dis = dis; E[tot].nx = head[fr]; head[fr] = tot++;
}
struct Node{
  int d, n, u;
  Node(int a=0, int b=0, int c=0):d(a), n(b), u(c){}
  bool operator < (const Node& T) const {
    if(d != T.d) return d > T.d;
    else return n < T.n;
  }
};
int d[N]; int cnt[N]; int num[N];
int vis[N];
void dijstra(int st) {
  priority_queue<Node> Q;
  memset(d, INF, sizeof(d));
  memset(cnt, 0, sizeof(cnt));
  memset(num, 0, sizeof(num));
  memset(vis, 0, sizeof(vis));

  d[st] = 0; num[st] = rescue[st]; cnt[st] = 1;
  for(int i = 0; i < N; ++i) cnt[i] = 1;
  Q.push(Node(d[st], num[st], st));
  while(!Q.empty()) {
    int x = Q.top().u; Q.pop();
    if(vis[x]) continue;
    vis[x] = 1;
    for(int i = head[x]; ~i; i = E[i].nx) {
      int to = E[i].to; int fl = 1;
      if(d[to] == d[x]+E[i].dis) cnt[to] += cnt[x];
      if(d[to] > d[x]+E[i].dis) {
        d[to] = d[x]+E[i].dis;
        num[to] = num[x]+rescue[to];
        cnt[to] = cnt[x];
      }else if(d[to] == d[x]+E[i].dis && num[to] < num[x]+rescue[to]) {
        num[to] = num[x]+rescue[to]; 
      }else fl = 0;
      if(fl) Q.push(Node(d[to], num[to], to));
    }
  }
}
int main() {
  int n, m, c1, c2;
  while(~scanf("%d %d %d %d",&n, &m, &c1, &c2)) {
    memset(head, -1, sizeof(head)); tot = 0;
    for(int i = 0; i < n; ++i) scanf("%d", &rescue[i]);

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

    dijstra(c1);
    printf("%d %d\n", cnt[c2], num[c2]);
  }
  return 0;
}

1004 bfs即可

#include<bits/stdc++.h>
using namespace std;
const int N = 105;

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 n,m;
int vis[N];
int du[N];
void bfs(int x) {
  memset(vis, 0, sizeof(vis));
  queue<int> Q;
  Q.push(x);
  vis[x] = 1;
  while(!Q.empty()) {
    vector<int> tmp; tmp.clear();
    if(Q.front() != 1) printf(" ");

    while(!Q.empty()) {
      int po = Q.front(); Q.pop(); tmp.push_back(po);
    }
    int all = 0;
    for(int i = 0; i < tmp.size(); ++i) {
      int po = tmp[i];
      if( (po == 1 && du[po] == 0) || (po != 1 && du[po] == 1) ) all ++;

      for(int j = head[po]; ~j; j = E[j].nx) {
        int to = E[j].to;
        if(!vis[to]) {
          vis[to] = 1;
          Q.push(to);
        }
      }
    }
    printf("%d",all);
  }
  printf("\n");
}
int main() {
  while(~scanf("%d %d", &n, &m)) {
    memset(head, -1, sizeof(head)); tot = 0;
    memset(du, 0, sizeof(du));
    for(int i = 1; i <= m; ++i) {
      int a; scanf("%d",&a);
      int tim; scanf("%d", &tim);
      for(int j = 1; j <= tim; ++j) {
        int b; scanf("%d", &b);
        add(a, b); add(b, a);
        du[a] ++; du[b] ++;
      }
    }
    bfs(1);
  }
  return 0;
}

1005 水题

#include<bits/stdc++.h>
using namespace std;

char s[105];
char a[][50] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int main() {
  while(~scanf("%s", s)) {
    int len = strlen(s);
    int num = 0;
    for(int i = 0; i < len; ++i) {
      num += s[i]-'0';
    }
    vector<int> ans;
    if(num == 0) {
      printf("zero\n"); continue;
    }
    while(num) {
      ans.push_back(num%10);
      num /= 10;
    }

    for(int i = ans.size()-1; i >= 0; --i) {
      if(i != ans.size()-1) printf(" ");
      printf("%s",a[ans[i]]);
    }
    printf("\n");
  }
  return 0;
}

1006 水题

#include<bits/stdc++.h>
using namespace std;
const int N = 105;
const int INF = 0x3f3f3f3f;

char s[N][20]; int tim[N];
int has[N];

int Time[100000];
int Tag[100000];
int main() {
  int m;
  while(~scanf("%d", &m)) {
    memset(Time, 0, sizeof(Time));
    memset(Tag, 0, sizeof(Tag));
    memset(has, 0, sizeof(has));


    int a1 = 0; int t1 = INF; 
    int a2 = 0; int t2 = -1;

    for(int i = 0; i < m; ++i) {
      scanf("%s", s[i]);
      int a,b,c, d,e,f;

      scanf("%d:%d:%d %d:%d:%d",&a,&b,&c,&d,&e,&f);
    //  printf("%d %d %d %d %d %d\n", a, b, c, d, e, f);
      int tim = a*60*60 + b*60 + c;
      if(tim < t1) {
        t1 = tim; a1 = i;
      }  

      tim = d*60*60 + e*60 + f;
      Time[tim] --; Tag[tim] = i+1;
      if(tim > t2) {
        t2 = tim; a2 = i;
      }
    }

    printf("%s %s\n", s[a1], s[a2]);
  }
  return 0;
}

1007 大体是个dp,需要稍微改改

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
const int INF = 0x3f3f3f3f;

int a[N];
int dp[N]; int st[N];
int main() {
  int k;
  while(~scanf("%d", &k)) {
    int fl = 0;
    for(int i = 0; i < k; ++i) {
      scanf("%d",&a[i]);
      if(a[i] >= 0) fl = 1;
    }
    if(fl == 0) {
      printf("0 %d %d\n",a[0],a[k-1]);
      continue;
    }

    for(int i = 0; i < k; ++i) {
      int pre = i?dp[i-1]:0;

      if(pre <= 0) {
        dp[i] = a[i]; st[i] = i;
      }else {
        dp[i] = pre+a[i]; st[i] = st[i-1];
      }
    }
    int t1,t2,t3; t1 = -1;
    for(int i = 0; i < k; ++i) {
      if(t1 < dp[i]) {
        t1 = dp[i]; t2 = st[i]; t3 = i;
      }else if(t1 == dp[i] && t2 > st[i]) {
        t2 = st[i]; t3 = i;
      }else if(t1 == dp[i] && t2 == st[i] && t3 > i) {
        t3 = i;
      }
    }

    printf("%d %d %d\n", t1, a[t2], a[t3]);
  }
  return 0;
}

1008 水题

#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+5;
const int INF = 0x3f3f3f3f;

int main() {
  int n;
  while(~scanf("%d", &n)) {
    int pre = 0;
    int ans = 0;
    for(int i = 0; i < n; ++i) {
      int a; scanf("%d", &a);
      if(a == pre) ans += 5;
      else if(a < pre) {
        ans += (pre-a)*4 + 5;
      }else {
        ans += (a-pre)*6 + 5;
      }

      pre = a;
    }
    printf("%d\n", ans);
  }
  return 0;
}

1009

#include<bits/stdc++.h>
using namespace std;

int n1[15]; double b1[15];
int n2[15]; double b2[15];
double mp[2005];
int main() {
  int k;
  scanf("%d", &k);
  for(int i = 1; i <= k; ++i) {
    scanf("%d %lf",&n1[i],&b1[i]);
  }
  scanf("%d",&k);
  for(int i = 1; i <= k; ++i) {
    scanf("%d %lf",&n2[i],&b2[i]);
  }

  for(int i = 1; i <= k; ++i) {
    for(int j = 1; j <= k; ++j) {
      mp[n1[i]+n2[j]] += b1[i]*b2[j];
    }
  }

  int all = 0;
  for(int i = 2000; i >= 0; --i) {
    if(mp[i] != 0) all++;
  }
  printf("%d", all);
  for(int i = 2000; i >= 0; --i) {
    if(mp[i] != 0) printf(" %d %.1f", i, mp[i]);
  }
  printf("\n");
}

1010 二分好题,一开始还以为会有大数,改了半天

#include<bits/stdc++.h>
using namespace std;
const int N = 105;
typedef unsigned long long ll;
const int MOD = 1e9+7;

int C(char c) {
  if(c <= '9') return c-'0';
  else return c-'a'+10;
} 
int main() {
  int tag, radix;
  char s1[15]; char s2[15];
  while(~scanf("%s %s %d %d", s1, s2, &tag, &radix)) {
    if(tag == 2) {
      for(int i = 0; i < 15; ++i) swap(s1[i], s2[i]);
    }
    int l1 = strlen(s1); int l2 = strlen(s2);

    ll ans = 0;
    for(int i = 0; i < l1; ++i) {
      ans = ans*radix + C(s1[i]);
      if(ans > 1e18) while(1);
    }

    int mincap = 0;
    for(int i = 0; i < l2; ++i) {
      mincap = max(mincap, C(s2[i])+1);
    }

    ll l = mincap; ll r = ans;
    while(l <= r) {
      ll mid = (l+r) >> 1;
      ll tmp = 0;
      for(int j = 0; j < l2; ++j) {
        tmp = tmp*mid + C(s2[j]);
        if(tmp > ans) {
          break;
        }
      }
      if(tmp >= ans) r = mid-1;
      else l = mid+1;
    }

    ll tmp = 0;
    for(int j = 0; j < l2; ++j) {
      tmp = tmp*l + C(s2[j]);
    }


    if(ans != tmp) printf("Impossible\n");
    else printf("%lld\n",l);
  }
  return 0;
}
posted @ 2017-05-10 00:11  basasuya  阅读(131)  评论(0编辑  收藏  举报