hdu_1003_Max Sum hdu_1058_Humble Numbers hdu_1059_Dividing

hdu_1003_Max Sum

http://acm.hdu.edu.cn/showproblem.php?pid=1003

题目大意是给出一组数,求出这组数的一个最大的连续子序列的和,并且要求出序列的左右区间。

这道题不会做,膜拜后的思想是,一个连续序列s要加上一个数a,除非该连续序列s的sum已经>0,否则a可以独立成一个序列s‘。于是,用maxsum记录最大的序列和,l和r记录左右区间。

#include <cstring>
#include <iostream>
using namespace std;

#define INF 10000000

int main (){
	int C;
	cin>>C;
	for (int c=1;c<=C;c++){
		int n,s=1,e=1;
		int a[100010],dp[100010];
		memset(dp,0,sizeof(dp));
		cin>>n;a[0]=0;
		for (int i=1;i<=n;i++){
			cin>>a[i];
		}
		int maxsum=-INF,sum=0,left=1;
		for (int i=1;i<=n;i++){
			sum+=a[i];
			if (sum>maxsum){
				maxsum=sum;
				s=left;
				e=i;
			}
			if (sum<0) {
				sum=0;
				left=i+1;
			}
		}
		if (c-1) cout<<endl;
		cout<<"Case "<<c<<":"<<endl;
		cout<<maxsum<<" "<<s<<" "<<e<<endl;
	}
	return 0;
}

hdu_1058_Humble Numbers

http://acm.hdu.edu.cn/showproblem.php?pid=1058

一个数的素因子只由2,3,5,7组成,则叫这个数为humble number,序列有:1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 24, 25, 27, ...

现在给你一个数n,要求输出第n个humble number;第1个humble number是1;

(本地暴力打表一分多钟算出5842个humble number,输出到 xxx.txt 里,然后copy数字到代码里存成数组,然后就可以直接输出来了。呵呵。我第一次AC就是这样);

呵呵:

一个数是humble number,那么这个数除以2,3,5,7也是humble number,反之,已知一个humble number序列,那么他的下一个humble number一定是该序列的某个数乘以2,3,5,7的某个数。

如此:dp[i] = min ( { dp[0] ... dp[i-1] } * {2,3,5,7} > dp[i-1] );这样先求出签5842个存到dp[]里。

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	long long dp[5850];
	long long a[]={2,3,5,7};
	dp[0]=1;
	long long minx;
	for (int i=1;i<5842;i++){
		minx=dp[i-1]*2;
		for (int j=i-2;j>=0;j--){
			if (dp[j]*7<dp[i-1]) break;
			for (int k=0;k<4;k++){
				if ((dp[j]*a[k])>dp[i-1]){
					minx=min(minx,dp[j]*a[k]);
				}
			}
		}
		dp[i]=minx;
	}
	int n;
	while (scanf ("%d",&n)!=EOF){
		if (n!=0){

			int a;int b;int i;int v=n;
			{
				char s[][5]={"st","nd","rd","th",""};
				a=n%10;
				n/=10;
				b=n%10;
				if (b==1) i=3;
				else{
					if (a==1) i=0;
					else if (a==2) i=1;
					else if (a==3) i=2;
					else i=3;
			 	}

				cout<<"The "<<v<<s[i]<<" humble number is ";
			}
			cout<<dp[v-1]<<"."<<endl;
		}
		else break;
	}
	return 0;

}

hdu_1059_Dividing

http://acm.hdu.edu.cn/showproblem.php?pid=1059

就这题思路还算清晰 =。=

一堆货物分为6类价值分别是1~6;第i类有ni个。问是否可以平均分成总价值相等的两类。

一个简单的多重背包问题,价值等于重量,dp[i]表示是否可以凑凑价值为i。如果dp[sum/2]=1;则可以dividing,(sum为1~6的总价值量,前提是sum%2==0);

多重背包可行性,use数组记录使用情况。

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int dp[420010];
int use[420010];

int main()
{
	// freopen ("g.txt","r",stdin);
	int a[6];
	int flag=1;
	int sum;
	int c=0;
	while (flag){
		c++;
		memset(dp,0,sizeof(dp));
		flag=0;sum=0;
		for (int i=0;i<6;i++){
			cin>>a[i];
			if (a[i]){
				flag=1;
			}
			sum+=a[i]*(i+1);
		}
		dp[0]=1;
		for (int i=0;i<6;i++){
			memset(use,0,sizeof(use));
			for (int j=i+1;j<=sum/2;j++){
				if (!dp[j]&&dp[j-(i+1)]&&use[j-(i+1)]<a[i]){
					dp[j]=1;
					use[j]=use[j-(i+1)]+1;
				}
			}
		}
		/*for (int i=0;i<=sum/2;i++){
			cout<<dp[i]<<" ";
		}cout<<endl;*/
		if (flag==0) continue;
		cout<<"Collection #"<<c<<":"<<endl;
		if (dp[sum/2]==1&&sum/2*2==sum){
			cout<<"Can be divided."<<endl;
		}
		else cout<<"Can't be divided."<<endl;
		cout<<endl;
	}
	return 0;
}

dp我水题不水=。=

时间: 2024-10-07 02:14:39

hdu_1003_Max Sum hdu_1058_Humble Numbers hdu_1059_Dividing的相关文章

hdu_1003_Max Sum

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142281    Accepted Submission(s): 33104 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

FreeCodeCamp之sum all numbers in a range

我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和.最小的数字并非总在最前面. sumAll([1, 4]) 应该返回一个数字. sumAll([1, 4]) 应该返回 10. sumAll([4, 1]) 应该返回 10. sumAll([5, 10]) 应该返回 45. sumAll([10, 5]) 应该返回 45. 题目给出的提示是Math.max()   Math.min()   和array.reduce().前两个用法一模一样,是静态方法,Math.max(

The sum of numbers form 0 to n.(20.9.2017)

#include <stdio.h> int main() { int a,b,sum; printf("输入一个数字: "); scanf("%d",&a); //输入数字a sum = 0; for(b=1;b<=a;b++){ //设置b=1 使用for loop,用b相加,一直加到输入的数字a的数值 sum = sum + b; } printf("%d",sum); }

Hdu_1003_Max Sum 解题心得

原题: Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains an

Sum All Numbers in a Range

我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 如果你被难住了,记得使用 Read-Search-Ask.尝试与他人结伴编程.编写你自己的代码. 这是一些对你有帮助的资源: Math.max() Math.min() Array.reduce() 下面的方法使用 apply 方法寻找一个数值数组中的最大元素. function getMaxOfArray(numArray) { return Math.max.apply(null, nu

【leetcode】Sum Root to Leaf Numbers(hard)

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T

[LintCode] Submatrix Sum 子矩阵之和

Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number. Have you met this question in a real interview? Yes Example Given matrix [ [1 ,5 ,7], [3 ,7 ,-8],

Submatrix Sum

Given an integer matrix, find a submatrix where the sum of numbers is zero. Your code should return the coordinate of the left-up and right-down number. Subarray Sum的follow up.求二维矩阵上和为0的submatrix. 这题暴力解法枚举左上两坐标,右下两坐标,求中间和.复杂度为O(n^6).太高. 比较好的办法是枚举开始行和

Maximum Subsequence Sum - 最大子列和问题_C语言实现

第一次写这方面的blog.自己也是初次接触相关知识,写的有不妥的地方十分欢迎大家指正~ 这是浙大PAT上的一道算法题(据说是浙大04年研究生复试题),题目是这样的: Maximum Subsequence Sum Given a sequence of KK integers { N_1N?1??, N_2N?2??, ..., N_KN?K?? }. A continuous subsequence is defined to be { N_iN?i??, N_{i+1}N?i+1??, ..