2014年7月23日比賽題目

UVA 12720 Algorithm of Phil

Phil is learning a new algorithm which wasn't taught in his algorithms classes. However, he is not sure whether he implemented it the right way, so he would really appreciate if you could implement it so that he can compare the outputs.

The algorithm starts with a binary string A and an empty string S. The algorithm consists of multiple steps. In each step, A and S are modied as follows:

·If the number of bits in A is odd, then the middle bit of A is added to the end of S and removed from A.

·If the number of bits in A is even, then both middle bits of A are compared. The bigger one (anyone in case of a tie) is added to the end of S and removed from A.

·If after some step the string A gets empty, the algorithm terminates. The algorithm's return is the decimal representation of the number represented by S.

A bit a is bigger than a bit b if a is 1 and b is 0.

Input

The rst line contains T (T <= 500) | the number of test cases, after this line T test cases follows. Each test case consists of one line containing a binary string A (1 <=|A|<=10^5), representing the algorithm's input.

Output

For each case print a line containing ‘Case #X: Y’, where X is the case number, starting at 1, and Y is the algorithm's return for the given input, modulo 1000000007 (10^9+ 7).

Sample Input

3

00000

0101101

100

Sample Output

Case #1: 0

Case #2: 106

Case #3: 2

【題目大意】

給出一個01串,每次操作:如果該串長度爲奇數,取出中間的放到ANS串末尾,如果是偶數,則取出中間兩個最大的那個放到ANS末尾,直到給定串爲空

【題解】

水題,一開始是用string類型定義給定串的,刪除字符就拷貝兩次,結果是喜聞樂見的TLE,後來發現根本不!用!刪!除!操!作!

【代碼】

RunID

User

Problem

Result

Memory

Time

Language

Length

Submit Time

2472432

zhyfzy

A

Accepted

0 KB

108 ms

C++ 4.8.2

914 B

2014-07-23 12:53:34


 

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#define MOD 1000000007
#define N 200000
using namespace std;
int i,j,k,n,T,K,len,m,ax;
long long aa;
char temp[N],s[N],ans[N];
int main()
{
	scanf("%d",&T);
	for (K=1;K<=T;K++)
	{
		getchar();
		scanf("%s",s);
		len=strlen(s);
		ans[len]='\0';
		if (len%2)
		{
			m=(len+1)/2-1;
			ans[0]=s[m];ax=0;
			for (i=1;i<=m;i++)
			{
				if (s[m-i]>s[m+i])
				{
					ans[++ax]=s[m-i];
					ans[++ax]=s[m+i];
				} else
				{
					ans[++ax]=s[m+i];
					ans[++ax]=s[m-i];
				}
			}
		} else
		{
			m=len/2;ax=-1;
			for (i=1;i<=m;i++)
			{
				if (s[m-i]>s[m+i-1])
				{
					ans[++ax]=s[m-i];
					ans[++ax]=s[m+i-1];
				} else
				{
					ans[++ax]=s[m+i-1];
					ans[++ax]=s[m-i];
				}
			}
		}	
		aa=0;
		for (i=0;i<len;i++)
		{
			if (ans[i]=='1') aa=(aa*2+1)%MOD;
			else aa=(aa*2)%MOD;
		}
		printf("Case #%d: %lld\n",K,aa);
			
	}
}

UVA 12721 Cheap B-Subsequence

【題目大意】

PDF粘題太麻煩了,就不粘題了

 

這道題是求給定字母串的子串的最小值

從給定小寫字母串中找一個長度爲b的子串(各字符位置可以不連續),

每個字母給Q個數對,(Pi,Ci)

子串的值表示爲字符的的值的和,我們假定某字符在子串的位置爲X,則該字符的值爲X/Pi*CiXPi的倍數時加入結果中。

比如

[a]={(2,3),(4,10)}

[b]={(1,4),(7,50)}

[c]={(1,2),(4,20)}

字符串abaabcbc,長度b=4

如果子串爲”aabc” (abaabcbc)

第一個a位置爲1,不能整除a的任何Pi值,值爲0

第二個a位置爲2,值爲3

第三個b值爲33/1*4=12,值爲12

第四個c值爲44/4*20+4/1*2=28,值爲28

所以子串的值爲43

現在求字符串的子串的最小值爲多少

【題解】

很簡單的動態規劃,數據規模不大(每個數都小於100),所以三層循環可以勝任

c[i][j]爲字母i在子串j位置處的值,通過預處理可以得到數組c的值

f[i][j]爲將給定串S的第i位放在子串j位置處的最小值

F[i][j]=min{f[k][j-1]+c[alp][j]}alp爲給定串Si位字符,k<i

 

【代碼】

RunID

User

Problem

Result

Memory

Time

Language

Length

Submit Time

2475919

zhyfzy

B

Accepted

0 KB

39 ms

C++ 4.8.2

815 B

2014-07-23 16:27:32

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
int i,j,k,n,T,alp,K,c[200][200],p,cc,ans,f[200][200],b,m;
char s[200];
int main()
{
	scanf("%d",&T);
	for (K=1;K<=T;K++)
	{
		memset(c,0,sizeof(c));
		memset(f,0x2f,sizeof(f));
		
		getchar();
		s[0]=' ';
		scanf("%s %d",s+1,&b);
		n=strlen(s)-1;
		for (i=1;i<=26;i++)
		{
			scanf("%d",&m);
			for (j=1;j<=m;j++)
			{
				scanf("%d%d",&p,&cc);
				for (k=p;k<=b;k+=p)
				{
					c[i][k]+=k/p*cc;
				}
			}
		}
		
		for (i=0;i<=n;i++) f[i][0]=0;
		ans=1<<30;
		
		for (j=1;j<=b;j++)
		{
			for (i=j;i<=n;i++)
			{
				alp=s[i]-'a'+1;
				for (k=0;k<=i-1;k++)
				{
					f[i][j]=min(f[i][j],f[k][j-1]+c[alp][j]);
				}
			}
		}
		for (i=b;i<=n;i++)
		{
			ans=min(ans,f[i][b]);
		}
		printf("Case #%d: %d\n",K,ans);
	}
		
}


UVA 12723 Dudu, the Possum

【題目大意】

冰箱有N層,老鼠在頂端,每次最多向下跳K層,向下跳i層的概率爲Pi,每層有多個食物,老鼠只能吃一個,食物的卡路里Ci,j(第i層的第j個食物)與吃該食物的概率Xi,j給定,求老鼠跳到最底層後吃到的食物卡路里的數學期望。

【題解】

模擬,由於到哪一層與食物沒有關係,先求出老鼠到每一層的概率,然後用這個概率乘每層食物的期望即可。

每層食物的卡路里的期望就不必要說了吧,卡路里乘上概率再求和

每一層的概率,f[i]=sum{f[i-j]*p[i]}1<=j<=k

【代碼】

RunID

User

Problem

Result

Memory

Time

Language

Length

Submit Time

2477328

zhyfzy

D

Accepted

0 KB

73 ms

C++ 4.8.2

703 B

2014-07-23 19:10:43

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
int i,j,k,n,T,K,q;
double t1,t2,p[1000],qw[1000],f[1000],ans;
int main()
{
	scanf("%d",&T);
	for (K=1;K<=T;K++)
	{
		ans=0;
		scanf("%d%d",&n,&k);
		for (i=1;i<=k;i++)
		{
			scanf("%lf",&p[i]);
		}
		for (i=1;i<=n;i++)
		{
			scanf("%d",&q);
			qw[i]=0;
			for (j=1;j<=q;j++)
			{
				scanf("%lf%lf",&t1,&t2);
				qw[i]+=t1*t2;//qw記錄每一層食物的期望
			}
			//printf("qw[%d]= %lf\n",i,qw[i]);
		}
		f[1]=1;ans=qw[1];
		for (i=2;i<=n;i++)
		{
			f[i]=0;
			for (j=1;j<=k;j++)
			{
				if (i-j<=0) break;
				f[i]+=f[i-j]*p[j];
			}
			ans+=f[i]*qw[i];
			//printf("f[%d]= %lf\n",i,f[i]);
		}
		printf("Case #%d: %.6lf\n",K,ans);
	}
}


UVA 12724 Hnelpig Arnde

【題目大意】

N個單詞的字典

然後給幾個句子,句子中每個單詞出去首尾字母都是亂序的,你需要幫忙恢復順序

【題解】

很水的題目,一種方法是統計單詞中字母的個數,當首尾相同是比較各個字母出現次數,如果相同則是一個單詞

另一種方法時將單詞中的除去首尾的字母排序,只需判斷排序後的單詞是否相同即可

第二種更快並且好寫一些,模擬賽時用的第一種方法,所以代碼看起來比較長

【代碼】

RunID

User

Problem

Result

Memory

Time

Language

Length

Submit Time

2475104

zhyfzy

E

Accepted

0 KB

92 ms

C++ 4.8.2

1229 B

2014-07-23 15:36:26

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
char *w[101],c,s[101];
int i,j,k,n,T,K,m,f[100],num[100][26],len[100],lens,tt[26];

bool cmp(char *x,char *y)
{
	if (x[0]==y[0]) return x[strlen(x)-1]<y[strlen(y)-1];
	else return x[0]<y[0];
}

bool equals(int x[26],int y[26])
{
	for (int i=0;i<25;i++)
	{
		if (x[i]!=y[i]) return false;
	}
	return true;
}

int main()
{
	scanf("%d",&T);
	for (K=1;K<=T;K++)
	{
		
		
		scanf("%d%d",&n,&m);
		getchar();
		for(i=0;i<n;i++)
		{
			w[i]=new char[101];
			scanf("%s",w[i]);
		}
		sort(w,w+n,cmp);
		for (i=0;i<n;i++)
		{
			if (i==0||w[i][0]!=w[i-1][0]) f[w[i][0]-'a']=i;
			memset(num[i],0,sizeof(num[i]));
			len[i]=strlen(w[i])-1;
			for (j=0;j<=len[i];j++)
			{
				num[i][w[i][j]-'a']++;
			}
		}
		
		printf("Case #%d:\n",K);
		getchar();
		for (i=1;i<=m;i++)
		{
			do
			{
				scanf("%s%c",s,&c);
				lens=strlen(s)-1;
				memset(tt,0,sizeof(tt));
				for (j=0;j<=lens;j++)
				{
					tt[s[j]-'a']++;
				}
				for (j=f[s[0]-'a'];w[j][0]==s[0];j++)
				{
					if (lens==len[j]&&w[j][len[j]]==s[lens]&&equals(tt,num[j]))
					{
						printf("%s%c",w[j],c);
						break;
					}
				}
			}while (c==' ');
		}
	}
		
}


UVA 12725 Fat and Orial

【題目大意】

Fat同學做了A次比賽平均分爲N,想再做B次比賽平均分漲到M,求這B次比賽的平均分(分數最高爲10),不可能達到則輸出Impossible

【題解】

水題,ans=((a+b)*m-a*n)/b,注意分數不可能低於0和高於10,因爲沒有判斷低於0的情況wa了一次,還以爲是精度問題。。。。T.T

【代碼】

RunID

User

Problem

Result

Memory

Time

Language

Length

Submit Time

2477573

zhyfzy

F

Accepted

0 KB

142 ms

C++ 4.8.2

337 B

2014-07-23 19:38:06

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
int i,j,k,T,K;
double n,m,a,b,tt;
int main()
{
	scanf("%d",&T);
	for (K=1;K<=T;K++)
	{
		scanf("%lf%lf%lf%lf",&n,&m,&a,&b);
		tt=((a+b)*m-a*n)/b;
		if (tt>10||tt<0) printf("Case #%d: Impossible\n",K);
		else printf("Case #%d: %.2lf\n",K,tt);
	}
}


UVA 12729 Squares Game

【題目大意】

n*m的棋盤,ana往裏邊放2*2的方塊,bob往裏邊放1*1的方塊,anabob輪流放,ana先放,如果ana不能再放就只能讓給bob放,最終誰的面積最大誰贏,或者平局

【題解】

開始想的是組合遊戲,使用記憶化搜索,但發現並不是,首先,組合遊戲一定是隻有輸贏兩種狀態,而且由輸的狀態推出贏的狀態,這裏都不好推

所以不是組合遊戲

......

不妨設n,m都是偶數,每當ana佔據一個四格時,bob一定會破壞另一個四格,使得ana不能再去佔據它,這樣ana一定只能佔據所有四格數的一半,ana的面積就能確定

n,m中有奇數時,先按偶數考慮,多出來的部分顯然不能由ana佔據。。。

【代碼】

RunID

User

Problem

Result

Memory

Time

Language

Length

Submit Time

2477856

zhyfzy

J

Accepted

0 KB

19 ms

C++ 4.8.2

442 B

2014-07-23 20:10:55


#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
int i,j,k,n,T,nn,mm,ana,bob,K,m,sum;
int main()
{
	scanf("%d",&T);
	for (K=1;K<=T;K++)
	{
		scanf("%d%d",&n,&m);
		if (n%2) nn=n-1; else nn=n;
		if (m%2) mm=m-1; else mm=m;
		sum=nn*mm/4;//統計有多少個四格 
		ana=(sum+1)/2*4;//ana佔所有的四格的一半,當四格數是奇數則除以二向上取整 
		bob=n*m-ana;
		printf("Case #%d: ",K);
		if (ana>bob) printf("Ana\n");
		else
		if (ana<bob) printf("Bob\n");
		else
		printf("Draw\n");
	}
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章