Mai intai trebuie sa te autentifici.
Cod sursa(job #1126338)
Utilizator | Data | 26 februarie 2014 22:43:53 | |
---|---|---|---|
Problema | Subsecventa de suma maxima | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.11 kb |
#include <iostream>
#include <fstream>
#include <vector>
std::ostream& operator<<(std::ostream& ostream, std::vector<int> v) {
unsigned int size = v.size();
for (unsigned int i = 0; i < size; i++)
ostream << v[i] << " ";
ostream << std::endl;
return ostream;
}
std::istream& operator>>(std::istream& istream, std::vector<int>& v) {
unsigned int n;
istream >> n;
int x;
for (unsigned int i = 0; i < n; i++) {
istream >> x;
v.push_back(x);
}
return istream;
}
int main() {
std::ifstream f("ssm.in");
std::ofstream g("ssm.out");
std::vector<int> v;
f >> v;
f.close();
int s = 0;
int start, ind, end, maxSubSum;
int size = v.size();
start = 0;
maxSubSum = v[0];
s = v[0];
for (int i = 0; i < size; i++) {
if (s < 0) {
s = v[i];
ind = i;
} else {
s += v[i];
}
if (s > maxSubSum) {
maxSubSum = s;
end = i;
start = ind;
}
}
g << maxSubSum << " " << start + 1 << " " << end + 1;
g.close();
return 0;
}