UVaOJ 112道题目-排序

1、110401/10041 Vito’s Family (Vito 家族)

距离最小的点必定是中位数,必定出现在输入的点之间

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<ctype.h>
using namespace std;
int s[505];
int dis[30005];
const int INF=1000000000;
int main()
{
    int T,r;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&r);
        int j,i,sum=0;
        memset(dis,0,sizeof(dis));
        for(i=0;i<r;i++)
        {
            scanf("%d",&s[i]);
        }
        for(i=0;i<r;i++)
        {
            int x=s[i];
            for(j=0;j<r;j++)
            {
                dis[x]+=abs(s[j]-x);
            }
        }
        int d=INF;
        for(i=0;i<r;i++)
            if(dis[s[i]]<d)d=dis[s[i]];
        printf("%d
",d);
    }
    return 0;
}
View Code

 2、110402/120  Stacks of Flapjacks (煎饼堆)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<ctype.h>
using namespace std;
int p[35];
int s[35];
char cmd[100];
int k=0;
void read()
{
    int len=strlen(cmd),i,tot=0;
    k=0;
    for(i=0;i<=len;i++)
    {
        if(i<len&&cmd[i]!=' ')
        {
            tot=tot*10+cmd[i]-'0';
        }
        else
        {
            if(tot==0)continue;
            p[k++]=tot;
            tot=0;
        }
    }
}
int main()
{
    while(gets(cmd)!=NULL)
    {
        read();
        int i,j,t;
        for(i=0;i<k;i++)
        {
            if(i)printf(" ");
            printf("%d",p[i]);
            s[i]=p[i];
        }
        printf("
");
        sort(s,s+k);
        for(i=k-1;i>0;i--)
        {
            if(p[i]==s[i])continue;
            for(j=1;j<i&&p[0]!=s[i];j++)
            {
                if(p[j]==s[i])
                {
                    printf("%d ",k-j);
                    for(t=0;t<=j/2;t++)
                        swap(p[t],p[j-t]);
                }
            }
            printf("%d ",k-i);
            for(t=0;t<=i/2;t++)
                swap(p[t],p[i-t]);
        }
        printf("0
");
    }
    return 0;
}
/*
8 4 6 7 5 2
1 5 6 2 3 4
*/
View Code

 3、110403/10037 Bridge (过桥)

贪心算法

1》最快和次快过桥,最快回来,最慢和次慢过桥,次快回来

  使用时间2*p[0]+p[i]+p[i-1];

2》最快和最慢过桥,最快回来,最快和次慢过桥,最快回来

  使用时间2*p[1]+p[0]+p[i];

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
int p[1005];
int rnd[2005][5];
int tot[2005];
void pass(int cnt,int t,int a,int b)
{
    tot[cnt]=t;
    rnd[cnt][0]=p[a];
    if(t>=2)rnd[cnt][1]=p[b];
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,i;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&p[i]);
        }
        sort(p,p+n);
        int sum=0;
        int cnt=0;
        for(i=n-1;i>2;i-=2)
        {
            if(2*p[1]+p[0]+p[i]>2*p[0]+p[i]+p[i-1])
            {
                sum+=2*p[0]+p[i]+p[i-1];
                pass(cnt++,2,0,i-1);
                pass(cnt++,1,0,-1);
                pass(cnt++,2,0,i);
                pass(cnt++,1,0,-1);
            }
            else
            {
                sum+=2*p[1]+p[0]+p[i];
                pass(cnt++,2,0,1);
                pass(cnt++,1,0,-1);
                pass(cnt++,2,i-1,i);
                pass(cnt++,1,1,-1);
            }
        }
        if(i==2)
        {
            sum+=p[0]+p[1]+p[2];
            pass(cnt++,2,0,1);
            pass(cnt++,1,0,-1);
            pass(cnt++,2,0,2);
        }
        if(i==1)
        {
            sum+=p[1];
            pass(cnt++,2,0,1);
        }
        if(i==0)
        {
            sum+=p[0];
            pass(cnt++,1,0,-1);
        }
        printf("%d
",sum);
        for(i=0;i<cnt;i++)
        {
            for(int j=0;j<tot[i];j++)
            {
                if(j)printf(" ");
                printf("%d",rnd[i][j]);
            }
            printf("
");
        }
        if(T)printf("
");
    }
    return 0;
}
View Code

 4、110404/10191 Longest Nap (最长打盹时间)

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
struct NODE
{
    int s,e;
}ap[105];
bool cmp(NODE a,NODE b)
{
    return a.s<b.s;
}

int main()
{
    int d;
    int day=1;
    while(scanf("%d",&d)!=EOF)
    {
        int i;
        char app[260];
        int sh,eh,sm,em;
        for(i=0;i<d;i++)
        {
            scanf("%d:%d %d:%d",&sh,&sm,&eh,&em);
            gets(app);
            ap[i].s=sh*60+sm;
            ap[i].e=eh*60+em;
        }
        sort(ap,ap+d,cmp);
        int max=ap[0].s-10*60,tmp;
        int sta=10*60;
        for(i=1;i<d;i++)
        {
            tmp=ap[i].s-ap[i-1].e;
            if(max<tmp)
            {
                max=tmp;
                sta=ap[i-1].e;
            }
        }
        tmp=18*60-ap[d-1].e;
        if(max<tmp)
        {
            max=tmp;
            sta=ap[d-1].e;
        }
        printf("Day #%d: the longest nap starts at %d:%02d and will last for ",day++,sta/60,sta%60);
        if(max<60)printf("%d minutes.
",max);
        else printf("%d hours and %d minutes.
",max/60,max%60);
    }
    return 0;
}
/*
4
11:00 12:00 Lectures
12:00 13:00 Lunch, like always.
13:00 15:00 Boring lectures...
15:30 15:45 Reading
*/
View Code

 5、110406/10138 CDVII (CDVII 高速公路)

注意点:1、没有消费的车不输出

    2、采样的汽车位置可能exit的位置小于enter的位置 

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
#include<map>
#include<string>
using namespace std;
typedef long long lld;
const int inf=1003;
int fare[24];
int h[inf],nxt[inf];
struct PHOTO
{
    char p[100];
}photo[inf];
map<string,int> m;
struct NODE
{
    int h,m;
    int dis;
    char w;
}nod[inf];
bool cmp(PHOTO a,PHOTO b)
{
    return strcmp(a.p,b.p)>0;
}
char name [inf][23];
typedef long long lld;
int trans(char tmp[])
{
    int i,len=strlen(tmp),tot=0;
    for(i=0;i<len;i++)
        tot=tot*10+tmp[i]-'0';
    return tot;
}
double calc(int t)
{
    int i,dis,st;
    double tot=2;
    bool sta=true;
    for(i=h[t];i!=-1;i=nxt[i])
    {
        if(nod[i].w==0)
        {
            dis=nod[i].dis;
            st=nod[i].h;
            sta=false;
        }
        if(nod[i].w==1&&!sta)
        {
            dis=abs(nod[i].dis-dis);
            tot+=dis*fare[st]*1.0/100+1;
            sta=true;
        }
    }
    return tot;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int i,j;
        memset(h,-1,sizeof(h));
        memset(nxt,-1,sizeof(nxt));
        for(i=0;i<24;i++)scanf("%d",&fare[i]);
        int n=0;
        getchar();
        while(gets(photo[n].p)!=NULL)
        {
            if(photo[n].p[0]=='')break;
            n++;
        }
        m.clear();
        sort(photo,photo+n,cmp);
        map<string ,int>::iterator it;
        int tot=0;
        for(i=0;i<n;i++)
        {
            int len=strlen(photo[i].p),k=0;    
            char tmp[100]="";
            int cnt=0;
            int d;
            for(j=0;j<=len;j++)
            {
                if(j<len&&photo[i].p[j]!=' ')
                {
                    tmp[k++]=photo[i].p[j];
                }
                else
                {
                    cnt++;
                    tmp[k]='';
                    k=0;
                    if(cnt==1)
                    {
                        it=m.find(tmp);
                        if(it==m.end())
                        {
                            m.insert(pair<string,int>(tmp,i));
                            strcpy(name[i],tmp);
                            d=i;
                        }
                        else
                            d=it->second;
                    }
                    else if(cnt==2)
                    {
                        nod[tot].h=(tmp[6]-'0')*10+tmp[7]-'0';
                        nod[tot].m=(tmp[9]-'0')*10+tmp[10]-'0';
                    }
                    else if(cnt==3)
                    {
                        if(strcmp(tmp,"enter")==0)
                            nod[tot].w=0;
                        else nod[tot].w=1;
                    }
                    else
                    {
                        nod[tot].dis=trans(tmp);
                    }
                }
            }
            nxt[tot]=h[d];
            h[d]=tot;
            tot++;
        }
        for(it=m.begin();it!=m.end();it++)
        {
            int t=it->second;
            double x=calc(t);
            if(x>2)
                printf("%s $%.2f
",name[it->second],x);
        }
        if(T)printf("
");
    }
    return 0;
}
/*
9

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
ABCD123 01:01:06:01 enter 17
765DEF 01:01:07:00 exit 95
ABCD123 01:01:08:03 exit 95
765DEF 01:01:05:59 enter 17
765DEF 01:01:04:01 enter 17
*/
View Code

 6、110407/10152 ShellSort (龟壳排序)

使用手工模拟很快能 出结果,即从 后往前遍历的最长子序列

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
#include<map>
#include<string>
using namespace std;
char tur[205][1000];
char dest[205][1000];
map<string,int> m;
map<int,string> mt;
int arr[205];
int ind[205];
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n,i,j;
        scanf("%d",&n);
        getchar();
        m.clear();
        mt.clear();
        for(i=0;i<n;i++)
        {
            gets(tur[i]);
        }
        for(i=0;i<n;i++)
        {
            gets(dest[i]);
            m.insert(pair<string,int>(dest[i],i));
            mt.insert(pair<int,string>(i,dest[i]));
        }
        map<string,int>::iterator it;
        map<int,string>::iterator itt;
        for(i=0;i<n;i++)
        {
            it=m.find(tur[i]);
            arr[i]=it->second;
        }
        int tot=0,cur=n-1;
        for(i=n-1;i>=0;i--)
        {
            if(arr[i]==cur)
            {
                tot++;
                cur--;
            }
        }
        for(i=n-1-tot;i>=0;i--)
        {
            itt=mt.find(i);
            cout<<itt->second<<endl;            
        }
        printf("
");
    }
    return 0;
}
View Code

 也可以不用这么麻烦

#include <stdio.h>
#include<string.h>
int main(void)
{

    int n,i,ax,bx,num;
    scanf("%d",&n);
    //while(scanf("%d",&n)!=EOF)
    //{
        while(n-->0)
        {
            char a[300][100],b[300][100];
            scanf("%d",&num);
            getchar();
            for(i=0;i<num;i++)
                gets(a[i]);

            for(i=0;i<num;i++)
                gets(b[i]);
            ax=num-1;
            bx=num-1;
            for(ax=num-1,bx=num-1;ax>=0;ax--)
            {
                if(!strcmp(a[ax],b[bx]))
                    bx--;
            }
            while(bx>=0)
                printf("%s
",b[bx--]);
            printf("
");
        }
    //}
    return 0;
}
View Code

 7、110408/10194 Football (aka Soccer) (足球)

注意:当平局的时候,以你不区分大小写的字典序排序

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
#include<string>
#include<map>
using namespace std;
map<string,int> m;
char tname[105],res[1005];
char team[35][100];
struct NODE
{
    int no;
    int b,c,d,e,f,g,h,i;
}node[35];
void calc(int ind,int sco,int ops)
{
    node[ind].c++;
    node[ind].h+=sco;
    node[ind].i+=ops;
    if(sco==ops)
    {
        node[ind].e++;
        node[ind].b++;
    }
    else if(sco>ops)
    {
        node[ind].d++;
        node[ind].b+=3;
    }
    else
        node[ind].f++;
}
int getnum(char r[],int s,int e)
{
    int i,tot=0;
    for(i=s;i<e;i++)
        tot=tot*10+r[i]-'0';
    return tot;
}
void proc()
{
    int i,len=strlen(res),s,e,mid,cnt=0,k=0,left,right,sl,sr;
    char tmp[100];
    map<string,int>::iterator it;
    for(i=0;i<len;i++)
    {
        tmp[k]=res[i];
        if(res[i]=='#'&&cnt==0)
        {
            tmp[k]='';    
            it=m.find(tmp);
            left=it->second;
            s=i;
            cnt++;
        }
        else if(res[i]=='#'&&cnt==1)
        {
            e=i;
            k=-1;
        }
        else if(res[i]=='@')
        {
            mid=i;
        }
        k++;
    }
    tmp[k]='';
    it=m.find(tmp);
    right=it->second;
    sl=getnum(res,s+1,mid);
    sr=getnum(res,mid+1,e);
    calc(left,sl,sr);
    calc(right,sr,sl);
}
int _strcmp(char a[],char b[])
{
    int la=strlen(a),lb=strlen(b),i;
    for(i=0;i<la&&i<lb;i++)
    {
        char c=a[i];
        char d=b[i];
        if(c>='A'&&c<='Z')c=c-'A'+'a';
        if(d>='A'&&d<='Z')d=d-'A'+'a';
        if(c!=d)return c<d;
    }
    return la<lb;
}
bool cmp(NODE a,NODE b)
{
    if(a.b!=b.b)return a.b>b.b;
    if(a.d!=b.d)return a.d>b.d;
    if(a.g!=b.g)return a.g>b.g;
    if(a.h!=b.h)return a.h>b.h;
    if(a.c!=b.c)return a.c<b.c;
    return _strcmp(team[a.no],team[b.no]);
}
int main()
{
    int i,j,N,T,G;
    scanf("%d",&N);
    getchar();
    while(N--)
    {        
        gets(tname);
        scanf("%d",&T);
        getchar();
        m.clear();
        for(i=0;i<T;i++)
        {
            gets(team[i]);
            m.insert(pair<string,int>(team[i],i));
            node[i].no=i;
            node[i].b=0;
            node[i].c=0;
            node[i].d=0;
            node[i].e=0;
            node[i].f=0;
            node[i].g=0;
            node[i].h=0;
            node[i].i=0;
        }
        scanf("%d",&G);
        getchar();
        for(i=0;i<G;i++)
        {
            gets(res);
            proc();
        }
        for(i=0;i<T;i++)
        {
            node[i].g=node[i].h-node[i].i;
        }
        sort(node,node+T,cmp);
        printf("%s
",tname);
        for(i=0;i<T;i++)
        {
            printf("%d) %s %dp, %dg (%d-%d-%d), %dgd (%d-%d)
",
                i+1,team[node[i].no],node[i].b,node[i].c,node[i].d,node[i].e,node[i].f,node[i].g,node[i].h,node[i].i);
        }
        if(N)printf("
");
    }
    return 0;
}
/*
3
World Cup 1998 - Group A
4
Brazil
Norway
Morocco
Scotland
6
Brazil#14@1#Scotland
Norway#2@14#Morocco
Scotland#1@1#Norway
Brazil#3@0#Morocco
Morocco#3@0#Scotland
Brazil#1@2#Norway
Some strange tournament
5
Team A
Team B
aeam C
Team D
Team E
5
Team A#1@1#Team B
Team A#2@2#aeam C
Team A#0@0#Team D
Team E#1@1#aeam C
Team E#0@0#aeam C
*/
View Code

 8 110405/10026 Shoemaker’s Problem (鞋匠的烦恼)

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<ctype.h>
using namespace std;
struct CUS
{
    int no;
    double v;
}cus[1005];
const double ex=1.0e-8;
int dblcmp(double x)
{
    if(fabs(x)<ex)return 0;
    return x>0?1:-1;
}
bool cmp(CUS a,CUS b)
{
    return dblcmp(a.v-b.v)>0;
}
int main()
{
    int T,N;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&N);
        int i,t,f;
        for(i=0;i<N;i++)
        {
            scanf("%d%d",&t,&f);
            cus[i].v=f*1.0/t;
            cus[i].no=i+1;
        }
        sort(cus,cus+N,cmp);
        for(i=0;i<N;i++)
        {
            if(i)printf(" ");
            printf("%d",cus[i].no);
        }
        printf("
");
        if(T)printf("
");
    }
    return 0;
}
/*
3

4
1 4
1 1000
1 2
1 5

4
2 10
1 100
1 1000
1 10000
 
4
1 10
2 100
1 1000
1 10000

4 
1 100
1 200
2 10
1 10000

4
1 1
2 100
2 10
3 1
*/
View Code
原文地址:https://www.cnblogs.com/varcom/p/4116497.html