Problem-A

概述:给出一段序列,求在这段序列中能求出的最大和,并且表示出这个和是从那里开始到哪里结束。

思路:最大序列和的问题。讲课时候讲过一道这样的题目,但是与那道题目不同的是,这道题要求把序列开始时数字的位置和结束时数字的位置写出,这个就要求在结束时选择的数字相等时就是结束的标志,然后倒着推回去,当数字相等时就是开始的位置。

思路:上午上课做的这道题,最开始落掉了位置这个。。wa了,改掉之后又然后因为输出格式的原因,一直wa。。。最后不清楚自己代码出的问题,找到了一个代码提交过。

原代码:

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

int max(int a,int b)
{
    if(a>b)
        return a;
    else return b;
}

int main()
{
    ifstream cin("in.txt");
    int T;
    int s,e,time=1;
    cin>>T;
    int N;
    int dp[100005];
    int a[100005];
    while(T--)
    {
        int temp=0;
        cin>>N;
        for(int i=1;i<=N;i++)
            cin>>a[i];
        int sum=0;
        dp[1]=max(a[1],0);
        for(int i=2;i<=N;i++)
            dp[i]=max(dp[i-1]+a[i],0);
        for(int i=1;i<=N;i++)
            if(sum<dp[i])
                {
                    sum=dp[i];
                    e=i;
                }
         for(int i=e;i>0;i--)
         {
             temp+=a[i];
             if(temp==sum)
                s=i;
         }
        cout<<"Case "<<time++<<":"<<endl;
        cout<<sum<<" "<<s<<" "<<e;
        if(T)
            cout<<endl<<endl;
    }
    return 0;
}

提交代码:

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

int get(int data[], int &l, int &r, int dl)
{
	int max = -10000000;
	l = 0;
	r = 0;
	int t = 1;
	int mt = 0;
	for (int i = 0; i < dl; i++)
	{
		mt = mt + data[i];
		if (mt > max)
		{
			max = mt;
			l = t; r = i + 1;
		}

		if (mt < 0)
		{
			mt = 0;
			t = i + 2;
		}
	}
	return max;
}

int main()
{
	//ifstream cin("in.txt");
	int T;
	cin >> T;
	for (int k = 1; k <= T;k++)
	{
		int n = 0;
		cin >> n ;
		int * data = new int[n];
		for (int i = 0;i < n; i++)
		{
			cin >> data[i];
		}
		int l = 0, r = n - 1;
		int max = 0;
		max = get(data, l, r, n);
		cout << "Case " << k << ":" << endl;
		cout << max << " " << l << " " << r << endl;

		if (k != T)
			cout << endl;

	}
	return 0;
}
时间: 2025-01-06 03:24:55

Problem-A的相关文章

A Math Problem

A Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 237    Accepted Submission(s): 117 Problem Description You are given a positive integer n, please count how many positive integers

Water Problem

water problem 发布时间: 2015年10月10日 15:34   时间限制: 1000ms   内存限制: 256M 描述 题意很简单 给你N个数, Q个查询 每次查询给你一个区间[L, R] 你要找出 [L, R] 这个区间里面取模M后的最大值. 输入 第一行一个T,表示测试数据组数.第二行两个整数N, M (1<=N<=10^5, 1<=M<=10^9).第三行给你N个整数 整数范围在1到10^9之间.第四行给你一个整数Q. ( 1<=Q<=10^5)

FOJ Problem 2261 浪里个浪

                                                                                                                                                           Problem 2261 浪里个浪 Accept: 40    Submit: 106Time Limit: 1500 mSec    Memory Limit : 32768 KB Pro

XJTUOJ wmq的A&#215;B Problem FFT

wmq的A×B Problem 发布时间: 2017年4月9日 17:06   最后更新: 2017年4月9日 17:07   时间限制: 3000ms   内存限制: 512M 描述 这是一个非常简单的问题. wmq如今开始学习乘法了!他为了训练自己的乘法计算能力,写出了n个整数,并且对每两个数a,b都求出了它们的乘积a×b.现在他想知道,在求出的n(n−1)2个乘积中,除以给定的质数m余数为k(0≤k<m)的有多少个. 输入 第一行为测试数据的组数. 对于每组测试数据,第一行为2个正整数n,

hidden node and exposed node problem

Exposed node problem In wireless networks, theexposed node problem occurs when a node is prevented from sending packets to other nodes because of a neighboring transmitter. Consider an example of 4 nodes labeled R1, S1, S2, and R2, where the two rece

南阳524 A-B Problem

A-B Problem 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 A+B问题早已经被大家所熟知了,是不是很无聊呢?现在大家来做一下A-B吧. 现在有两个实数A和B,聪明的你,能不能判断出A-B的值是否等于0呢? 输入 有多组测试数据.每组数据包括两行,分别代表A和B. 它们的位数小于100,且每个数字前中可能包含+,- 号. 每个数字前面和后面都可能有多余的0. 每组测试数据后有一空行. 输出 对于每组数据,输出一行. 如果A-B=0,输出YES,否则输出NO

Lab 1: Write a java program for the triangle problem and test the program with Junit.

Tasks: 1. Install Junit(4.12), Hamcrest(1.3) with Eclipse 将两个jar包添加到工程中 2. Install Eclemma with Eclipse 3. Write a java program for the triangle problem and test the program with Junit. [Description of triangle problem]Function triangle takes three i

HDU 1016 Prime Ring Problem(DFS)

题目链接 Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime. Note: the number of first circle should always

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

hdu 5293 Tree chain problem(树链剖分+树形dp)

题目链接:hdu 5293 Tree chain problem 维护dp[u], sum[u],dp[u]表示以u为根节点的子树的最优值.sum[u]表示以u节点的所有子节点的dp[v]之和.对于边a,b,w,在LCA(a,b)节点的时候进行考虑.dp[u] = min{dp[u], Sum(a,b) - Dp(a,b) + sum[u] | (ab链上的点,不包括u } #pragma comment(linker, "/STACK:1024000000,1024000000")