Cod sursa(job #43631)

Utilizator DeeAdit andreea sorina DeeA Data 30 martie 2007 12:31:53
Problema Secventa 2 Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
#include<fstream>
#include<vector>
using namespace std;

int n, k;
vector<int> a;
vector<vector<int> > s;
int smax = 0, pi, pf, suma;
void read();
void solve();
void write();
int sum(int st, int dr);

ofstream fout("secv2.out");

int main()
{
    read();
    solve();
    write();
    fout.close();
    return 0;
}

void read()
{
    ifstream fin("secv2.in");
    fin >> n >> k;
    //int x;
    a.resize(n+1);
    for(int i = 1 ; i <= n; i++)
    {
       fin >> a[i];
   }    
    s.resize(n+1);
    for(int i = 1 ; i <= n; i++)
       s[i].resize(n+1);
    fin.close();
}

void solve()
{
    for(int i = 1; i <= n; i++)
       for(int j = 1; j <= n; j++)
          s[i][j] = 0;
    s[1][1] = a[1];
    for(int i = 1; i <= n; i++)
       for(int j = i + k; j <= n; j++)
           s[i][j] = sum(i, j);
    smax = 0;
    for(int i = 1; i <= n; i++)
       for(int j = 1; j <= n; j++)
          if(s[i][j] > smax  )
          {
              smax = s[i][j];
              pi = i;
              pf = j;
          }
         
}

int sum(int st, int dr)
{
    suma = 0;
    for(int i = st; i <= dr; i++)
       suma += a[i];
    return suma;
}
          
void write()
{
    fout << pi << " "<< pf << " " << smax;
}