ACM Steps_Chapter One_Section3

Moving Tables

/*解题思路:这道题最少花多少时间,实际上我们只要考虑哪一段重合度最高,
重合度最高的地方,也就是我们至少要移动的次数了。
因为有400间房间,1-2对应一段走廊,3-4对应一段走廊,
如此我们可以把走廊分成200段,标记为a[1]-a[200],
之后我们根据输进的房间序号,就可以算出要用到哪几段的走廊,
之后给对应的a[n]值加1就好,最后求出a[n]最大值就是移动的次数了。
*/
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
	int T;
	int pair;
	int a[201];
	int b,c,n1,n2;
	cin>>T;
	while(T--)
	{
		cin>>pair;
		memset(a,0,sizeof(a));
		while(pair--)
		{
			cin>>b>>c;
			if(b>c)
				swap(b,c);
			if(b%2==1)
				n1=(b-1)/2+1;
			else
				n1=b/2;
			if(c%2==1)
				n2=(c-1)/2+1;
			else
				n2=c/2;
			if(n1!=n2)
			{
				for(int i=n1;i<=n2;i++)
					a[i]++;
			}
			else
				a[n1]++;
		}
		int max=-1;
		for(int i=1;i<=200;i++)
		{
			if(max<a[i])
				max=a[i];
		}
		cout<<10*max<<endl;
	}
	system("pause");
	return 0;
}
				

Tian Ji -- The Horse Racing

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(const int &a,const int &b)
{    
    return a>b;
}
int main()
{  
    int win,n=0,i,l,m,t,k; 
    int a[1010],b[1010];  
    while(cin>>n&&n!=0) 
    {      
        for(i=0; i<n; i++)    
            cin>>a[i]; 
        for(i=0; i<n; i++)  
            cin>>b[i];        
        sort(a,a+n,cmp); 
        sort(b,b+n,cmp);  
        win=0;    
        l=0;//田忌最快的马    
        m=0;//齐王最快的马      
        t=n-1;//田忌最慢的马        
        k=n-1;//齐王最慢的马   
        for(i=0;i<n;i++)   
        {        
            if(a[l]<b[m])//田忌最快与齐王最快比,齐王快,用田忌最慢与齐王最快PK,败       
            {             
                t--;      
                m++;        
                win--;      
            }         
            else if(a[l]>b[m])//田忌最快比齐王最快快,直接PK,胜        
            {          
                l++;     
                m++;     
                win++;     
            }       
            else // a[l]==b[m],田忌最快与齐王最快相等  
            {           
                if(a[t]>b[k])   //田忌最慢比齐王最慢要快,直接PK掉,胜           
                {     
                    t--;      
                    k--;         
                    win++;       
                }            
                else if(a[t]<b[k])//田忌最慢比齐王最慢还慢,用田忌最慢PK齐王最快,败      
                {       
                    t--;      
                    m++;        
                    win--;     
                }              
            	else
				{//田忌与齐王最快与最慢都相等,则用田忌最慢PK齐王最快                 {            
                    if(a[t]<b[m])//田忌最慢的比齐王最快的慢,败      
                    {                
                        t--;         
                        m++; 
						win--;               
                    }//否则,平局        
                }      
            }     
        }    
        cout<<win*200<<endl;   
    }
    return 0;
}

排名

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 1005
int que[15];

struct node
{
    char name[25];
    int num;
    int sum;
} stu[N];

bool cmp(const node &a,const node &b)
{
    if(a.sum == b.sum)
        return strcmp(a.name,b.name) < 0 ? 1 : 0;
    else
        return a.sum > b.sum;
}

int main()
{
    int stu_num,text_num,line,a,cnt;

    while(scanf("%d",&stu_num)!=EOF && stu_num)
    {
        cnt = 0;
        int i;
        scanf("%d%d",&text_num,&line);
        for(i = 1; i<=text_num; i++)
            scanf("%d",&que[i]);
        for(i = 1; i<=stu_num; i++)
        {
            stu[i].sum = 0;
            scanf("%s%d",stu[i].name,&stu[i].num);
            while(stu[i].num--)
            {
                scanf("%d",&a);
                stu[i].sum+=que[a];
            }
            if(stu[i].sum>=line)
                cnt++;
        }
        sort(stu+1,stu+1+stu_num,cmp);
        cout << cnt << endl;
        for(i = 1; i<=stu_num; i++)
        {
            if(stu[i].sum >=line)
                printf("%s %d\n",stu[i].name,stu[i].sum);
            else
                break;
        }
    }
    return 0;
}

开门人和关门人

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
struct person
{
	char id[16];
	int stime;
	int ftime;
}a[100];
bool cmp1(const person &a,const person &b)
{
	return a.stime<b.stime;
}
bool cmp2(const person &a,const person &b)
{
	return a.ftime>b.ftime;
}

int main()
{
	int T;
	int n;
	int sh,sm,ss,fh,fm,fs;
	cin>>T;
	while(T--)
	{
		cin>>n;
		getchar();
		for(int i=0;i<n;i++)
		{
			scanf("%s %d:%d:%d %d:%d:%d",a[i].id,&sh,&sm,&ss,&fh,&fm,&fs);
			a[i].stime=sh*3600+sm*60+ss;
			a[i].ftime=fh*3600+fm*60+fs;
			getchar();
		}
		sort(a,a+n,cmp1);
		cout<<a[0].id<<" ";
		sort(a,a+n,cmp2);
		cout<<a[0].id<<endl;
	}
	system("pause");
	return 0;
}

第二小整数

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{
	return x<y;
}
int main()
{
	int T;
	cin>>T;
	int a[11];
	while(T--)
	{
		int n;
		cin>>n;
		for(int i=0;i<n;i++)
			cin>>a[i];
		sort(a,a+n,cmp);
		cout<<a[1]<<endl;
	}
	system("pause");
	return 0;
}

悼念512汶川大地震遇难同胞——老人是真饿了

#include<iostream>
#include<algorithm>
using namespace std;
struct intn
{
	int p,h;
}d[1100];
bool cmp(intn a,intn b)
{
	if(a.p==b.p)
		return a.h>b.h;
	return a.p<b.p;
}
int main()
{
	int c,n,m,i,money;
	double sum;
	while(cin>>c)
	{
		while(c--)
		{
		  cin>>n>>m;
		  for(i=0;i<m;i++)
		  cin>>d[i].p>>d[i].h;
		  sort(d,d+m,cmp);
		  sum=0;
		  money=0;
		  for(i=0;i<m;i++)
		  {
			 money+=d[i].p*d[i].h;
			 sum+=d[i].h;
			 if(money>n)
			 {
				sum-=(money-n)*(double)1/d[i].p;
				break;
			 }
		  }
		  printf("%.2lf\n",sum);
		}
		
	}
	return 0;
}

Fighting for HDU

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int n;
	int a[100],b[100];
	while(cin>>n,n!=0)
	{
		int na=0;
		int nb=0;
		for(int i=0;i<n;i++)
			cin>>a[i];
		for(int i=0;i<n;i++)
			cin>>b[i];
		sort(a,a+n);
		sort(b,b+n);
		for(int i=0;i<n;i++)
		{
			if(a[i]>b[i])
			{
				na+=2;
				nb+=0;
			}
			else if(a[i]==b[i])
			{
				na+=1;
				nb+=1;
			}
			else
			{
				na+=0;
				nb+=2;
			}
		}
		cout<<na<<" vs "<<nb<<endl;
	}
	system("pause");
	return 0;
}

Who's in the Middle

#include<iostream>
#include<algorithm>
int cmp(int x,int y)
{
	return x<y;
}
using namespace std;
int main()
{
	int n;
	int a[10001];
	while(cin>>n)
	{
		for(int i=0;i<n;i++)
			cin>>a[i];
		sort(a,a+n,cmp);
		cout<<a[n/2]<<endl;
	}
	system("pause");
	return 0;
}


posted @ 2013-04-20 21:10  VeryBigMan  阅读(109)  评论(0编辑  收藏  举报