A
B
C
给你一个长度为N的01串 你可以翻转一次任意【L,R】的区间
问你最长的不递减序列为多少长
处理出1的前缀和 和2的后缀和
然后N^2 DP 处理出 【L,R】区间的最长不递增序列
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!! ") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; get min const double eps = 1.0e-10; const double EPS = 1.0e-4; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; const int turn[4][2] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}}; //priority_queue<int, vector<int>, less<int>> que; //next_permutation const int MAXN = 2005; int a[2005]; int pre[2005]; int suf[2005]; int dp[2005][2005][3]; int ans = 0; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + (a[i] == 1); } for (int i = n; i >= 1; i--) { suf[i] = suf[i + 1] + (a[i] == 2); } for (int i = 1; i <= n; i++) { ans = max(pre[i] + suf[i], ans); } //cout<<ans<<endl; for (int i = 1; i <= n; i++) { for (int j = i; j <= n; j++) { dp[i][j][2] = dp[i][j - 1][2] + (a[j] == 2); dp[i][j][1] = max(dp[i][j - 1][2], dp[i][j - 1][1]) + (a[j] == 1); ans = max(ans, max(dp[i][j][2], dp[i][j][1]) + pre[i - 1] + suf[j + 1]); } } cout << ans << endl; }
D
找规律
考虑构造q(x).先让常数项小于k,也就是让相乘后的常数项+p小于k.
q(x)的常数项就是p / (-k).这样会产生一次项,那么继续消一次项.直到最后的p = 0.
#include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!! ") #define pb push_back #define inf 1e9 //std::ios::sync_with_stdio(false); using namespace std; //priority_queue<int,vector<int>,greater<int>> que; get min const double eps = 1.0e-10; const double EPS = 1.0e-4; typedef pair<int, int> pairint; typedef long long ll; typedef unsigned long long ull; //const int maxn = 3e5 + 10; const int turn[4][2] = {{1, 0}, { -1, 0}, {0, 1}, {0, -1}}; //priority_queue<int, vector<int>, less<int>> que; //next_permutation ll Mod = 1000000007; int cnt; int ans[1000005]; int main() { ll p; ll k; cin >> p >> k; ll flag = 0; while (p != 0) { ans[++cnt] = p % k; p = p / k * (-1); if (flag) { p += flag; flag = 0; } if (p < 0) { flag += (k - p) / k; p += flag * k;; } } cout << cnt << endl; for (int i = 1; i <= cnt; i++) { cout << ans[i] << " "; } cout << endl; return 0; }