【BestCoder】#33

1001

首先读进来的时候把字母和数字都转换成0到35的数字,加起来直接取模,算出答案。 坑点是只有1个数的情况,还有答案等于0的时候也要输出一行一个0。

注意去掉前导0,因为求和过程也有可能产生0,所以求完和在去0。

#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <set>
using namespace std;
#define read() freopen("data.in", "r", stdin)
#define write() freopen("data.out", "w", stdout)
#define rep( i , a , b ) for ( int i = ( a ) ; i <  ( b ) ; ++ i )  
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i ) 
#define clr( a , x ) memset ( a , x , sizeof a )  
#define cpy( a , x ) memcpy ( a , x , sizeof a ) 
#define _max(a,b) ((a>b)?(a):(b))
#define _min(a,b) ((a<b)?(a):(b))
#define LL long long 
const int maxNumber=205;

int sum[maxNumber];
char s[maxNumber];

int main()
{
	//read();
	int n,b;
	while(cin>>n>>b)
	{	
		int temp = 0;
		clr(sum,0);
		while(n--)
		{
			scanf("%s",s);
			int len = strlen(s);
			reverse(s,s+len);
			for (int i = 0; i < len; ++i)
			{
				if (s[i]>='a'&&s[i]<='z')
				{
					sum[i]+=(s[i]-'a'+10);
				}else
				{
					sum[i]+=s[i]-'0';
				}
			}	
			temp = _max(temp,len);
		}
		for (int i = 0; i < temp; ++i)
		{
			sum[i] %= b;
		}
		while (temp > 1 && sum[temp-1]==0)
		{
			--temp;
		}
		for (int i = temp-1; i >= 0; --i)
		{
			if (sum[i] < 10)
			{
				printf("%d",sum[i] );
			}else
			{
				printf("%c",sum[i]-10+'a' );
			}
		}
		printf("\n");
	}
    return 0;
   
}

1002

枚举最终的W堆积木在哪,确定了区间,那么就需要把高于H的拿走,低于H的补上,高处的积木放到矮的上面,这样最优。因此把这个区间变成W*H的代价就是max((HiH),(HHj))(HiH,HjH)即在把高的变矮和把矮的变高需要的移动的积木数取较大的。从第一个区间[1,W]到第二区间[2,W+1]只是改变了2堆积木,可以直接对这两堆积木进行删除和添加来维护(HiH)(HHj)。需要注意的是,最终选取的W堆积木中,有可能有几堆原本不存在。如 9 8 7 形成3*3,可把3堆积木变成5堆 3 3 3 8 7,最少移动6个积木。因此需要在n堆积木两端补上W个0。整个问题的复杂度是O(n+W).
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <ctime>
#include <set>
using namespace std;


#define read() freopen("data.in", "r", stdin)
#define write() freopen("data.out", "w", stdout)
#define clr( a , x ) memset ( a , x , sizeof a )  
#define cpy( a , x ) memcpy ( a , x , sizeof a ) 
#define _max(a,b) ((a>b)?(a):(b))
#define _min(a,b) ((a<b)?(a):(b))
#define LL long long 
const int maxNumber=50005;
LL sum;
int a[maxNumber];

int main()
{
	//read();
	int n,w,h;
	LL cnt;
	LL add;
	LL minus;
	while(cin>>n>>w>>h)
	{
		sum = 0;
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d",&a[i]);
			sum += a[i];
		}
		if (sum < (LL)h * w )
		{
			printf("-1\n");
			continue;
		}
		cnt = (LL)w*h;
		add = 0;
		minus = 0;
		add = (LL)w*h;
		for (int i = 1; i <= n + w; ++i)
		{
			if (i >= 1&&i <= n)
			{
				if (a[i] < h)
				{
					add += h - a[i];
				}else
				{
					minus += a[i] - h;
				}
			}else
			{
				add += h;
			}
			if (i >= w+1&&i <= n+w)
			{
				if (a[i-w] < h)
				{
					add -= h - a[i-w];
				}else
				{
					minus -= a[i-w] - h;
				}
			}else
			{
				add -= h;
			}
			cnt = _min(cnt,_max(add,minus));
		}
		cout<<cnt<<endl;

	}
    return 0;
   
}

  

 
posted @ 2015-03-24 17:47  Summer先生  阅读(148)  评论(0编辑  收藏  举报