day15
clki
·
·
个人记录
得分情况与用时
::::success[查看]
$T2 \ :$ $100$分,二分
$T3 \ :$ $0$分,大模拟没做出来
$T4 \ :$ $5$分,$e$ ... ,代码就是写的谁也不懂,奇奇怪怪
::::
### [CSP-J 2022] 乘方
::::success[思路]
### 题意:
给定两个正整数 $a,b$,计算 $a^b$($b$ 个 $a$ 相乘)。
- 如果 $a^b \le 10^9$,输出 $a^b$;
- 如果 $a^b > 10^9$,输出 $-1$。
数据范围:$1\le a,b \le 10^9
思路:
直接循环一遍一遍乘,但是每乘一次就检查有没有超过 10^9。
| 一旦超过,立刻停止,直接输出 -1 |
| - [x] 完成思路 |
| ::::success[你们最爱看的Code:] |
| ```cpp |
| #include<bits/stdc++.h> |
| using namespace std; |
| #define int long long |
| int a,b,res = 1; |
| signed main() { |
| ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); |
| cin >> a >> b; |
| if(a == 1){ |
| cout << 1; |
| return 0; |
| } |
| for(int i = 1; i <= b; i ++ ){ |
| res *= a; |
| if(res > 1e9){ |
| cout << -1; |
| return 0; |
| } |
| } |
| cout << res; |
| return 0; |
| } |
::::
### [CSP-J 2022] 解密
::::success[思路]
#### 思路:
由 $(p+q=m)$,得 $(q=m-p)$,代入 $(pq=n)$:
$(p(m-p)=n)$
构造函数 $(f(p)=p(m-p))$。
二次函数 $(f(p)=-p^2+mp)$ 对称轴为 $(p=\frac{m}{2})$,在区间 $([1,\frac{m}{2}])$ 单调递增,可以二分。
::::
- [x] 完成思路
::::success[你们最爱看的Code:]
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 1e6 + 10;
int t;
void solve(){
int n,e,d;
cin >> n >> e >> d;
int m = n - e * d + 2;
int l = 0,r = m / 2 + 1,p;
while(l + 1 < r){
p = (l + r) / 2;
int q = m - p;
if(q * p >= n)r = p;
else l = p;
}
if(r * (m - r) == n)cout << r << " " << m - r << '\n';
else cout << "NO" << '\n';
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> t;
while(t -- ){
solve();
}
return 0;
}
::::
[CSP-J 2022] 上升点列
::::success[思路]
题意:
给定平面上 n 个点,你最多可以额外添加 k 个点。
选出一个点序列,满足:
序列里的点横坐标严格递增;
同时纵坐标严格递增;
(也就是从左下往右上走,只能向右上方)
序列可以包含原本的点,也包含我们自己补的点。
求这个序列最长能有多长。
两个原有点 q\to i,曼哈顿距离 d=|x_i‑x_q|+|y_i‑y_q|
两个点之间,需要补上 \boldsymbol{d‑1} 个点,才能连成连续上升序列。
思路:
动态规划
先把所有点按 x 从小到大排序;x 相同按 y 从小到大。保证遍历的时候 x 是递增的。
状态定义:
### 初始化:
只选自己这个点,再补上 $j$ 个新增点:$f[i][j]=j+1$。
(自己 $1$ 个点,再加 $j$ 个补的点,总长度 $j+1$)
### 转移:
枚举前面一个原始点$q\;(q<i)
- 必须满足 a[q].y \le a[i].y,保证纵坐标不下降;
- 两点之间需要补的点数:d = getd(i,q)-1
- 如果需要补 d 个点,而当前剩余补点次数 j \ge d,就可以从 q 转移过来:
f[i][j]=\max(f[i][j],\ f[q][j-d]+d+1)
$+d+1$:中间补 $d$ 个点,再算上 $i$ 这个原始点。
全程记录所有 $f[i][j]$ 的最大值作为答案。
::::
- [x] 完成思路
::::success[你们最爱看的Code:]
```cpp
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
const int N = 505 + 10;
//存储点的坐标
struct P {
int x,y;
} a[N];
int n,k;
// f[i][j]:以第i个原始点为结尾,已经使用j个补点,最长序列长度
int f[N][101];
//排序规则:先按x升序,x相同按y升序
bool cmp(P a1, P a2) {
if(a1.x != a2.x )return a1.x < a2.x;
return a1.y < a2.y;
}
//求两点曼哈顿距离
int getd(P a1, P a2) {
return abs(a1.x - a2.x) + abs(a1.y - a2.y);
}
signed main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n ; i ++ ) {
cin >> a[i].x >> a[i].y;
}
sort(a + 1,a + 1 + n,cmp); //把点按横坐标从小到大排序
int ans = 0;
for(int i = 1; i <= n ; i ++ ){ //枚举序列最后一个原始点i
for(int j = 0; j <= k; j ++ ){ //枚举一共用了j个补点
//初始化:只保留i点,额外补j个点,长度 j+1
f[i][j] = j + 1;
//枚举i前面的原始点q,尝试从q转移到i
for(int q = 1; q < i; q ++ ){
if(a[q].y > a[i].y) continue; //q的y比i大,无法构成上升序列
int d = getd(a[i],a[q]) - 1; //q到i中间需要补d个点
if(d > j) continue; //需要补的点超过已使用名额,不能转移
// j-d:前面q已经用掉j‑d个补点;d+1:d个补点+i这个点
f[i][j] = max(f[i][j], f[q][j - d] + d + 1);
}
ans = max(ans, f[i][j]); //更新全局最大答案
}
}
cout << ans;
return 0;
}
```
::::
### qp(依旧第一)