Educational Codeforces Round 26 A B C 三道水题 - - D 动态规划

本文解析了四道涉及字符串处理、二维数组分析、几何计算及动态规划的编程题。通过详细的代码实现,展示了如何解决实际问题,适用于算法初学者及竞赛选手。

A:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;

int main(){
	int ans = 0;
	int n;
	cin>>n;
	char c;
	int maxn = 0;
	getchar();
	while( n-- ){
		c = getchar();
		if( c >= 'A' && c <= 'Z' ){
			ans++;
		}
		if( c == ' ' || n == 0 ) { maxn = max(maxn,ans); ans = 0;}
		//cout<<"ans ; "<<ans<<endl;
	}
	
	printf("%d\n",maxn);
	return 0;
}

B:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <map>
#include <algorithm>
using namespace std;
const int AX = 200+2;
char mp[AX][AX];
map<char,int>p;
int main(){
	int n,m;
	cin>>n>>m;
	for( int i = 0 ; i < n ; i++ ){
		scanf("%s",mp[i]);
	}
	int flagm = 1;
	int flagn = 1;

	if( m % 3 && n % 3 ) {printf("NO\n");return 0 ;}
	
	if( n == 3  && m == 1){
		if( mp[0][0] != mp[1][0] && mp[0][0] != mp[2][0] && mp[1][0] != mp[2][0] )
			{printf("YES\n");return 0;}  else {printf("NO\n");return 0;}
	}
	else if( m == 3  && n == 1){
		
		if( mp[0][0] != mp[0][1] && mp[0][0] != mp[0][2] && mp[0][1] != mp[0][2] ){
			printf("YES\n");return 0 ;
		} 
		else {printf("NO\n");return 0;}
	}

	if( m == 1 && n > 3 && n % 3 == 0 ){
		int ave = n/3;
		for( int i = 0; i < n ;i += ave ){
			for(int j = i+1 ; j < i+ave; j++ ){
				if( mp[j][0] != mp[j-1][0] ) {flagn = 0;break;}
				p[mp[j][0]] = 1;
			}
			if(flagn == 0 ) break;
		} 
		if( !p['R'] || !p['G'] || !p['B'] ) flagn = 0;
	}
	else if( n == 1 && m>3 && m % 3 == 0 ){
		int ave = m/3;
		for( int i = 0; i < m ;i += ave ){
			for(int j = i+1 ; j < i+ave; j++ ){
				if( mp[0][j] != mp[0][j-1] ) {flagm = 0;break;}
				p[mp[0][j]] = 1;
			}
			if(flagm == 0 ) break;
		} 
		if( !p['R'] || !p['G'] || !p['B'] ) flagm = 0;
	
	}else{

		if( m % 3 == 0 ){
			int ave = m/3;
			for( int j = 0,num = 1 ;j < m ; j++,num++ ){

				if( num  == ave + 1 ) { p[mp[1][j-1]] = 1;num = 1; }

				for( int i = 1 ; i < n  ; i ++ ){
					if( mp[i][j] != mp[i-1][j] || p[mp[i][j]] )  {flagm = 0 ; break;}
				}
				if( flagm == 0  ) break;
			}
		}
		if( n % 3 == 0 ){
			int ave = n/3;
			for( int i = 0,num = 1 ;i < n ; i++,num++ ){

				if( num  == ave + 1 ) { p[mp[i-1][1]] = 1; num = 1;}

				for( int j = 1 ; j < m  ; j++ ){
					if( mp[i][j] != mp[i][j-1] || p[mp[i][j]] )  { flagn = 0 ; break;}
				}
				if( flagn == 0  ) break;
			}
		}
		
	}


	if( m % 3 ==0 && n % 3 == 0  ){
		if( flagn || flagm ) printf("YES\n");
		else printf("NO\n");
	}else if( m % 3 == 0 ){
		if( flagm ) printf("YES\n");
		else printf("NO\n");
	}else{
		if( flagn ) printf("YES\n");
		else printf("NO\n");
	}

	return 0;
}


C:

#include <iostream>
#include <cstdio>
using namespace std;
const int AX = 1e2+3;
struct Node
{
	int x;
	int y;
}s[AX];

int main(){
	int n,a,b;
	int cnt = 0;
	scanf("%d%d%d",&n,&a,&b);
	int x1,y1;
	for( int i = 0 ; i < n ; i++ ){
		scanf("%d%d",&x1,&y1);
		if( ( y1 > a && y1 > b ) || ( x1 > a && x1 > b ) || (x1 == a && y1 == b) || (y1 == a && x1 == b ) ) continue;
		s[cnt].x = x1;
		s[cnt].y  = y1;
		cnt++;
	}
/*	for( int i = 0 ; i < cnt ; i ++ ){
		cout<<"s[]: "<<s[i].x<<' '<<" s[] "<<s[i].y<<endl;
	}*/

	int ans = 0;
	for( int i = 0 ; i < cnt ; i++ ){
		for( int j = 0 ; j < cnt ; j++ ){
			if( i == j ) continue;
			if( s[i].x + s[j].y <= a && s[i].y <=b && s[j].x <=b ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}
			if( s[i].x + s[j].y <= b && s[i].y <=a && s[j].x <=a ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}

			if( s[i].x + s[j].x <= a && s[i].y <=b && s[j].y <=b ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}
			if( s[i].x + s[j].x <= b && s[i].y <=a && s[j].y <=a ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}
			
			if( s[i].y + s[j].x <= a && s[i].x <=b && s[j].y <=b ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}
			if( s[i].y + s[j].x <= b && s[i].x <=a && s[j].y <=a ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}
			
			if( s[i].y + s[j].y <= b && s[i].x <=a && s[j].x <=a ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}
			if( s[i].y + s[j].y <= a && s[i].x <=b && s[j].x <=b ) 	{ ans = max( ans , s[i].x * s[i].y + s[j].x * s[j].y ); continue;}
		}
	}
	cout<<ans<<endl;

	return 0;
}


D:

D. Round Subset
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let's call the roundness of the number the number of zeros to which it ends.

You have an array of n numbers. You need to choose a subset of exactly k numbers so that the roundness of the product of the selected numbers will be maximum possible.

Input

The first line contains two integer numbers n and k (1 ≤ n ≤ 200, 1 ≤ k ≤ n).

The second line contains n space-separated integer numbers a1, a2, ..., an (1 ≤ ai ≤ 1018).

Output

Print maximal roundness of product of the chosen subset of length k.

Examples
Input
3 2
50 4 20
Output
3
Input
5 3
15 16 3 25 9
Output
3
Input
3 3
9 77 13
Output
0

Note

In the first example there are 3 subsets of 2 numbers. [50, 4] has product 200 with roundness 2, [4, 20] — product 80, roundness 1, [50, 20] — product 1000, roundness 3.

In the second example subset [15, 16, 25] has product 6000, roundness 3.

In the third example all subsets has product with roundness 0.

dp[i][j]表示选i个数中 5的个数,j代表 2的个数

#include <bits/stdc++.h>
#define LL long long
#define INF 0x7777777
using namespace std;
const int AX = 200+6;
const int maxn = 64*AX;
LL a[AX];
int dp[AX][maxn];
int main(){
	int n,k;
	while( ~scanf("%d%d",&n,&k) ){
		memset( a , 0 , sizeof(a) );
		LL x;
		for( int i = 0 ; i < n ; i++ ){
			cin>>a[i];
		}
		for( int i = 0 ; i <= k ; i++ ){
			for( int j = 0 ; j < maxn ; j++ ){
				dp[i][j] = -INF;
			}
		}
		dp[0][0] = 0;
		for( int i = 0 ; i < n ; i++ ){
			LL temp = a[i];
			int num2 = 0 , num5 = 0 ;
			while( temp % 5  == 0 ){
				num5 ++;
				temp /= 5;
			}
			temp = a[i];
			while( temp % 2 == 0 ){
				num2 ++;
				temp /= 2;
			}
			for (int j = k ; j >= 1; j-- ){
				for( int l = num2 ; l < maxn ;l++ ){
					dp[j][l] = max( dp[j-1][l-num2] + num5 ,dp[j][l] );
				}
			}
		}
		int res = 0;
		for( int i = 1 ; i < maxn ; i++ ){
			res = max( res , min( i , dp[k][i] ) );
		}
		printf("%d\n",res);

	}
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值