TYVJ 1007 排座椅 by C++

   1: #include<iostream>
   2: using namespace std;
   3: int m,n,k,l,d;
   4: const int maxn=3000;
   5: int hen[maxn]={0},shu[maxn]={0},posh[maxn]={0},poss[maxn]={0};
   6: bool opth[maxn]={0},opts[maxn]={0};
   7:  
   8: void get_prepare()
   9: {
  10:     int x1,y1,x2,y2;
  11:     cin >> n >> m >> k >> l >> d;
  12:     for (int i=0;i<d;i++){
  13:         cin >> x1 >> y1 >> x2 >> y2;
  14:         x1==x2?shu[(y1+y2)/2-1]++:hen[(x1+x2)/2-1]++;
  15:     }
  16:     for (int i=0;i<n;i++) posh[i]=i+1;
  17:     for (int i=0;i<m;i++) poss[i]=i+1;
  18: }
  19: void sorth(int l,int r)
  20: {
  21:     int i=l,j=r,mid=hen[(l+r)/2],tmp;
  22:     while (i<=j){
  23:         while (hen[i]>mid) i++;
  24:         while (hen[j]<mid) j--;
  25:         if (i<=j){
  26:             tmp=hen[i];hen[i]=hen[j];hen[j]=tmp;
  27:             tmp=posh[i];posh[i++]=posh[j];posh[j--]=tmp;
  28:         }
  29:     }
  30:     if (i<r) sorth(i,r);
  31:     if (l<j) sorth(l,j);
  32: }
  33: void sorts(int l,int r)
  34: {
  35:     int i=l,j=r,mid=shu[(l+r)/2],tmp;
  36:     while (i<=j){
  37:         while (shu[i]>mid) i++;
  38:         while (shu[j]<mid) j--;
  39:         if (i<=j){
  40:             tmp=shu[i];shu[i]=shu[j];shu[j]=tmp;
  41:             tmp=poss[i];poss[i++]=poss[j];poss[j--]=tmp;
  42:         }
  43:     }
  44:     if (i<r) sorts(i,r);
  45:     if (l<j) sorts(l,j);
  46: }
  47: int main()
  48: {
  49:     get_prepare();
  50:     sorth(0,n-1);
  51:     sorts(0,m-1);
  52:     for (int i=0;i<k;i++) opth[posh[i]]=1;
  53:     for (int i=0;i<n;i++) if (opth[i]) cout << i << ' ';
  54:     cout << '\n';
  55:     for (int i=0;i<l;i++) opts[poss[i]]=1;
  56:     for (int i=0;i<m;i++) if (opts[i]) cout << i << ' ';
  57:     cout << '\n';
  58:     return 0;
  59: }
还不会用一个过程对两个不同的数组进行排序,所以……将就看吧
posted @ 2012-07-05 14:03  NoRush  阅读(802)  评论(0编辑  收藏  举报