940A
转化为求一个有序数列中满足条件的最大子集。
1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(0); 9 int n, d, h, t, meow; 10 cin >> n >> d; 11 int rua[n]; 12 for (int i = 0; i < n; i++) 13 cin >> rua[i]; 14 sort(rua, rua + n); 15 h = t = meow = 0; 16 while (t < n) 17 if (rua[t] - rua[h] <= d){ 18 t += 1; 19 meow = max(meow, t - h); 20 } 21 else 22 h += 1; 23 cout << n - meow; 24 return 0; 25 }
940B
当可被整除时,整除与递减取最优;
否则递减至可被整除。
1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(0); 9 long long n, k, a, b, cost, t; 10 cin >> n >> k >> a >> b; 11 cost = 0; 12 while (n >= k && k != 1){ 13 t = n % k; 14 if (t == 0){ 15 long long temp = n; 16 n /= k; 17 cost += min(b, (temp - n)*a); 18 } 19 else{ 20 n -= t; 21 cost += t*a; 22 } 23 } 24 cout << cost + (n - 1)*a; 25 return 0; 26 }
940C
字符串处理,转化为高精加法,考虑此长彼短两种特殊情况。
1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(0); 9 int n, k, flag = 1; 10 int letter[26] = {0}; 11 int table[26] = {0}; 12 int retable[26] = {0}; 13 int hex = 0; 14 cin >> n >> k; 15 char input[n]; 16 int result[k + 1] = {0}; 17 cin >> input; 18 for (int i = 0; i < n; i++) 19 letter[input[i] - ‘a‘] = 1; 20 for (int i = 0; i < 26; i++) 21 if (letter[i] == 1){ 22 table[i] = hex; 23 retable[hex] = i; 24 hex += 1; 25 } 26 for (int i = 0; i < k; i++) 27 result[i + 1] = i < n? table[input[i] - ‘a‘]: flag = 0; 28 if (flag) 29 result[k] += 1; 30 for (int i = k; i > 0; i--) 31 if (result[i] >= hex){ 32 result[i] -= hex; 33 result[i - 1] += 1; 34 } 35 if (result[0] != 0) 36 cout << char(retable[result[0]] + ‘a‘); 37 for (int i = 1; i < k + 1; i++) 38 cout << char(retable[result[i]] + ‘a‘); 39 return 0; 40 }
940D
0转1约束l,1转0约束r。
1 #include <iostream> 2 #include <algorithm> 3 4 using namespace std; 5 6 int main(){ 7 ios::sync_with_stdio(false); 8 cin.tie(0); 9 int l = -1000000000; 10 int r = 1000000000; 11 int n; 12 cin >> n; 13 int a[n]; 14 char b[n]; 15 for (int i = 0; i < n; i++) 16 cin >> a[i]; 17 cin >> b; 18 for (int i = 4; i < n; i++) 19 if (b[i] == ‘0‘ && b[i - 1] == ‘1‘) 20 for (int j = i - 4; j <= i; j++) 21 r = min(r, a[j] - 1); 22 else if (b[i] == ‘1‘ && b[i - 1] == ‘0‘) 23 for (int j = i - 4; j <= i; j++) 24 l = max(l, a[j] + 1); 25 cout << l << ‘ ‘ << r; 26 return 0; 27 }
940E
贪心:
长度为 c+x ( x<c ) 的序列,等价于一个长度为 c 的序列加上 x 个长度为1的序列。
长度为 2c ( m∈Z ) 的序列,当 2 个移除项处于一段长度为 c 的子序列时,将其看为 2 段长度为 c 的序列处理,得到最优解,其他情况等价。
ST表查找区间最小值。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 int st[100001][17], meow[100001]; 5 long long gugugu[100001], sum[100001]; 6 int n, c; 7 8 void st_init(){ 9 int jmax = floor(log(n)/log(2)); 10 for (int i = 1; i <= n; i++) 11 st[i][0] = meow[i]; 12 for (int j = 1; j <= jmax; j++) 13 for (int i = 1; i <= n; i++) 14 if (i + (1 << j) - 1 <= n) 15 st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]); 16 } 17 18 int st_query(int n, int m){ 19 int k = floor(log(m - n + 1) / log(2)); 20 return min(st[n][k], st[m - (1 << k) + 1][k]); 21 } 22 23 string rua(){ 24 cin >> n >> c; 25 for (int i = 1; i <= n; i++){ 26 cin >> meow[i]; 27 sum[i] = sum[i - 1] + meow[i]; 28 } 29 st_init(); 30 for (int i = 1; i <= n; i++){ 31 long long gu = gugugu[i - 1] + meow[i]; 32 long long bugu = i >= c? gugugu[i - c] + sum[i] - sum[i - c] - st_query(i - c + 1, i): 1000000000000000000; 33 gugugu[i] = min(gu, bugu); 34 } 35 cout << gugugu[n]; 36 return "mole!"; 37 } 38 39 int main(){ 40 rua(); 41 return 0; 42 }
940F
tbc.