Cod sursa(job #930960)

Utilizator Corina1997Todoran Ana-Corina Corina1997 Data 27 martie 2013 21:59:23
Problema Secventa Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <cstdio>
#include <algorithm>
using namespace std;

FILE * is = fopen("secventa.in", "r");
FILE * os = fopen("secventa.out", "w");

long n, k;
int a[500001];
int minim = 40000;
int aux;

struct Secventa{
    int st, dr, min;
};
Secventa c[500001];

int main()
{
    fscanf(is, "%d", &n);
    fscanf(is, "%d", &k);
    for(int i = 1; i <= n; i++)
        fscanf(is, "%d", &a[i]);
    for(int i = 1; i <= n - k + 1; i++)
    {
        minim = 40000;
        for(int j = i; j < i+k; j++)
            if(minim > a[j])
            {
                minim = a[j];
                c[i].st = i;
                c[i].dr = i+k-1;
                c[i].min = minim;
            }
    }
    for(int i = 1; i <=  n - k; i++)
    {
        for(int j = i + 1; j <= n - k + 1; j++)
        {
            if(c[i].min < c[j].min)
            {
                aux = c[i].min;
                c[i].min = c[j].min;
                c[j].min = aux;
                aux = c[i].dr;
                c[i].dr = c[j].dr;
                c[j].dr = aux;
                aux = c[i].st;
                c[i].st = c[j].st;
                c[j].st = aux;

            }
        }
    }
    fprintf(os, "%d %d %d", c[1].st, c[1].dr, c[1].min );
    fclose(is);
    fclose(os);
    return 0;
}