咯咯咯
总结咯咯咯
友好城市
简要题意
河两岸各有n个城市,南北岸城市一一对应。每对城市之间可以修建一条道路,要求任意两条道路不能重叠。求最多能批准修建的道路数量。
思路
将每对城市按南岸坐标从小到大排序。若南岸坐标递增,则要保证道路不重叠,对应的北岸坐标也必须严格递增。因此问题转化为:求排序后北岸坐标序列的最长上升子序列长度。
由于,需用的二分 + 贪心法。
100分
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
struct City {
int s, n;
} c[N];
bool cmp(City x, City y) {
return x.s < y.s;
}
int a[N], dp[N], v[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> c[i].s >> c[i].n;
}
sort(c + 1, c + n + 1, cmp);
for (int i = 1; i <= n; i++) a[i] = c[i].n;
memset(v, 0x3f, sizeof(v));
int cnt = 1;
for (int i = 1; i <= n; i++) {
int l = 1, r = n, ans = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (v[mid] < a[i]) {
l = mid + 1;
} else {
r = mid - 1;
ans = mid;
}
}
v[ans] = min(v[ans], a[i]);
dp[i] = ans;
cnt = max(cnt, ans);
}
cout << cnt << endl;
return 0;
}棍木加工
简要题意
有n根棍木,每根有长度l和宽度w。加工第一根棍木需要1分钟,之后若下一根棍木的长和宽都不超过上一根,则不需要时间,否则需要1分钟。求加工完所有棍木所需的最短准备时间。
思路
将棍木按从大到小排序,长度相同时按宽度从大到小排序。
排序后,若宽度也保持不增,则这些棍木可以连续加工。一旦宽度出现严格上升,就必须增加时间。因此,最短准备时间等于排序后宽度序列的最长严格上升子序列长度。
100分
#include<bits/stdc++.h>
using namespace std;
const int N = 5005;
struct node {
int l, w;
} a[N];
bool cmp(node x, node y) {
if (x.l != y.l) return x.l > y.l;
return x.w > y.w;
}
int c[N];
int v[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].l >> a[i].w;
}
sort(a + 1, a + n + 1, cmp);
for (int i = 1; i <= n; i++) {
c[i] = a[i].w;
}
int len = 0;
for (int i = 1; i <= n; i++) {
int x = c[i];
int pos = lower_bound(v, v + len, x) - v;
if (pos == len) {
v[len++] = x;
} else {
v[pos] = x;
}
}
cout << len << '\n';
return 0;
}导弹拦截
简要题意
给定导弹高度,求拦截系统最多能拦截多少导弹和拦截所有导弹最少需要多少套系统
思路
第一问求的是最长不上升子序列。用二分对于每个高度x,找到第一个<x的位置并替换,若不存在则追加。
第二问根据Dilworth定理,最少系统数等于最长上升子序列的长度,用贪心+二分即可。
100分
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N], n;
int f[N], g[N];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int x;
while (cin >> x) a[++n] = x;
// 第一问:最长不上升子序列长度
int len1 = 0;
for (int i = 1; i <= n; i++) {
int x = a[i];
// 在递减序列 f 中找第一个 < x 的位置
int pos = upper_bound(f, f + len1, x, greater<int>()) - f;
if (pos == len1) f[len1++] = x;
else f[pos] = x;
}
// 第二问:最长上升子序列长度
int len2 = 0;
for (int i = 1; i <= n; i++) {
int x = a[i];
int pos = lower_bound(g, g + len2, x) - g;
if (pos == len2) g[len2++] = x;
else g[pos] = x;
}
cout << len1 << '\n' << len2 << '\n';
return 0;
}Shift vs. CapsLock
简要题意
给定三种操作:1.按下a键,耗时x:指示灯关时输出a,开时输出A。2.同时按下shift+a,耗时y:指示灯关时输出A,开时输出a。3.按下capslock键,耗时z:切换指示灯开关。
求输入字符串s所需的最短时间。
思路
设dp[i][0]表示capslock状态为关,dp[i][1]表示capslock状态为开时前i个字符的最小耗时。
初始dp[0][0]=0,dp[0][1]=Z
对于第i+1个字符c
如果c为A,转移公式为:dp[i+1][st]=min(dp[i][st]+cost(st,c))
如果c为a,转移公式为:dp[i+1][st^1]=min(dp[i][st]+Z+cost(st^1,c))
最终答案即为min(dp[n][0],dp[n][1])。
100分
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 300005;
const int INF = 4e18;
int X, Y, Z;
string S;
int dp[N][2];
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> X >> Y >> Z;
cin >> S;
int n = S.size();
dp[0][0] = 0;
dp[0][1] = Z;
for (int i = 0; i < n; i++) {
dp[i+1][0] = dp[i+1][1] = INF;
for (int st = 0; st <= 1; st++) {
int cost;
if (S[i] == 'A') {
cost = (st == 0 ? Y : X);
} else { // 'a'
cost = (st == 0 ? X : Y);
}
// 不切换状态
dp[i+1][st] = min(dp[i+1][st], dp[i][st] + cost);
// 切换状态
dp[i+1][st ^ 1] = min(dp[i+1][st ^ 1], dp[i][st] + Z + cost);
}
}
cout << min(dp[n][0], dp[n][1]) << '\n';
return 0;
}Consecutive Subsequence
简要题意
给定长度为n的数组,求一个最长的严格递增子序列。输出最长长度和下标方案。
思路
设dp[v]表示以数值v结尾的最长连续递增子序列的长度。
初始
遍历数组时,对于,若存在以结尾的子序列,则可以将当前元素接在后面,即
由于要求的序列是连续递增的,因此起始数值为start=val-len+1。
只需遍历数组,按顺序输出所有值为start,start+1,…的下标即可。
100分
#include<bits/stdc++.h>
using namespace std;
const int N = 200005;
int a[N];
map<int, int> dp;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
int len = 0, val = 0;
for (int i = 1; i <= n; i++) {
int x = a[i];
if (dp.count(x - 1)) {
dp[x] = max(dp[x], dp[x - 1] + 1);
} else {
dp[x] = max(dp[x], 1);
}
if (dp[x] > len) {
len = dp[x];
val = x;
}
}
cout << len << '\n';
int s = val - len + 1;//序列第一个数字
for (int i = 1; i <= n; i++) {
if (a[i] == s) {
cout << i << ' ';
s++;
}
}
cout << '\n';
return 0;
}