1. 程式人生 > >第八屆河南省程式設計大賽

第八屆河南省程式設計大賽

http://acm.nyist.net/JudgeOnline/problem.php?pid=1236

 
#include<iostream>//c++
#include<cmath>//數學公式
#include<cstdlib>//malloc
#include<cstring>
#include<string>
#include<cstdio>//輸入輸出
#include<algorithm>//快排
#include<queue>//佇列
#include<functional>//優先佇列
#include<stack>//棧
#include<vector>//容器
#include<map>//地圖  if continue
typedef long long ll;
const  int N=30+2000;
const  int inf=0x7fffffff;
using namespace std;
int n,ans;
char s[100];
string c;
map<string,int>q;
int main()
{
    int t,m,i,j;
    q["Na"]=23;
    q["N"]=14;
    q["C"]=12;
    q["O"]=16;
    q["Cl"]=35;
    q["S"]=32;
    q["H"]=2;
    q["Al"]=27;
    q["Ca"]=40;
    q["Zn"]=65;
    int a,b;
    cin>>t;
    while(t--)
    {
        ans=0;int sum=0,tmp;
        cin>>s;
        n=strlen(s);
        for(i=0;i<n;i++)  if(s[i]=='=')   break;
        for(i++;i<n;i++)  if(s[i]<='9'&&s[i]>='0')  sum=sum*10+s[i]-'0';else break;
        for(;i<n;i++)
        {
            if(s[i]=='+')   break;
            if(s[i]<='Z'&&s[i]>='A')
            {
                c=s[i];
                if(s[i+1]<='z'&&s[i+1]>='a'&&i+1<strlen(s))
                {
                    c+=s[i+1];
                    a=q[c];

                    b=0;
                    for(i+=2;i<n;i++)
                    if(s[i]<='9'&&s[i]>='0')  b=b*10+s[i]-'0';else {i--;break;}
                    if(b==0)  ans+=a,tmp+=a;
                    else   ans+=a*b,tmp+=a*b;
                }
                else
                {
                    a=q[c];
                    b=0;
                    for(i+=1;i<n;i++)
                    if(s[i]<='9'&&s[i]>='0')  b=b*10+s[i]-'0';else {i--;break;}
                    if(b==0)  ans+=a,tmp+=a;
                    else   ans+=a*b,tmp+=a*b;
                }
            }
            else  if(s[i]=='(') tmp=0;
            else  if(s[i]==')')
            {
                b=0;
                for(i+=1;i<n;i++)
                if(s[i]<='9'&&s[i]>='0')  b=b*10+s[i]-'0';else {i--;break;}
                ans+=tmp*(b-1);
            }
        }
        if(sum==0)  printf("%04d\n",ans);
        else printf("%04d\n",ans*sum);
    }
	return 0;
}
        
http://acm.nyist.net/JudgeOnline/problem.php?pid=1237
 
#include<iostream>//c++
#include<cmath>//數學公式
#include<cstdlib>//malloc
#include<cstring>
#include<string>
#include<cstdio>//輸入輸出
#include<algorithm>//快排
#include<queue>//佇列
#include<functional>//優先佇列
#include<stack>//棧
#include<vector>//容器
#include<map>//地圖  if continue
typedef long long ll;
const  int N=30+1e3;
const  int inf=0x7fffffff;
using namespace std;
int m,n;
char mp[N][N];
int dfs(int i,int j)
{
    int tmp=0;
    if(i-1>=0&&i-1<m&&j-1>=0&&j-1<n)      if(mp[i-1][j-1]=='1')      mp[i-1][j-1]='0',tmp+=1+dfs(i-1,j-1);
    if(i-1>=0&&i-1<m&&j>=0&&j<n)          if(mp[i-1][j]=='1')        mp[i-1][j]='0',tmp+=1+dfs(i-1,j);
    if(i-1>=0&&i-1<m&&j+1>=0&&j+1<n)      if(mp[i-1][j+1]=='1')      mp[i-1][j+1]='0',tmp+=1+dfs(i-1,j+1);
    if(i>=0&&i-1<m&&j-1>=0&&j-1<n)        if(mp[i][j-1]=='1')        mp[i][j-1]='0',tmp+=1+dfs(i,j-1);
    if(i>=0&&i-1<m&&j+1>=0&&j+1<n)        if(mp[i][j+1]=='1')        mp[i][j+1]='0',tmp+=1+dfs(i,j+1);
    if(i+1>=0&&i+1<m&&j-1>=0&&j-1<n)      if(mp[i+1][j-1]=='1')      mp[i+1][j-1]='0',tmp+=1+dfs(i+1,j-1);
    if(i+1>=0&&i+1<m&&j>=0&&j<n)          if(mp[i+1][j]=='1')        mp[i+1][j]='0',tmp+=1+dfs(i+1,j);
    if(i+1>=0&&i+1<m&&j+1>=0&&j+1<n)      if(mp[i+1][j+1]=='1')      mp[i+1][j+1]='0',tmp+=1+dfs(i+1,j+1);

    return tmp;
}
int main()
{
	int i,j,k,t,ans=0,sum=0;
	char c;
    scanf("%d%d%d%*c",&m,&n,&t);
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            scanf("%c",&c);
            if(c==' ') j--;
            else mp[i][j]=c;
        }
        getchar();
    }
    for(i=0;i<m;i++)
    for(j=0;j<n;j++)
        if(mp[i][j]=='1') sum++,ans=max(ans,dfs(i,j));
    cout<<sum<<" "<<ans*t<<endl;
	return 0;
}
        

http://acm.nyist.net/JudgeOnline/problem.php?pid=1238

 
#include <algorithm>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
using namespace std;
vector<int>map[505];
bool vis[505];
struct node
{
	int pos;
	int cost;
	friend bool operator<(node x,node y)
	{
		return x.cost>y.cost;
	}
};
int N;
int bfs()
{
	priority_queue<node>s;
	while(!s.empty()) s.pop();
	node temp,temp1;
	temp.pos=1;temp.cost=0;
	s.push(temp);
	while(!s.empty())
	{
		temp=temp1=s.top();s.pop();
		if(temp.pos==N) return temp.cost;
		if(vis[temp.pos]) continue;
		vis[temp.pos]=true;
		for(int i=0;i<map[temp.pos].size();i++)
		{
			temp.pos=map[temp.pos][i];
			temp.cost++;
			s.push(temp);
			temp=temp1;
		}
	}
	return -1;
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		int M,t;
		memset(map,0,sizeof(map));
		memset(vis,false,sizeof(vis));
		scanf("%d %d",&M,&N);
		getchar();
		int temp[505];
		for(int i=0;i<M;i++)
		{
			int sum=0;
			t=0;
			while(true)
			{
				char ch;
				scanf("%c",&ch);
				if(ch==' ')
				{
					if(sum)
					temp[t++]=sum;
					sum=0;
					
				}
				else if(ch=='\n'||ch=='\r')
				{
					if(sum)
					temp[t++]=sum;
					break;
				}
				else
				sum=sum*10+ch-'0';
			}
			for(int j=0;j<t;j++)
			{
		//	printf("%d ",temp[j]);
			for(int k=j+1;k<t;k++)
			{
		//		printf("%d ",temp[k]);
				map[temp[j]].push_back(temp[k]);
			}
		//	printf("\n");
			}
		}
		if(N==1)
		{
			printf("0\n");
			continue;
		}
		int res=bfs();
		if(res==-1)
		printf("NO\n");
		else
		printf("%d\n",res-1);
	}
}        

http://acm.nyist.net/JudgeOnline/problem.php?pid=1239

http://acm.nyist.net/JudgeOnline/problem.php?pid=1240

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,pre[10010],cnt;
struct node
{
	int x,y;
	int val;
}p[10010];
void init()
{
	for(int i=0;i<10010;i++)
	pre[i]=i;
}
int cmp(node s1,node s2)
{
	return s1.val<s2.val;
 } 
int find(int x)
{
	return x==pre[x]?x:find(pre[x]);
}
int join(int x,int y)
{
	int fx=find(x);
	int fy=find(y);
	if(fx!=fy)
	{
		pre[fx]=fy;
		return 1;
	}
	return 0;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		init();
		cnt=0;
		scanf("%d",&n);
		for(int i=0;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
			{
				scanf("%d",&p[cnt].val);
				p[cnt].x=i,p[cnt++].y=j;
			}
		}
		sort(p,p+cnt,cmp);
		int sum=0;
		for(int i=0;i<cnt;i++)
		{
			if(join(p[i].x,p[i].y))
			sum+=p[i].val;
		}
		printf("%d\n",sum);
	}
	return 0;
}


http://acm.nyist.net/JudgeOnline/problem.php?pid=1241

 
#include<iostream>//c++
#include<cmath>//數學公式
#include<cstdlib>//malloc
#include<cstring>
#include<string>
#include<cstdio>//輸入輸出
#include<algorithm>//快排
#include<queue>//佇列
#include<functional>//優先佇列
#include<stack>//棧
#include<vector>//容器
#include<map>//地圖  if continue
typedef long long ll;
const  int N=30+1e3;
const  int inf=0x7fffffff;
using namespace std;
int m,n;
struct point{  int x ,y ;  };

point p[101] ;
int main()
{
	int i , j , coutx ,couty;
	int px ,py ;
	cin>>n >>m ;

	for(i = 0 ; i < n ; i++) cin>>p[i].x>>p[i].y ;
    for(i=0;i<m;i++){
        cin>>px>>py ;
        couty =coutx = 0 ;
        for(j = 0 ; j < n ; j ++){
            if( (p[j].x<px && p[j].y<py))coutx ++ ;
            if(p[j].x>px&&p[j].y > py) coutx++;
            }
            cout<<coutx-(n-coutx)<<endl;
    }


	return 0;
}
        

http://acm.nyist.net/JudgeOnline/problem.php?pid=1242

 
#include<iostream>//c++
#include<cmath>//數學公式
#include<cstdlib>//malloc
#include<cstring>
#include<string>
#include<cstdio>//輸入輸出
#include<algorithm>//快排
#include<queue>//佇列
#include<functional>//優先佇列
#include<stack>//棧
#include<vector>//容器
#include<map>//地圖  if continue
typedef long long ll;
const  int N=30+2000;
const  int inf=0x7fffffff;
using namespace std;
int f[N];
int main()
{
    int t,n,m,i,j;
    cin>>t;
    while(t--)
    {
        int sum;
        double ans=0.0;
        cin>>n>>m;
        for(i=0;i<n;i++) cin>>f[i];
        for(i=0;i<n;i++)
        {
            sum=0;
            for(j=i;j<n;j++)
            {
                sum+=f[j];
                if((j-i)>=m-1)
                    if((ans-1.0*sum/(j-i+1))<1e-6)  ans=1.0*sum/(j-i+1);
            }
        }
        ans*=10000;
        printf("%d\n",(int)ans/10);
    }
	return 0;
}

http://acm.nyist.net/JudgeOnline/problem.php?pid=1243