Pagini recente » Cod sursa (job #671035) | Cod sursa (job #671895) | Cod sursa (job #1540105) | Cod sursa (job #2500644) | Cod sursa (job #526402)
Cod sursa(job #526402)
// 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;
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();
}