This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int H,W;
int A[50000],B[50000];
map<pii,int> dp;
int findAns(int x,int y) {
if ((x < 0) || (x >= W) || (y < 0) || (y >= H)) return -1;
else if (dp.count(mp(x,y))) return dp[mp(x,y)];
int i,ans;
if (A[y] < B[x]) {
for (i = y; i >= 0; i--) {
if (A[i] > B[x]) break;
}
ans = findAns(x,i)+abs(i-y);
for (i = y; i < H; i++) {
if (A[i] > B[x]) break;
}
ans = max(ans,findAns(x,i)+abs(i-y));
}
else {
for (i = x; i >= 0; i--) {
if (B[i] > A[y]) break;
}
ans = findAns(i,y)+abs(i-x);
for (i = x; i < W; i++) {
if (B[i] > A[y]) break;
}
ans = max(ans,findAns(i,y)+abs(i-x));
}
dp[mp(x,y)] = ans;
return ans;
}
int main() {
int i,j;
int Q,S,T;
scanf("%d %d %d",&H,&W,&Q);
for (i = 0; i < H; i++) scanf("%d",&A[i]);
for (i = 0; i < W; i++) scanf("%d",&B[i]);
for (i = 0; i < Q; i++) {
scanf("%d %d",&S,&T);
S--,T--;
int ans = 0;
for (j = S-1; j >= 0; j--) {
if (A[j] > B[T]) break;
}
ans = max(ans,findAns(T,j)+abs(j-S));
for (j = S+1; j < H; j++) {
if (A[j] > B[T]) break;
}
ans = max(ans,findAns(T,j)+abs(j-S));
for (j = T-1; j >= 0; j--) {
if (B[j] > A[S]) break;
}
ans = max(ans,findAns(j,S)+abs(j-T));
for (j = T+1; j < W; j++) {
if (B[j] > A[S]) break;
}
ans = max(ans,findAns(j,S)+abs(j-T));
printf("%d\n",ans);
}
return 0;
}
Compilation message (stderr)
abduction2.cpp: In function 'int main()':
abduction2.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%d %d %d",&H,&W,&Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
abduction2.cpp:93:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | for (i = 0; i < H; i++) scanf("%d",&A[i]);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:94:34: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | for (i = 0; i < W; i++) scanf("%d",&B[i]);
| ~~~~~^~~~~~~~~~~~
abduction2.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%d %d",&S,&T);
| ~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |