zoukankan      html  css  js  c++  java
  • sort题目

    uva,146

    全排列问题:permutation 

    具体详解:参考Devymex

    UVa Problem 146 - ID Codes

     
    Problem:

    Please find the problem here.

    Solution:

    This is simply the next permutation problem. Thanks to the hint on Competitive Programming, there is a simple call in C++ library named next_permutation that can solve the problem, and therefore I used it.

    Suppose we are not given the next_permutation code, how would I solve it? First, note that in order to find the least significant digit to increase, but you can't just increase a digit because this is a permutation, one also need to change another digit to change as well. Surely we don't want to alter the more significant digits, so we can only pick the less significant ones.

    Formally, one scan from right to left, trying to find a digit such that there is a larger digit to its right. If such a digit does not exist, we can conclude there is no successor. Otherwise, change that digit to the least larger one found on the right, and sort the rest of the digits, that gives the answer.

    For example, the successor of 1, 2, 3, 3 is 1, 3, 2, 3, this is done by noting 2 < 3, so we switch 2 to 3, and just sort [2, 3], which give [2, 3]

    As a more exotic scenario, let's look at 1, 4, 3, 2, its successor is 2, 1, 3, 4, this is done by noting 1 < 2, so we switch 1 to 2, and just sort [1, 4, 3], which gives [1, 3, 4].
     
    贴一下自己写的代码:
    总是wrong answer不知道怎么的:到时候再来填坑。
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <vector>
     5 
     6 using namespace std;
     7 
     8 int main()
     9 {
    10     vector<char> s;
    11     while(!cin.eof())
    12     {
    13         char c=cin.get();
    14         if(c!=-1)
    15         {
    16             if(c!='
    ' && c!='
    ')
    17             {
    18                 if(c=='#')
    19                     break;
    20                 s.push_back(c);
    21             }
    22             else
    23             {
    24                 vector<char>::iterator it=s.end(),at;
    25                 for(--it;it!=s.begin();--it)
    26                 {
    27                     if(*(it-1)<*it)
    28                         break;
    29                 }
    30                 if(it==s.begin())
    31                     printf("No Successor
    ");
    32                 else
    33                 {
    34                     it--;
    35                     for(at=it+1;at!=s.end();at++)
    36                         if(*at<*it)
    37                             break;
    38                     iter_swap(it,--at);
    39                     reverse(it+1,s.end());
    40                     for(vector<char>::iterator i=s.begin();i!=s.end();i++)
    41                         cout<<*i;
    42                     cout<<endl;
    43                 }
    44                 s.clear();
    45                 
    46             }
    47         }
    48     }
    49     return 0;
    50 }
    View Code

    accept代码:Andrew

     1 #include <iostream>
     2 #include <vector>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     vector<char> line;
    10     while (!cin.eof())
    11     {   
    12         char c = cin.get();
    13         if (c != -1)
    14         {
    15             if (c != '
    ' && c != '
    ')
    16             {
    17                 if (c == '#')
    18                 {
    19                     break;
    20                 }
    21                 line.push_back(c);
    22             }
    23             else
    24             {
    25                 if (next_permutation(line.begin(), line.end()))
    26                 {
    27                     for (vector<char>::iterator i = line.begin(); i != line.end(); i++)
    28                     {
    29                         cout << *i;
    30                     }
    31                     cout << endl;
    32                 }
    33                 else
    34                 {
    35                     cout << "No Successor" << endl;
    36                 }
    37                 line.clear();
    38             }
    39         }
    40     }
    41 
    42     return 0;
    43 }
    View Code

    uva,299

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 
     5 using namespace std;
     6 const int maxn=55;
     7 
     8 int main()
     9 {
    10     int N;
    11     int L;
    12     int a[maxn];
    13     scanf("%d",&N);
    14     while(N--)
    15     {
    16         scanf("%d",&L);
    17         for(int i=0;i<L;i++)
    18             scanf("%d",&a[i]);
    19         int count=0;
    20         for(int i=0;i<L;i++)
    21             for(int j=1;j<L-i;j++)
    22             {
    23                 if(a[j]<a[j-1])
    24                 {
    25                     swap(a[j],a[j-1]);
    26                     count++;
    27                 }
    28             }
    29         printf("Optimal train swapping takes %d swaps.
    ",count);
    30     }
    31     return 0;
    32 }
    View Code

    ======

    用结构体来模拟map很舒服。

    uva,10008

     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstdio>
     4 #include <cstring>
     5 
     6 using namespace std;
     7 
     8 struct P
     9 {
    10     int times;
    11     char ch;
    12 };
    13 
    14 int comp(const P x,const P y)
    15 {
    16     if(x.times==y.times)
    17         return x.ch<y.ch;
    18     return x.times>y.times;
    19 }
    20 int main()
    21 {
    22     int n;
    23     char ch,s[10000];
    24     P a[27];
    25     for(int i=0;i<26;i++)
    26     {
    27         a[i].times=0;
    28         a[i].ch=i+65;
    29     }
    30     scanf("%d",&n);
    31     while(n--)
    32     {
    33         cin.getline(s,10000);
    34         for(int i=0;i<strlen(s);i++)
    35         {
    36             if(isalpha(s[i]))
    37             {
    38                 if(islower(s[i])) s[i]-=32;
    39                 a[(int)s[i]-65].times++;
    40             }
    41         }
    42     }
    43     sort(a,a+26,comp);
    44     for(int i=0;i<26,a[i].times>0;i++)
    45         cout<<a[i].ch<<" "<<a[i].times<<endl;
    46     
    47 }
    View Code

    uva,10041

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <math.h>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 int main()
     8 {
     9     int t,r;
    10     int s[500];
    11     scanf("%d",&t);
    12     while(t--)
    13     {
    14         scanf("%d",&r);
    15         for(int i=0;i<r;i++)
    16             scanf("%d",&s[i]);
    17         sort(s,s+r);
    18         int count=s[r/2];
    19         int total=0;
    20         int t=r/2;
    21         for(int i=0;i<r;i++)
    22         {
    23             if(i==t)
    24                 continue;
    25             total+=abs(count-s[i]);
    26         }
    27         printf("%d
    ",total);
    28     }
    29     return 0;
    30 }
    View Code

    uva,10050

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 const int maxn=3655;
     6 
     7 int main()
     8 {
     9     int T,P,N;
    10     int day[maxn];
    11     int h[105];
    12     int k;
    13     scanf("%d",&T);
    14     while(T--)
    15     {
    16         scanf("%d",&N);
    17         scanf("%d",&P);
    18         for(int i=0;i<P;i++)
    19         {
    20             scanf("%d",&h[i]);
    21         }
    22         memset(day,0,sizeof(day));
    23         for(int i=0;i<P;i++)
    24         {
    25             k=1;
    26             while(k*h[i]<=N)
    27             {
    28                 day[k*h[i]]=1;
    29                 k++;
    30             }
    31         }
    32         int count=0;
    33             for(int i=1;i<=N;i++)
    34             {
    35                 if(i%7==6 || i%7==0)
    36                     continue;
    37                 if(day[i]==1)
    38                     count++;
    39             }
    40         printf("%d
    ",count);
    41     }
    42     return 0;
    43 }
    View Code

    uva,10107

     1 #include <iostream>
     2 #include <algorithm>
     3 using namespace std;
     4 
     5 int main()
     6 {
     7     long long a[10005],l,X;
     8     int i=0;
     9     while(cin>>X)
    10     {
    11         a[i]=X;
    12         sort(a,a+i+1);
    13         if(i%2==0)
    14             cout<<a[i/2]<<endl;
    15         else
    16         {
    17             l=a[(i-1)/2]+a[(i+1)/2];
    18             cout<<l/2<<endl;
    19         }
    20         i++;
    21     }
    22     return 0;
    23 }
    View Code

    uva,10327

     1 #include <iostream>
     2 #include <cstdio>
     3 
     4 using namespace std;
     5 const int maxn=1005;
     6 
     7 void flip(int *x,int *y)
     8 {
     9     int tmp;
    10     tmp=*x;
    11     *x=*y;
    12     *y=tmp;
    13 }
    14 
    15 int main()
    16 {
    17     int N;
    18     int a[maxn];
    19     while((scanf("%d",&N))!=EOF)
    20     {
    21         for(int i=0;i<N;i++)
    22             scanf("%d",&a[i]);
    23         int count=0;
    24         for(int i=0;i<N;i++)
    25         {
    26             for(int j=1;j<N-i;j++)
    27             {
    28                 if(a[j]<a[j-1])
    29                 {
    30                     flip(&a[j],&a[j-1]);
    31                     count++;
    32                 }
    33             }
    34         }
    35         printf("Minimum exchange operations : %d
    ",count);
    36     }    
    37     return 0;
    38 }
    View Code
    活在现实,做在梦里。
  • 相关阅读:
    opencv-0-项目启程
    [SketchUp]-绘制自己的家
    C51_PID 水温控制系统
    latex-列表环境
    nCOV 数据简要分析 (0326)
    引入OpenCV导致私有内存巨大
    【带着canvas去流浪(15)】threejs fundamentals翻译系列1-scene graph
    【一统江湖的大前端(9)】TensorFlow.js 开箱即用的深度学习工具
    【一统江湖的大前端(8)】matter.js 经典物理
    2019年12月前端面经及总结(西安,杭州)
  • 原文地址:https://www.cnblogs.com/do-it-best/p/5357507.html
Copyright © 2011-2022 走看看