Pagini recente » Cod sursa (job #1304701) | Cod sursa (job #3318971) | Cod sursa (job #3131303) | Cod sursa (job #526422) | Cod sursa (job #526404)
Cod sursa(job #526404)
// http://infoarena.ro/problema/ssm
#include <fstream>
//#include <vector>
using namespace std;
#define INFINITY 0x3f3f3f3f
int lenght;
int bestSum,leftPosition,rightPosition;
/*vector<int> array;
vector<int> sum;
vector<int> best;*/
int array[6000000],sum[6000000],best[6000000];
void read();
void getMaxSubsequence();
void findLeftPosition();
void write();
int main() {
read();
getMaxSubsequence();
findLeftPosition();
write();
return (0);
}
void read() {
ifstream in("ssm.in");
in >> lenght;
/*sum.resize(lenght+1);
best.resize(lenght+1);
array.resize(lenght+1);*/
for(int i=1;i<=lenght;i++)
in >> array[i];
in.close();
}
void getMaxSubsequence() {
bestSum = array[1];
for(int i=1;i<=lenght;++i) {
best[i] = array[i];
if(best[i] < best[i-1] + array[i])
best[i] = best[i-1] + array[i];
if(bestSum < best[i]) {
bestSum = best[i];
rightPosition = i;
}
}
}
void findLeftPosition() {
int tmp = 0;
for(int i=rightPosition;i>=1;i--) {
tmp = tmp + array[i];
if(tmp == bestSum) {
leftPosition = i;
break;
}
}
}
void write() {
ofstream out("ssm.out");
out << bestSum << " " << leftPosition << " " << rightPosition << "\n";
out.close();
}