2026.10.4 学习总结
cgy20140502 · · 个人记录
考试题目
T1
每次给定一个
请你判断是否存在正整数
T2
首先给定
请你输出从
T3
给出
有多组测试数据。
T4
给出一个初始时大小为
求将序列变为非降序列所需要的最少合并次数。
题目思考
T1
::::success[code]
#include <bits/stdc++.h>
#define int long long
using namespace std;
int k,tot,t,a[10005],b[10005];
int qpow(int x,int y){
int res = 1;
while (y){
if (y & 1) res *= x;
x *= x,y >>= 1;
}
return res;
}
bool prime(int x){
if (x == 0 || x == 1) return 0;
for (int i = 2;i * i <= x;i++)
if (x % i == 0) return 0;
return 1;
}
int check(){
if (k == 1 || prime(k)) return -1;
for (int i = 2;i <= k;i++){
if (k % i == 0){//没加这一句
if (k % i != 0) continue;
tot++;int cnt = 0;
while (k % i == 0){
cnt++;
k /= i;
}
a[tot] = cnt;
b[tot] = i;
if (cnt == 1) t++;
}
}
if (t == tot) return -1;
int ans = 1;
for (int i = 1;i <= tot;i++){
if (a[i] % 2 == 0) ans *= qpow(b[i],a[i]/2);
else ans *= qpow(b[i],(a[i]+1)/2);
}
return ans;
}
int find(){
int l = 0,r = sqrt(k)+10,mid;
while (l+1 < r){
mid = l+r >> 1;
if (mid*mid == k) return mid;
else if (mid*mid < k) l = mid;
else if (mid*mid > k) r = mid;
}
return -1;
}
signed main(){
freopen("2321.in" ,"r" ,stdin);
freopen("2321.out","w",stdout);
cin >> k;
int ans = find();
if (ans != -1) cout << ans;
else cout << check();
return 0;
}
:::: 考场 95 pts。
考场思路
首先分解质因数,如果都是偶数个,就万事大吉直接分裂,如果有奇数个就加倍数,再分裂,如果全是
死因
超时。
解决方法
加一句 if (k % i != 0) continue;。
T2
根据大佬们的题解得出。
AC错解
首先不知道为什么可以推出是什么单谷什么函数,然后可以三分,三分完再用两个树状数组加离散化然后就做完了。
正解
依旧两个树状数组+离散化,但这里不用三分了,这里用一种需要可以满足三个条件的数据结构。
- 插入
- 自动排序
- 按大小分成两半
发现平衡树可以,因此不会,所以改用对顶堆来维护。
T3
根据大佬们的题解得出。
首先可以知道,好吧,我们发现只需要求前
T4
设
枚举
移项得
记
用单调队列优化。
用单调队列存
再把
答案
#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,dp[3000005],a[3000005];
signed main(){
freopen("trans.in" ,"r" ,stdin);
freopen("trans.out","w",stdout);
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for (register int i = 1;i <= n;++i) cin >> a[i];
for (register int i = 1;i <= n;++i) a[i] += a[i-1];
deque <pair<int,int> > q;
q.push_back({0,0});
int x;
for (register int i = 1;i <= n;++i){
int xx;
while (!q.empty() && q.front().second <= a[i]){
xx = q.front().first;
q.pop_front();
}
x = i - xx;
dp[i] = dp[xx]+1;
while (!q.empty() && q.back().second >= a[i]+a[i]-a[xx]) q.pop_back();
q.push_back({i,a[i]+a[i]-a[xx]});
}
cout << n-dp[n];
return 0;
}
::::