Submission #43426713


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
{
#ifdef OPENIOBUF
 public:
	void flush()
	{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
 private:
	void __putc(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	template<typename T>
	void __write(T x)
	{
		char stk[64],*top=stk;
		if(x<0) return __putc('-'),__write(-x);
		do *(top++)=x%10,x/=10; while(x);
		for(;top!=stk;__putc(*(--top)+'0'));
	}
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
	~FastOutput(){ flush(); }
#endif
	FastOutput &operator <<(char x)
	{ return __putc(x),*this; }
	FastOutput &operator <<(const char *s)
	{ for(;*s;__putc(*(s++)));return *this; }
	FastOutput &operator <<(const string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
	{ return __write(x),*this; }
	template<typename ...T>
	void writesp(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	void writeln(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc('\n'),0)...}; }
	template<typename Iter>
	void writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	void writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;

class FastInput final: public FastIOBase
{
#ifdef OPENIOBUF
 public:
	void flush()
	{ buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
#endif
 private:
	bool __eof;
	char __getc()
	{
		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 __ungetc(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf_p--;
#else
		ungetc(c,target);
#endif
	}
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	bool eof()const { return feof(target); }
#endif
	char peek() { return __getc(); }
	explicit operator bool()const { return !__eof; }
	FastInput &operator >>(char &x)
	{ while(isspace(x=__getc()));return *this; }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=__getc()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=__getc();
		for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
		return __ungetc(ch),sym?x=-x:x,*this;
	}
	FastInput &operator >>(char *s)
	{
		while(isspace(*s=__getc()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=__getc());
		return __ungetc(*s),*s='\0',*this;
	}
	FastInput &operator >>(string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=__getc()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=__getc()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		__ungetc(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	void read(T &...x)
	{ initializer_list<int>{(this->operator>>(x),0)...}; }
	template<typename Iter>
	void 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;
constexpr int INF=2e9;

#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#else
#define FILEIO(file)
#endif

int n,a[200005],b[200005];
set<pair<int,int>> s1,s2;

inline void add(int i)
{
	s1.emplace(b[i]-a[i],i);
}
inline void del(int i)
{
	s1.erase({b[i]-a[i],i});
}
int main()
{
	qin>>n;
	for(int i=1;i<=n;i++)
		qin>>a[i]>>b[i],add(i);
	LL ans=0;
	for(int i=1;i<=n;i++)
	{
		int x=s1.begin()->second;
		del(x),swap(a[x],b[x]),add(x);
		x=s1.begin()->second;
		del(x),ans+=a[x];
	}
	qout<<ans<<'\n';
	return 0;
}

Submission Info

Submission Time
Task C - Reversible Card Game
User balalida
Language C++ (GCC 9.2.1)
Score 500
Code Size 4528 Byte
Status AC
Exec Time 144 ms
Memory 14512 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 40
Set Name Test Cases
Sample sample-01.txt, sample-02.txt
All in-01.txt, in-02.txt, in-03.txt, in-04.txt, in-05.txt, in-06.txt, in-07.txt, in-08.txt, in-09.txt, in-10.txt, in-11.txt, in-12.txt, in-13.txt, in-14.txt, in-15.txt, in-16.txt, in-17.txt, in-18.txt, in-19.txt, in-20.txt, in-21.txt, in-22.txt, in-23.txt, in-24.txt, in-25.txt, in-26.txt, in-27.txt, in-28.txt, in-29.txt, in-30.txt, in-31.txt, in-32.txt, in-33.txt, in-34.txt, in-35.txt, in-36.txt, in-37.txt, in-38.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
in-01.txt AC 128 ms 14460 KiB
in-02.txt AC 126 ms 14428 KiB
in-03.txt AC 124 ms 14496 KiB
in-04.txt AC 127 ms 14296 KiB
in-05.txt AC 105 ms 14512 KiB
in-06.txt AC 104 ms 14468 KiB
in-07.txt AC 108 ms 14424 KiB
in-08.txt AC 3 ms 3552 KiB
in-09.txt AC 2 ms 3388 KiB
in-10.txt AC 4 ms 3436 KiB
in-11.txt AC 2 ms 3576 KiB
in-12.txt AC 2 ms 3532 KiB
in-13.txt AC 2 ms 3432 KiB
in-14.txt AC 2 ms 3552 KiB
in-15.txt AC 2 ms 3588 KiB
in-16.txt AC 2 ms 3372 KiB
in-17.txt AC 2 ms 3544 KiB
in-18.txt AC 2 ms 3592 KiB
in-19.txt AC 38 ms 5904 KiB
in-20.txt AC 17 ms 4820 KiB
in-21.txt AC 73 ms 9456 KiB
in-22.txt AC 101 ms 11532 KiB
in-23.txt AC 87 ms 10580 KiB
in-24.txt AC 3 ms 3576 KiB
in-25.txt AC 2 ms 3544 KiB
in-26.txt AC 4 ms 3664 KiB
in-27.txt AC 5 ms 3748 KiB
in-28.txt AC 2 ms 3516 KiB
in-29.txt AC 2 ms 3380 KiB
in-30.txt AC 2 ms 3440 KiB
in-31.txt AC 1 ms 3376 KiB
in-32.txt AC 3 ms 3540 KiB
in-33.txt AC 2 ms 3380 KiB
in-34.txt AC 144 ms 14460 KiB
in-35.txt AC 136 ms 14468 KiB
in-36.txt AC 142 ms 14468 KiB
in-37.txt AC 136 ms 14460 KiB
in-38.txt AC 140 ms 14316 KiB
sample-01.txt AC 2 ms 3592 KiB
sample-02.txt AC 2 ms 3532 KiB