#include <bits/stdc++.h>
using namespace std;
ofstream fout("date.out");
mt19937 rng(chrono::steady_clock().now().time_since_epoch().count());
typedef long long ll;
const ll INF=1e17;
ll n,m;
ll A[100005],B[100005];
vector<vector<ll>> dp;
ll from[10005][10005];
string dig(ll x)
{
string s;
while(x!=0)
{
s.push_back(char('0'+x%10));
x/=10;
}
while(s.size()<4)
s.push_back('0');
reverse(s.begin(),s.end());
return s;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
vector<ll> lins,cols;
ll minA=INF,minB=INF;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
//A[i]=A[i-1]+rng()%100+1;
cin>>A[i];
minA=min(minA,A[i]);
}
for(int i=1;i<=m;i++)
{
//B[i]=B[i-1]+rng()%100+1;
cin>>B[i];
minB=min(minB,B[i]);
}
ll minim=INF;
for(int i=n;i>=1;i--)
{
if(A[i]<minim)
{
lins.push_back(i);
minim=min(minim,A[i]);
}
}
minim=INF;
for(int i=m;i>=1;i--)
{
if(B[i]<minim)
{
cols.push_back(i);
minim=min(minim,B[i]);
}
}
minim=INF;
for(int i=1;i<=n;i++)
if(A[i]<minim)
{
lins.push_back(i);
minim=A[i];
}
minim=INF;
for(int i=1;i<=m;i++)
if(B[i]<minim)
{
cols.push_back(i);
minim=B[i];
}
sort(lins.begin(),lins.end());
sort(cols.begin(),cols.end());
lins.erase(unique(lins.begin(),lins.end()),lins.end());
cols.erase(unique(cols.begin(),cols.end()),cols.end());
dp.clear();
dp.resize(lins.size());
for(int i=0;i<lins.size();i++)
dp[i].resize(cols.size());
for(int i=0;i<lins.size();i++)
for(int j=0;j<cols.size();j++)
if(i!=0||j!=0)
{
dp[i][j]=INF;
if(i>0&&dp[i-1][j]+B[cols[j]]*(lins[i]-lins[i-1])<dp[i][j])
{
dp[i][j]=min(dp[i][j],dp[i-1][j]+B[cols[j]]*(lins[i]-lins[i-1]));
from[i][j]=1;
}
if(j>0&&dp[i][j-1]+A[lins[i]]*(cols[j]-cols[j-1])<dp[i][j])
{
dp[i][j]=min(dp[i][j],dp[i][j-1]+A[lins[i]]*(cols[j]-cols[j-1]));
from[i][j]=2;
}
}
ll ans=dp[(int)lins.size()-1][(int)cols.size()-1];
cout<<ans;
int i=(int)lins.size()-1;
int j=(int)cols.size()-1;
int prv=0;
int changes=0;
while(i!=0||j!=0)
{
if(from[i][j]!=prv&&prv!=0)
changes++;
prv=from[i][j];
if(from[i][j]==1)
i--;
else
j--;
}
assert(changes<=100);
return 0;
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:82:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i=0;i<lins.size();i++)
| ~^~~~~~~~~~~~
kyoto.cpp:84:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i=0;i<lins.size();i++)
| ~^~~~~~~~~~~~
kyoto.cpp:85:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for(int j=0;j<cols.size();j++)
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4600 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6636 KB |
Output is correct |
13 |
Runtime error |
112 ms |
172764 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
5 ms |
27476 KB |
Output is correct |
4 |
Correct |
7 ms |
28332 KB |
Output is correct |
5 |
Correct |
7 ms |
15196 KB |
Output is correct |
6 |
Correct |
3 ms |
4956 KB |
Output is correct |
7 |
Correct |
11 ms |
6236 KB |
Output is correct |
8 |
Correct |
11 ms |
6252 KB |
Output is correct |
9 |
Correct |
14 ms |
32052 KB |
Output is correct |
10 |
Correct |
14 ms |
33960 KB |
Output is correct |
11 |
Runtime error |
211 ms |
308560 KB |
Execution killed with signal 6 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4600 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
2 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6636 KB |
Output is correct |
13 |
Runtime error |
112 ms |
172764 KB |
Execution killed with signal 6 |
14 |
Halted |
0 ms |
0 KB |
- |