Hướng dẫn giải của Duyệt binh


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

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

var a:ansistring;
    x,y,i,n,re,dem:longint;
begin
     readln(n);
     readln(a);
     for i:=1 to n do
         if a[i]='<' then
         begin
              if (x>0) and (x+y>re) then re:=x+y;
              inc(y);
              dem:=dem+x;
         end
         else
         begin
              inc(x);
              if y>0 then dec(y);
         end;
     writeln(re);
end.

Code mẫu của happyboy99x

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

char s[1000001]; int n;

int main() {
    scanf("%d%s",&n,s);
    int res = 0, delay = 0, cnt = 0, i;
    for(i = 0; i < n && s[i] == '<'; ++i);
    for(; i<n; ++i) 
        if(s[i] == '<') 
            res = max(res, delay++ + cnt);
        else {
            ++cnt;
            if(delay > 0) --delay;
        }
    printf("%d\n", res);
    return 0;
}

Code mẫu của ladpro98

#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("PYRAMID2.txt", "r", stdin);
#endif // ONLINE_JUDGE
    int n; string s;
    cin >> n >> s;
    if (*s.rbegin() == '<') ++n, s += '>';
    vector<int> pos;
    int last = -1;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '>')
            pos.push_back(i);
        else
            last = i;
    }
    while (!pos.empty() && pos.back() > last + 1) pos.pop_back();
    int ans = 0;
    for (int i = int(pos.size()) - 2, cnt = 0, lastDelay = 0; i >= 0; --i) {
        cnt += pos[i + 1] - pos[i] - 1;
        int delay = max(lastDelay - (pos[i + 1] - pos[i]) + 2, 0);
        ans = max(ans, delay + cnt);
        lastDelay = delay;
    }
    cout << ans << endl;
    return 0;
}

Code mẫu của RR

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define MAXN 1000111
#define FOR(i,a,b)  for(long i=a; i<=b; i++)
#define FORD(i,a,b) for(long i=a; i>=b; i--)
using namespace std;
long n;
char a[MAXN];
int main() {
    scanf("%ld",&n); gets(a);
    gets(a); 
    while (a[n-1]=='>') n--;
    long time=0,len2=0,len1=0;
    FORD(i,n-1,0)
      if (a[i]=='>') {
             if (len2>0) time=max(time,len2+len1);
             len1++;
      } else {
             len1--; if (len1<0) len1=0;
             len2++;
      }
    printf("%ld\n",time);
    return 0;
}

Code mẫu của hieult

#include <stdio.h>
#include <string.h>
//#include <conio.h>

int main()
{
    // freopen("DUYETBINH.in","r",stdin);
     char s[1000001];
     int n;
     scanf("%ld",&n);
     scanf("%s",s) ;
     int inf =0;int sup =n-1;
     while (inf < n-1 && s[inf]=='<')  inf ++;
     while (sup > 0 && s[sup]=='>')  sup--;
     int out = 0;
     int count =-1;
     for(int i=inf;i<= sup;i++)
     {
         if(s[i]=='>')
         {
                      out++;
                      if( count > -1 )count--;
         }
        if(s[i]=='<') count++;
     }
     if(inf > sup) printf("0");
     else printf("%d",out + count);
    // getch();
}

Code mẫu của skyvn97

#include<bits/stdc++.h>
#define MAX   1000100
char s[MAX];
int delay[MAX];
int nmale[MAX];
int prev[MAX];
int n;
void maximize(int &x,const int &y) {
    if (x<y) x=y;
}
void init(void) {
    scanf("%d",&n);
    scanf("%s",&s[1]);
    prev[1]=0;
    nmale[1]=0;
    int i;
    for (i=2;i<=n;i=i+1) {
        if (s[i-1]=='<') prev[i]=i-1;
        else prev[i]=prev[i-1];
        nmale[i]=nmale[i-1]+(s[i-1]=='>');
    }
}
void count(void) {
    int i;
    for (i=1;i<=n;i=i+1)
        if (s[i]=='<') {
            if (nmale[i]==0) delay[i]=0;
            else {
                if (prev[i]==0) delay[i]=0;
                else delay[i]=delay[prev[i]]+1-(i-prev[i]-1);
                if (delay[i]<0) delay[i]=0;
            }           
        }
    int res=0;
    for (i=1;i<=n;i=i+1)
        if (s[i]=='<' && nmale[i]>0) maximize(res,delay[i]+nmale[i]);
    printf("%d",res);
}
int main(void) {
    init();
    count();
    return 0;
}

Code mẫu của khuc_tuan

import java.io.*;

public class Main{
    public static void main(String[] Args) throws Exception {
        BufferedReader kb=new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(kb.readLine());
        int tgc=0,ss1=0,res=0;
        char c, ct=(char)0;
        boolean bd2=false,bd=false;
        String s = kb.readLine();
        for(int i=0;i<n;++i) {
            c=s.charAt(i);
            if(!bd) {
                if(c=='<') continue;
                else bd=true;
            }
            if(c=='>') {
                ++ss1;
                if( bd2 && tgc>0 ) --tgc;
            }
            else {
                if(!bd2) bd2=true;
                res=Math.max(res,ss1+Math.max(tgc,0));
                ++tgc;
            }
            ct=c;
        }
        System.out.println(res);
    }
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.