Table of contents
-
- Test Question A: Paper Cutter
- Question B: Rat Control Pioneer
- Question C: Sum
- Question D: XOR
- Test Question E: A Beetle Climbing a Tree
- Question F: Frogger across the river
- Question G: The longest non-decreasing subsequence
- Question H: Scanning Game
- Question I: Splitting Numbers
- Question J: Derivation Part and
Test Question A: Paper Cutter
The answer is n ∗ m − 1 + 4 n*m-1+4n∗m−1+4
443
Question B: Rat Control Pioneer
LLLV
Question C: Sum
Estimated score 100%
Idea: maintain a prefix sum sumJust s u m .
Total time complexity O ( n ) O(n)O ( n )
Reference Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
int a[N];
void solve()
{
int n;
scanf("%d", &n);
long long ans = 0, sum = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
ans += sum * a[i];
sum += a[i];
}
cout << ans << endl;
}
int main()
{
solve();
return 0;
}
Question D: XOR
Estimated score 100%
for each position iii ,设 y = a [ i ] y = a[i] y=a[i] ^ x x x , find the nearest oneyySubscriptidx idx of yi d x , writingb [ i ] b[i]b [ i ] . Then use the line segment tree to maintainbbThe interval maximum value of the b arraymaxx maxxma xx ifmaxx >= L maxx >= Lmaxx>=L , thenyes yesyes。
Total time complexity O ( nlogn ) O(nlogn)O(nlogn)
Reference Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int a[N], b[N];
struct node
{
int l, r, val, maxx;
} tr[N * 4];
void pushup(int k) {
tr[k].maxx = max(tr[k * 2].maxx, tr[k * 2 + 1].maxx); }
void build(int k, int l, int r)
{
tr[k].l = l;
tr[k].r = r;
if (tr[k].l == tr[k].r)
{
tr[k].val = tr[k].maxx = b[l];
return;
}
int mid = l + r >> 1;
build(k * 2, l, mid);
build(k * 2 + 1, mid + 1, r);
pushup(k);
}
int query(int k, int l, int r)
{
if (tr[k].l == l && tr[k].r == r)
return tr[k].maxx;
int mid = tr[k].l + tr[k].r >> 1;
if (r <= mid)
return query(k * 2, l, r);
else if (l > mid)
return query(k * 2 + 1, l, r);
else
return max(query(k * 2, l, mid), query(k * 2 + 1, mid + 1, r));
}
void solve()
{
int n, m, x;
scanf("%d %d %d", &n, &m, &x);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
map<int, int> last;
for (int i = 1; i <= n; i++)
{
int y = a[i] ^ x;
if (!last.count(y))
b[i] = -1;
else
b[i] = last[y];
last[a[i]] = i;
}
build(1, 1, n);
while (m--)
{
int l, r;
scanf("%d %d", &l, &r);
int maxx_pos = query(1, l, r);
puts(maxx_pos >= l ? "yes" : "no");
}
}
int main()
{
solve();
return 0;
}
Test Question E: A Beetle Climbing a Tree
Not yet.
Question F: Frogger across the river
2022.4.25 UPDATE: One sentence is missing, it has been corrected
Estimated score 100%
Two-point classic question adaptation question, two points at a glance, check is a bit difficult to write.
Check ideas:
Can make dp [ i ] dp[i]d p [ i ] means jump to theiiThe maximum number of i stones.
put the lastmid midmi dp of d stones[ i ] dp[i]d p [ i ] add up,sum >= 2 ∗ x sum>= 2*xsum>=2∗x means legal.
Total time complexity O ( nlogn ) O(nlogn)O(nlogn)
Reference Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int a[N];
long long dp[N];
// dp[i]表示最多能跳到位置i dp[i]次
int n, x;
bool check(int mid)
{
long long sum = 0;
for (int i = 1; i <= mid; i++) //前mid个都可以
dp[i] = a[i];
int l = 1;
for (int i = mid + 1; i <= n; i++)
{
while (i - l > mid)//控制跳跃距离。
++l;
dp[i] = 0;
while (dp[i] < a[i] && l < i)
{
if (dp[l] + dp[i] <= a[i])
{
dp[i] += dp[l];
dp[l] = 0;
++l;
}
else
{
dp[l] -= a[i] - dp[i];
dp[i] = a[i];
}
}
}
long long ans = 0;
int L = n - mid + 1;
for (int i = L; i <= n; i++)
ans += dp[i];
return ans >= 2 * x;
}
void solve()
{
scanf("%d %d", &n, &x);
--n;
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
int l = 1, r = n, ans = n + 1;
while (l <= r)
{
int mid = l + r >> 1;
if (check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
printf("%d\n", ans);
}
int main()
{
solve();
return 0;
}
Question G: The longest non-decreasing subsequence
Expected score 10% ~ 30%
Violent thoughts:
enum modification position, bisection O(nlogn) O(nlogn)O ( n l o g n ) find the longest non-decreasing subsequence, (skip the length of the middle section iskkarray of k )
Total time complexity O ( n 2 logn ) O(n^2logn)O ( n2logn)
Reference Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int a[N], dp[N];
int n, k;
int LIS(int L, int R) //二分求LIS
{
int len = 0;
dp[len] = -0x3f3f3f3f;
for (int i = 1; i <= n; i++)
{
if (i == L) //跳过k个
{
i = R;
continue;
}
if (a[i] >= dp[len])
{
++len;
dp[len] = a[i];
}
int p = upper_bound(dp + 1, dp + 1 + len, a[i]) - dp;
dp[p] = a[i];
}
return len;
}
void solve()
{
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
if (n - 1 <= k)
//修改k个必然能全部满足
{
printf("%d\n", n);
return;
}
int ans = k + 1;
for (int i = 1; i + k - 1 <= n; i++)
ans = max(ans, k + LIS(i, i + k - 1));
printf("%d\n", ans);
}
int main()
{
solve();
return 0;
}
Question H: Scanning Game
Expected score 10% ~ 30%
Violent thoughts:
Sort by polar angle. After each violent scan, loop until no items are scanned.
Total time complexity O ( n 2 ) O(n^2)O ( n2)
Reference Code:
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
const int INF = 0x3f3f3f3f;
struct node
{
long long x, y, z, id;
double theta;
bool operator<(const node &tmp) const {
return theta < tmp.theta; }
};
vector<node> vec;
int out[N];
node tmp;
void solve()
{
int n;
long long L;
scanf("%d %lld", &n, &L);
for (int i = 1; i <= n; i++)
{
out[i] = -1;
tmp.id = i;
scanf("%lld %lld %lld", &tmp.x, &tmp.y, &tmp.z);
tmp.theta = atan2(tmp.y, tmp.x);
vec.push_back(tmp);
}
sort(vec.begin(), vec.end());
int len = (int)vec.size();
int p = -1;
for (int i = len - 1; i >= 0; i--)
{
if (vec[i].x >= 0)
{
p = i;
break;
}
}
if (p == -1)
{
for (int i = len - 1; i >= 0; i--)
{
if (vec[i].y <= 0)
{
p = i;
break;
}
}
}
if (p == -1)
p = (int)vec.size() - 1;
vector<node> tmp = vec;
vec.clear();
for (int i = p; i >= 0; i--)
vec.push_back(tmp[i]);
for (int i = (int)tmp.size() - 1; i > p; i--)
vec.push_back(tmp[i]);
int rk = 0, sum = 0;
node pre;
pre.theta = INF;
while (1)
{
vector<node> now;
int m = vec.size();
for (int i = 0; i < m; i++)
if (vec[i].x != INF)
now.push_back(vec[i]);
m = now.size();
bool flag = 0;
for (int i = 0; i < m; i++)
{
if (L * L >= now[i].x * now[i].x + now[i].y * now[i].y)
{
L += now[i].z;
now[i].x = INF; //标记
flag = 1;
if (fabs(now[i].theta - pre.theta) <= 1e-6)
out[now[i].id] = rk;
else
{
rk = sum + 1;
out[now[i].id] = rk;
}
sum++;
pre = now[i];
}
}
if (!flag)
break;
vec = now;
}
for (int i = 1; i <= n; i++)
printf("%d%c", out[i], i == n ? '\n' : ' ');
}
int main()
{
solve();
return 0;
}
Question I: Splitting Numbers
Expected score 10%
Violent idea: screen out all prime factors, there cannot be a prime factor that appears only once, and there are at most two prime factors that appear an odd number of times (even times only need to be equally distributed to x 1 x_1x1和y 1 y_1y1)
Total time complexity O ( tsqrt ( n ) ) O(tsqrt(n))O ( t s q r t ( n ))
Reference Code:
#include <bits/stdc++.h>
using namespace std;
bool check(long long x)
{
map<int, int> mp;
for (int i = 2; i * i <= x; i++)
{
if (x % i == 0)
while (x % i == 0)
{
x /= i;
mp[i]++;
}
}
if (x >= 2)
mp[x]++;
int odd = 0, flag = 0;
for (auto it : mp)
{
if (it.second == 1)
return 0;
if (it.second & 1)
odd++;
if (it.second >= 2)
flag = 1;
}
return odd <= 2;
}
void solve()
{
int t;
scanf("%d", &t);
while (t--)
{
long long x;
scanf("%lld", &x);
puts(check(x) ? "yes" : "no");
}
}
int main()
{
solve();
return 0;
}
Question J: Derivation Part and
Not yet.