Submission #71499193


Source Code Expand

#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace IO {
	template<typename T> inline void read(T &x) {
		bool f = 1;
		x = 0;
		char ch = getchar();
		while (ch < '0' || ch > '9') {
			if (ch == '-')f = 0;
			ch = getchar();
		}
		while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
		x = f ? x : -x;
	}
	template<typename T> inline void write(T x) {
		if (x < 0) putchar('-'), x = -x;
		if (x > 9) write(x / 10);
		putchar(('0' + x % 10));
	}
	template <typename T, typename ...Args> inline
	void read(T &x, Args &...args) {read(x),read(args...);}
	template<typename T> inline void write(T x, char c) {write(x), putchar(c);}
}
using namespace IO;
const int INF=1e18;

int n;

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);

	cin>>n;
	vector<int> s(n+1);
    for(int i=1;i<=n;i++) cin>>s[i];
    vector<int> l(n+1),r(n+1),f(n+1),ff;
    for(int i=1;i<=n;i++)
	{
        int x=0;
        while(!ff.empty()&&s[ff.back()]<s[i]){x=ff.back();ff.pop_back();}
        if(!ff.empty()) r[ff.back()]=i,f[i]=ff.back();
        if(x) l[i]=x,f[x]=i;
        ff.push_back(i);
    }
    
    int ans=1;
    for(int i=1;i<=n;i++)
		if(f[i]==0)
		{
			ans=i;
			break;
		}
    vector<vector<int> > sum(n+1);
    for(int i=1;i<=n;i++)
	{
        if(l[i]) sum[i].push_back(l[i]);
        if(r[i]) sum[i].push_back(r[i]);
    }
    vector<int> fff;
	vector<int> ss;
	ss.push_back(ans);
    while(!ss.empty())
	{
		int u=ss.back();
		ss.pop_back();
		fff.push_back(u);
		for(int v:sum[u]) ss.push_back(v);
	}
    vector<int> Ans(n+1);
    for(int i=fff.size()-1;i>=0;i--)
	{
        int u=fff[i],x=0;
        for(int v:sum[u]) x=max(x,Ans[v]+llabs(u-v));
        Ans[u]=x;
    }
    cout<<Ans[ans]<<"\n";

	return 0;
}

Submission Info

Submission Time
Task F - Cat exercise
User NingMeng_yang
Language C++23 (GCC 15.2.0)
Score 550
Code Size 1862 Byte
Status AC
Exec Time 30 ms
Memory 24608 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 2
AC × 40
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3576 KiB
example_01.txt AC 1 ms 3488 KiB
hand_00.txt AC 23 ms 23820 KiB
hand_01.txt AC 23 ms 24556 KiB
hand_02.txt AC 23 ms 24608 KiB
hand_03.txt AC 24 ms 23872 KiB
hand_04.txt AC 22 ms 22232 KiB
hand_05.txt AC 1 ms 3508 KiB
hand_06.txt AC 1 ms 3532 KiB
hand_07.txt AC 22 ms 20668 KiB
random_00.txt AC 29 ms 21828 KiB
random_01.txt AC 29 ms 21792 KiB
random_02.txt AC 30 ms 21692 KiB
random_03.txt AC 30 ms 21716 KiB
random_04.txt AC 30 ms 21700 KiB
random_05.txt AC 28 ms 21712 KiB
random_06.txt AC 28 ms 21696 KiB
random_07.txt AC 29 ms 21720 KiB
random_08.txt AC 28 ms 21736 KiB
random_09.txt AC 29 ms 21788 KiB
random_10.txt AC 27 ms 22640 KiB
random_11.txt AC 27 ms 22480 KiB
random_12.txt AC 30 ms 21832 KiB
random_13.txt AC 27 ms 22240 KiB
random_14.txt AC 26 ms 24088 KiB
random_15.txt AC 29 ms 21756 KiB
random_16.txt AC 30 ms 21816 KiB
random_17.txt AC 27 ms 22204 KiB
random_18.txt AC 28 ms 21864 KiB
random_19.txt AC 26 ms 22096 KiB
random_20.txt AC 28 ms 22308 KiB
random_21.txt AC 28 ms 22748 KiB
random_22.txt AC 28 ms 21820 KiB
random_23.txt AC 27 ms 22336 KiB
random_24.txt AC 29 ms 21928 KiB
random_25.txt AC 27 ms 22184 KiB
random_26.txt AC 28 ms 21720 KiB
random_27.txt AC 28 ms 22432 KiB
random_28.txt AC 29 ms 21772 KiB
random_29.txt AC 30 ms 21924 KiB