Editorial for Dãy số


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
    int f[10100]={0},n,k,x,ans=0;
    cin >> n >> k;
    for (int i=1;i<=n;i++)
    {
        cin >> x;
        f[i]=-(1<<29);
        for (int j=1;j<=k;j++)
            if (i-j>=0) f[i]=max(f[i],f[i-j]+x);
        ans=max(ans,f[i]);
    }
    cout << ans << endl;
}

Code mẫu của happyboy99x

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<int> vi;
typedef vector<vi> vvi;

#define sz(a) int((a).size())
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c,i) for(typeof((c).begin()) i = (c).begin(), _e = (c).end(); i != _e; ++i)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define rep(i,n) for(int i = 0, _n = (n); i < _n; ++i)
#define repd(i,n) for(int i = (n)-1; i >= 0; --i )
#define fo(i,a,b) for(int i = (a), _b = (b); i <= _b; ++i)
#define fod(i,a,b) for(int i = (a), _b = (b); i >= _b; --i)

#define INF 1000000000
#define N 10005

int a[N], f[N], n, k;

int main() {
#ifndef ONLINE_JUDGE
    freopen( "input.txt", "r", stdin );
    //freopen( "output.txt", "w", stdout );
#endif
    scanf("%d%d",&n,&k);
    fo(i,1,n) scanf("%d",a+i);
    f[0] = 0;
    fo(i,1,n) f[i] = a[i] + *max_element(f+max(0,i-k), f+i);
    printf("%d\n", *max_element(f,f+n+1));
    return 0;
}

Code mẫu của ladpro98

program amsseq2;
uses    math;
const   fi='';
var     f,a:array[0..10001] of longint;
        maxnum: array[0..10001] of longint;
        res,i,n,k,j:longint;
        inp:text;

procedure input;
var     i:longint;
begin
        assign(inp,fi);
        reset(inp);
        readln(inp,n,k);
        for i:=1 to n do read(inp,a[i]);
        close(inp);
end;



procedure init;
var     i,j,tmp:longint;
begin
       tmp:=low(longint);
       for i:=0 to k-1 do
        begin
                tmp:=max(tmp,a[i]);
                maxnum[i+1]:=tmp;
        end;
        for i:=k+1 to n+1 do
        begin
                tmp:=low(longint);
                for j:=i-1 downto i-k do
                begin
                        tmp:=max(tmp,a[j]);

                end;
                maxnum[i]:=tmp;

        end;
end;

begin
        input;
        init;
        f[0]:=0;
        f[1]:=a[1];
        res:=0;
        for i:=2 to k do
        begin
                f[i]:=low(longint);
                for j:=i-1 downto 0 do
                f[i]:=max(f[j]+a[i],f[i]);
                res:=max(res,f[i]);
        end;
        for i:=k+1 to n+1 do
        begin
                f[i]:=low(longint);
                for j:=i-1 downto i-k do
                f[i]:=max(f[j]+a[i],f[i]);
                res:=max(res,f[i]);
        end;
        write(res);
end.

Code mẫu của RR

#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>

#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair

#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;

//Buffer reading
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
    if(!*inp) { \
        if (REACHEOF) return 0;\
        memset(BUF,0,sizeof BUF);\
        int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
        if (inpzzz != BUFSIZE) REACHEOF = true;\
        inp=BUF; \
    } \
    INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
    AM=0;\
    GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
    if (INP=='-') {AM=1;GETCHAR(INP);} \
    j=INP-'0'; GETCHAR(INP); \
    while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
    if (AM) j=-j;\
}
//End of buffer reading

const long double PI = acos((long double) -1.0);

int a[10111], f[10111];

int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    FOR(i,1,n) scanf("%d", &a[i]);
    f[0] = 0;
    int res = 0;
    FOR(i,1,n) {
        f[i] = f[i-1] + a[i];
        FOR(j,1,k)
            if (i - j >= 0)
                f[i] = max(f[i], f[i-j] + a[i]);
        res = max(res, f[i]);
    }
    printf("%d\n", res);
    return 0;
}

Code mẫu của hieult

#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <iostream>
#include <algorithm>

#include <ctime>
#include <deque>
#include <bitset>
#include <cctype>
#include <utility>
#include <cassert>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

#define Rep(i,n) for(int i = 0; i < (n); ++i)
#define Repd(i,n) for(int i = (n)-1; i >= 0; --i)
#define For(i,a,b) for(int i = (a); i <= (b); ++i)
#define Ford(i,a,b) for(int i = (a); i >= (b); --i)
#define Fit(i,v) for(__typeof((v).begin()) i = (v).begin(); i != (v).end(); ++i)
#define Fitd(i,v) for(__typeof((v).rbegin()) i = (v).rbegin(); i != (v).rend(); ++i)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(), (a).end()
#define ms(a,x) memset(a, x, sizeof(a))

template<class F, class T> T convert(F a, int p = -1) {
  stringstream ss;
  if (p >= 0)
    ss << fixed << setprecision(p);
  ss << a;
  T r;
  ss >> r;
  return r;
}
template<class T> T gcd(T a, T b) {
  T r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
template<class T> T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template<class T> T sqr(T x) {
  return x * x;
}
template<class T> T cube(T x) {
  return x * x * x;
}
template<class T> int getbit(T s, int i) {
  return (s >> i) & 1;
}
template<class T> T onbit(T s, int i) {
  return s | (T(1) << i);
}
template<class T> T offbit(T s, int i) {
  return s & (~(T(1) << i));
}
template<class T> int cntbit(T s) {
  return s == 0 ? 0 : cntbit(s >> 1) + (s & 1);
}

typedef pair<int, int> II;

const double PI = acos(-1.0);
const double eps = 1e-3;

const int dr[] = { -1, 0, +1, 0 };
const int dc[] = { 0, +1, 0, -1 };

const int inf = (int) 1e9 + 5;
const ll linf = (ll) 1e16 + 5;
const ll mod = (ll) 1e9 + 7;

#define maxn 10005

int n, k, f[maxn], a[maxn];

int main() {
//  freopen("in.txt", "r", stdin);

    cin >> n >> k;
    For(i, 0, n) f[i] = -inf;
    For(i, 1, n) {
        cin >> a[i];
    }
    f[0] = 0;
    int res = 0;
    For(i, 1, n){
        for(int j = i - 1; j >= 0 && j >= i - k; j--){
            f[i] = max(f[i], f[j] + a[i]);
        }
        res = max(res, f[i]);
    }

    cout << res;

    return 0;
}

Code mẫu của skyvn97

#include<stdio.h>
#define MAX   20000
int n,k,i,j;
int a[MAX];
long opt[MAX];
int main(void)
{
    scanf("%d",&n);
    scanf("%d",&k);
    for (i=1;i<=n;i=i+1) scanf("%d",&a[i]);
    for (i=1;i<=k;i=i+1) opt[n+i]=0;
    for (i=n;i>=0;i=i-1)
        for (j=1;j<=k;j=j+1)
            if (opt[i+j]+a[i]>opt[i]) opt[i]=opt[i+j]+a[i];
    printf("%ld",opt[0]);
    return 0;
}

Comments

Please read the guidelines before commenting.


There are no comments at the moment.