Submission #836098

#TimeUsernameProblemLanguageResultExecution timeMemory
836098josanneo22Sightseeing in Kyoto (JOI22_kyoto)C++17
100 / 100
8 ms5460 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ld long double
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define ar array
/* observation:
	with a shit ton of inequalities:
	we only go up in the middle if 
	(Ab-Ac)/(b-c)<(Bx-By)/(x-y)<(Aa-Ab)/(a-b)
*/
//ca compares a's
const int N=1e6+6000;
int A[N],B[N],aa[N],bb[N];
bool ca(int x,int y,int z) {
	return (A[x]-A[y])*(x-z)<(A[x]-A[z])*(x-y);
}
bool cb(int x,int y,int z) {
	return (B[x]-B[y])*(x-z)<(B[x]-B[z])*(x-y);
}
bool cab(int x,int y,int z,int w) {
	return (A[x]-A[y])*(w-z)<(B[z]-B[w])*(y-x);
}
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline int rd()
{
	int s=0;
	char ch=getchar(),last;
	while(ch<'0'||ch>'9') last=ch,ch=getchar();
	while(ch>='0'&&ch<='9') s=(s<<1)+(s<<3)+(ch^48),ch=getchar();
	return last=='-'?-s:s;
}
int num[100];
inline void rt(int x)
{
	if(x<0) putchar('-'),x=-x;;
	int len=0;
	do num[len++]=x%10;while(x/=10);
	while(len--) putchar(num[len]+'0');
}
void solve(){
	int n=rd(),m=rd();
	for(int i=1;i<=n;i++) A[i]=rd();
	for(int i=1;i<=m;i++) B[i]=rd();
	int ta=0,tb=0;
	aa[++ta]=1; bb[++tb]=1;
	for(int i=2;i<=n;i++){
		while(ta>1 && ca(i,aa[ta],aa[ta-1])) ta--;
		aa[++ta]=i;
	}
	for(int i=2;i<=m;i++){
		while(tb>1 && cb(i,bb[tb],bb[tb-1])) tb--;
		bb[++tb]=i;
	}
	int x=1,y=1,ans=0;
	while(x<ta || y<tb){
		//if already taken all a's force take y
		if(x==ta || (y<tb && cab(aa[x],aa[x+1],bb[y],bb[y+1]))){
			int cost=(bb[y+1]-bb[y])*A[aa[x]]; y++;
			ans+=cost;
		}
		else{
			int cost=(aa[x+1]-aa[x])*B[bb[y]]; x++;
			ans+=cost;
		}
	}
	rt(ans);
}
signed main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	clock_t tStart = clock();
	int local=0,multi=0,debug=1,tt=1;
	if(local){
	    freopen("input.txt","r",stdin);
	    freopen("output.txt","w",stdout);
	}	
	if(multi) cin>>tt;
	for(int i=1;i<=tt;i++){
		if(debug && multi && local) cout<<"样例 "<<i<<'\n';
		solve();
 	}
	fprintf(stderr, "\n>> Runtime: %.10fs\n", (double) (clock() - tStart) / CLOCKS_PER_SEC);
}

Compilation message (stderr)

kyoto.cpp: In function 'int main()':
kyoto.cpp:81:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   81 |      freopen("input.txt","r",stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp:82:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   82 |      freopen("output.txt","w",stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp: In function 'long long int rd()':
kyoto.cpp:38:18: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
   38 |  return last=='-'?-s:s;
      |         ~~~~~~~~~^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...