Submission #48624423


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
// #define OPENIOBUF

namespace FastIO
{

class FastIOBase
{
 protected:
#ifdef OPENIOBUF
	static const int BUFSIZE=1<<16;
	char buf[BUFSIZE+1];
	int buf_p=0;
#endif
	FILE *target;
	FastIOBase(FILE *f): target(f){}
	~FastIOBase()=default;
 public:
#ifdef OPENIOBUF
	virtual void flush()=0;
#endif
};

class FastOutput final: public FastIOBase
{
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
	~FastOutput() { flush(); }
	void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
	void put(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	FastOutput &operator <<(char x)
	{ return put(x),*this; }
	FastOutput &operator <<(const char *s)
	{ for(;*s;put(*(s++)));return *this; }
	FastOutput &operator <<(const std::string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(T x)
	{
		if(x<0) return put('-'),(*this)<<(-x);
		char stk[std::numeric_limits<T>::digits10+1],*top=stk;
		do *(top++)=x%10+'0',x/=10; while(x);
		while(top!=stk) put(*(--top));
		return *this;
	}
	template<typename ...T>
	void writesp(T &&...x)
	{ std::initializer_list<int>{((*this)<<(x),put(' '),0)...}; }
	template<typename ...T>
	void writeln(T &&...x)
	{ std::initializer_list<int>{((*this)<<(x),put('\n'),0)...}; }
	template<typename Iter>
	std::enable_if_t<std::is_base_of<
		std::forward_iterator_tag,
		typename std::iterator_traits<Iter>::iterator_category>
	::value> writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	std::enable_if_t<std::is_base_of<
		std::forward_iterator_tag,
		typename std::iterator_traits<Iter>::iterator_category>
	::value> writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;

class FastInput final: public FastIOBase
{
 private:
	bool __eof;
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
	bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	bool eof()const { return feof(target); }
#endif
	char get()
	{
		if(__eof) return EOF;
#ifdef OPENIOBUF
		if(buf_p==BUFSIZE) flush();
		char ch=buf[buf_p++];
#else
		char ch=getc(target);
#endif
		return ~ch?ch:(__eof=true,EOF);
	}
	void unget(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf[--buf_p]=c;
#else
		ungetc(c,target);
#endif
	}
	explicit operator bool()const { return !__eof; }
	FastInput &operator >>(char &x)
	{ while(isspace(x=get()));return *this; }
	template<typename T>
	std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=get()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=get();
		for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
		return unget(ch),sym?x=-x:x,*this;
	}
	FastInput &operator >>(char *s)
	{
		while(isspace(*s=get()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=get());
		return unget(*s),*s='\0',*this;
	}
	FastInput &operator >>(std::string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=get()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		unget(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	void read(T &&...x)
	{ std::initializer_list<int>{((*this)>>(x),0)...}; }
	template<typename Iter>
	std::enable_if_t<std::is_base_of<
		std::forward_iterator_tag,
		typename std::iterator_traits<Iter>::iterator_category>
	::value> read(Iter begin,Iter end)
	{ while(begin!=end) (*this)>>*(begin++); }
}qin;

} // namespace FastIO
using FastIO::qin,FastIO::qout;

using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;

#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define LOG(...) [](auto...){}(__VA_ARGS__)
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif

constexpr int MAXN=2e5;

int n,m,a[MAXN+5],nn,lsh[2*MAXN+5],bk[2*MAXN+5];

int main()
{
	qin>>n>>m;
	qin.read(a+1,a+n+1);
	for(int i=1;i<=n;i++) lsh[i]=a[i];
	sort(lsh+1,lsh+n+1);
	nn=unique(lsh+1,lsh+n+1)-lsh-1;
	for(int i=1;i<=n;i++) bk[lower_bound(lsh+1,lsh+nn+1,a[i])-lsh]++;
	for(int i=1;i<=nn;i++) lsh[i+nn]=lsh[i]+m,bk[i+nn]=bk[i];
	LL ans=0;
	set<pair<int,int>> s;
	for(int i=1;i<=nn;i++) s.emplace(-bk[i],lsh[i]);
	for(int i=nn+1,j=1;i<=2*nn;i++)
	{
		s.emplace(-bk[i],lsh[i]);
		while(lsh[i]-lsh[j]>=m)
			s.erase(make_pair(-bk[j],lsh[j])),j++;
		auto [c,p]=*s.begin();
		ans=max(ans,m*LL(n-1-(-c))+(p-lsh[i]+m));
	}
	qout<<ans<<'\n';
	return 0;
}

Submission Info

Submission Time
Task A - Shuffle and mod K
User balalida
Language C++ 20 (gcc 12.2)
Score 500
Code Size 5045 Byte
Status AC
Exec Time 109 ms
Memory 16840 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 40
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt
Case Name Status Exec Time Memory
example_00.txt AC 1 ms 3472 KiB
example_01.txt AC 1 ms 3504 KiB
test_00.txt AC 1 ms 3476 KiB
test_01.txt AC 90 ms 15068 KiB
test_02.txt AC 56 ms 11368 KiB
test_03.txt AC 53 ms 10732 KiB
test_04.txt AC 11 ms 4984 KiB
test_05.txt AC 12 ms 5072 KiB
test_06.txt AC 11 ms 5088 KiB
test_07.txt AC 11 ms 5044 KiB
test_08.txt AC 11 ms 4988 KiB
test_09.txt AC 109 ms 16812 KiB
test_10.txt AC 104 ms 16768 KiB
test_11.txt AC 104 ms 16708 KiB
test_12.txt AC 1 ms 3452 KiB
test_13.txt AC 1 ms 3512 KiB
test_14.txt AC 4 ms 4216 KiB
test_15.txt AC 28 ms 7788 KiB
test_16.txt AC 35 ms 8564 KiB
test_17.txt AC 103 ms 16840 KiB
test_18.txt AC 2 ms 3672 KiB
test_19.txt AC 6 ms 4284 KiB
test_20.txt AC 10 ms 5100 KiB
test_21.txt AC 4 ms 3896 KiB
test_22.txt AC 5 ms 4060 KiB
test_23.txt AC 5 ms 4016 KiB
test_24.txt AC 9 ms 4540 KiB
test_25.txt AC 12 ms 5064 KiB
test_26.txt AC 12 ms 5032 KiB
test_27.txt AC 14 ms 4720 KiB
test_28.txt AC 17 ms 4784 KiB
test_29.txt AC 19 ms 4908 KiB
test_30.txt AC 19 ms 5028 KiB
test_31.txt AC 19 ms 5132 KiB
test_32.txt AC 52 ms 9336 KiB
test_33.txt AC 18 ms 5720 KiB
test_34.txt AC 7 ms 4312 KiB
test_35.txt AC 61 ms 9956 KiB
test_36.txt AC 66 ms 10460 KiB
test_37.txt AC 66 ms 10492 KiB