求把数组分成m段的最大和,也可以相邻
#include
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int m, n;
while(cin >> m >> n) {
vector<int> a(n + 1);
for(int i=1;i<=n;i++) {
cin >> a[i];
}
vector<vector<int> > dp(n + 1, vector<int>(m + 1));
vector<int> dp0(n + 1);
for(int j=1;j<=m;j++) {
int mx = INT_MIN;
for(int i=j;i<=n;i++) {
dp[i][j] = max(dp[i - 1][j] + a[i], dp0[i - 1] + a[i]);
dp0[i - 1] = mx;
mx = max(mx, dp[i][j]);
}
}
cout << dp[n][m] << '\n';
}
return 0;
}
#include
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int m, n;
while(cin >> m >> n) {
vector<int> a(n + 1);
for(int i=1;i<=n;i++) {
cin >> a[i];
}
vector<int> dp(n + 1);
vector<int> dp0(n + 1);
int mx;
for(int j=1;j<=m;j++) {
mx = INT_MIN;
for(int i=j;i<=n;i++) {
dp[i] = max(dp[i - 1] + a[i], dp0[i - 1] + a[i]);
dp0[i - 1] = mx;
mx = max(mx, dp[i]);
}
}
cout << mx << '\n';
}
return 0;
}
#include
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
while(cin >> n){
int ans = -1;
int cnt = 0;
for(int i=0;i<n;i++){
int x;
cin >> x;
if(cnt == 0) ans = x;
if(ans == x) cnt += 1;
else cnt -= 1;
}
cout << ans << '\n';
}
return 0;
}
大致原理就是如果一个数在一组数里面出现次数多于一半,那么这个数一定是唯一的,那么我们记录一下最多的数字出现的次数,然后用其他的数字来减小 c n t cnt cnt,这样最后得到的数字就是那个唯一众数
这有一个模拟这一过程的网页
https://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html
#include
using namespace std;
typedef long long ll;
struct st{
int x, y, z;
bool operator < (const st &B)const{
return x > B.x || (x == B.x && y > B.y);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int kase = 1;
int n;
while(cin >> n && n){
vector<st> a;
for(int i=0;i<n;i++){
int x, y, z;
cin >> x >> y >> z;
a.push_back({x, y, z});
a.push_back({x, z, y});
a.push_back({y, x, z});
a.push_back({y, z, x});
a.push_back({z, x, y});
a.push_back({z, y, x});
}sort(a.begin(), a.end());
int m = (int)a.size();
vector<int> dp(m);
int ans = 0;
for(int i=0;i<m;i++){
dp[i] = a[i].z;
for(int j=0;j<i;j++){
if(a[j].x > a[i].x && a[j].y > a[i].y) dp[i] = max(dp[i], dp[j] + a[i].z);
}
ans = max(ans, dp[i]);
}
cout << "Case " << kase++ << ": maximum height = " << ans << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
struct st{
string name;
int Deadline;
int need;
};
struct answer{
int fa;// 前一个状态是谁
int score;// 当前扣分
int now;// 做完的时间
int who;// 现在是哪个作业
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--){
int n;
cin >> n;
vector<st> a(n);
for(int i=0;i<n;i++){
cin >> a[i].name >> a[i].Deadline >> a[i].need;
}
vector<answer> dp(1 << n, {-1, 0x3f3f3f3f, 0, 0});
dp[0].score = 0;
int state = (1 << n) - 1;
for(int s=1;s<=state;s++){
for(int j=n-1;j>=0;j--){
if((s >> j) & 1){
int tmp = (s ^ (1 << j));
int score = dp[tmp].now + a[j].need - a[j].Deadline;// 减少的分数
if(score < 0) score = 0;// 按时完成了, 不扣分
if(dp[s].score > dp[tmp].score + score){
dp[s].who = j;
dp[s].fa = tmp;
dp[s].score = dp[tmp].score + score;
dp[s].now = dp[tmp].now + a[j].need;
}
}
}
}
cout << dp[(1 << n) - 1].score << '\n';
vector<string> ans(n);
int j = n - 1;
while(dp[state].fa != -1){
ans[j] = a[dp[state].who].name;
state = dp[state].fa;
j -= 1;
}
for(int i=0;i<n;i++){
cout << ans[i] << '\n';
}
}
return 0;
}
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
while(cin >> n && n){
vector<int> a(n + 1);
vector<ll> dp(n + 1);
for(int i=1;i<=n;i++){
cin >> a[i];
dp[i] = a[i];
for(int j=1;j<i;j++){
if(a[i] > a[j]){
dp[i] = max(dp[j] + a[i], dp[i]);
}
}
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--){
int e, f;
cin >> e >> f;
int n;
cin >> n;
vector<pair<int, int> > a(n + 1);
for(int i=1;i<=n;i++){
cin >> a[i].first >> a[i].second;
}
vector<int> dp(f - e + 1, 0x3f3f3f3f);
dp[0] = 0;
for(int i=1;i<=n;i++){
for(int j=a[i].second;j<=f-e;j++){
dp[j] = min(dp[j - a[i].second] + a[i].first, dp[j]);
}
}
if(dp[f - e] == 0x3f3f3f3f){
cout << "This is impossible.\n";
}else{
cout << "The minimum amount of money in the piggy-bank is " << dp[f - e] << ".\n";
}
}
return 0;
}
#include
using namespace std;
typedef long long ll;
const int N = 1e5;
int x[N + 10][11];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
while(cin >> n && n){
int mx = -1;
for(int i=0;i<n;i++){
int y, t;
cin >> y >> t;
mx = max(mx, t);
x[t][y] += 1;
}
vector<vector<int> > dp(mx + 1, vector<int>(11));
for(int i=mx-1;i>=0;i--){
for(int j=0;j<11;j++){
dp[i][j] = dp[i + 1][j] + x[i + 1][j];
if(j + 1 < 11) dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + x[i + 1][j + 1]);
if(j - 1 >= 0) dp[i][j] = max(dp[i][j], dp[i + 1][j - 1] + x[i + 1][j - 1]);
}
}
for(int i=1;i<=mx;i++){
memset(x[i], 0, sizeof x[i]);
}
cout << dp[0][5] << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
const int INF= 0x3f3f3f3f;
const int N = 2000;
int dp[N + 5][N + 5][2];
int a[N + 5];
int b[N + 5];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--){
int k;
memset(dp, 0x3f, sizeof dp);
cin >> k;
for(int i=1;i<=k;i++){
cin >> a[i];
}
for(int i=2;i<=k;i++){
cin >> b[i];
}
for(int i=1;i<=k;i++){
for(int j=0;j<k;j++){
if(j > 0){
dp[i][j][0] = min(dp[i - 1][j][0] + a[i], dp[i - 1][j][1] + a[i]);
if(i > 1) dp[i][j][1] = min(dp[i - 1][j - 1][0] - a[i - 1] + b[i], dp[i - 1][j][1] + a[i]);
}else{
dp[i][j][0] = (dp[i - 1][j][0] == INF ? 0 : dp[i - 1][j][0]) + a[i];
}
}
}
int ans = INF;
for(int j=0;j<k;j++){
for(int l=0;l<2;l++){
ans = min(ans, dp[k][j][l]);
}
}
if(ans == INF) ans = 0;
int second = ans % 60;
ans /= 60;
int minute = ans % 60;
ans /= 60;
int hour = ans;
cout << setw(2) << setfill('0') << 8 + hour << ':' << setw(2) << setfill('0')
<< minute << ':' << setw(2) << setfill('0') << second << ' ' << (8 + hour >= 12 ? "pm" : "am") << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
const int INF= 0x3f3f3f3f;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--){
int k;
cin >> k;
vector<int> a(k + 1);
for(int i=1;i<=k;i++){
cin >> a[i];
}
vector<int> b(k + 1);
for(int i=2;i<=k;i++){
cin >> b[i];
}
vector<int> dp(k + 1);
for(int i=1;i<=k;i++){
dp[i] = dp[i - 1] + a[i];
if(i >= 2) dp[i] = min(dp[i - 2] + b[i], dp[i]);
}
int ans = dp[k];
int second = ans % 60;
ans /= 60;
int minute = ans % 60;
ans /= 60;
int hour = ans;
cout << setw(2) << setfill('0') << 8 + hour << ':' << setw(2) << setfill('0')
<< minute << ':' << setw(2) << setfill('0') << second << ' ' << (8 + hour >= 12 ? "pm" : "am") << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
while(cin >> n){
vector<int> a(n + 1);
for(int i=1;i<=n;i++){
cin >> a[i];
}
vector<int> dp(n + 1, 1);
int ans = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
if(a[j] < a[i]){
dp[i] = max(dp[j] + 1, dp[i]);
}
}
ans = max(ans, dp[i]);
}
cout << ans << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
while(cin >> n){
vector<int> a(n);
for(int i=0;i<n;i++){
cin >> a[i];
}
vector<int> LIS;
for(int i=0;i<n;i++){
int p = upper_bound(LIS.begin(), LIS.end(), a[i]) - LIS.begin();
if(p == (int)LIS.size()){
LIS.push_back(a[i]);
}else{
LIS[p] = a[i];
}
}
cout << (int)LIS.size() << '\n';
}
return 0;
}
#include
using namespace std;
typedef long long ll;
struct st{
int w, s, fa;
int sz;
};
struct node{
int w, s, id;
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int w, s;
vector<node> a(1);
int n = 1;
while(cin >> w >> s){
a.push_back(node{w, s, n});
n += 1;
}
sort(a.begin() + 1, a.end(), [&](node x, node y){
return x.s > y.s;
});
n -= 1;
vector<st> dp(n + 1);
for(int i=1;i<=n;i++){
dp[i].fa = i;
dp[i].w = a[i].w;
dp[i].s = a[i].s;
dp[i].sz = 1;
for(int j=1;j<i;j++){
if(dp[i].sz < dp[j].sz + 1 && a[j].w < a[i].w && a[j].s > a[i].s){
dp[i].fa = j;
dp[i].w = a[j].w;
dp[i].s = a[j].s;
dp[i].sz = dp[j].sz + 1;
}
}
}
int mx = -1;
int now = 0;
for(int i=1;i<=n;i++){
if(mx < dp[i].sz){
mx = dp[i].sz;
now = i;
}
}
function<void(int)> solve = [&](int x){
if(x == dp[x].fa){
cout << a[x].id << '\n';
return;
}
solve(dp[x].fa);
cout << a[x].id << '\n';
};
cout << dp[now].sz << '\n';
solve(now);
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s1, s2;
while(cin >> s1 >> s2){
int n = s1.length();
int m = s2.length();
vector<vector<int> > dp(n + 1, vector<int>(m + 1));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(s1[i - 1] == s2[j - 1]){
dp[i][j] = dp[i - 1][j - 1] + 1;
}else{
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
}
}
cout << dp[n][m] << '\n';
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
struct st{
int l, r, h;
bool operator < (const st &B)const{
return h > B.h;
}
};
const int INF = 0x3f3f3f3f;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while(t--){
int n, x, y, mx;
cin >> n >> x >> y >> mx;
vector<st> a(n + 2);
a[0].l = a[0].r = x; a[0].h = y;
a[1].l = -20000; a[1].r = 20000; a[1].h = 0;
vector<vector<int> > dp(n + 2, vector<int>(2));
for(int i=2;i<=n+1;i++){
cin >> a[i].l >> a[i].r >> a[i].h;
}sort(a.begin(), a.end());
for(int i=n;i>=0;i--){// 从下往上
int k = i + 1;// 下面的某个
bool ok = false;// 能不能通过
// 左边
while(k < n + 1 && mx >= a[i].h - a[k].h){
if(a[i].l >= a[k].l && a[i].l <= a[k].r){
dp[i][0] = a[i].h - a[k].h + min(dp[k][0] + a[i].l - a[k].l, dp[k][1] + a[k].r - a[i].l);
ok = true;
break;
}
k += 1;
}
if(!ok){
if(a[i].h - a[k].h > mx){
dp[i][0] = INF;
}else{
dp[i][0] = a[i].h - a[k].h;
}
}
// 右边
ok = false;
k = i + 1;
while(k < n + 1 && mx >= a[i].h - a[k].h){
if(a[i].r >= a[k].l && a[i].r <= a[k].r){
dp[i][1] = a[i].h - a[k].h + min(dp[k][0] + a[i].r - a[k].l, dp[k][1] + a[k].r - a[i].r);
ok = true;
break;
}
k += 1;
}
if(!ok){
if(a[i].h - a[k].h > mx){
dp[i][1] = INF;
}else{
dp[i][1] = a[i].h - a[k].h;
}
}
}
cout << min(dp[0][0], dp[0][1]) << '\n';
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
vector<int> dp(n + 1, 1);
for(int i=1;i<=n;i++) cin >> a[i];
int ans = 0;
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
if(a[i] > a[j]){
dp[i] = max(dp[j] + 1, dp[i]);
}
}
ans = max(ans, dp[i]);
}
cout << ans << '\n';
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
for(int i=1;i<=n;i++){
cin >> a[i];
}
vector<vector<int> > dp(n + 1, vector<int>(n + 1));
for(int i=0;i<=n;i++){
for(int j=0;i+j<=n;j++){
int len = i + j;
if(i > 0) dp[i][j] = max(dp[i][j], dp[i - 1][j] + len * a[i]);
if(j > 0) dp[i][j] = max(dp[i][j], dp[i][j - 1] + len * a[n - (j - 1)]);
}
}
int ans = 0;
for(int i=0;i<=n;i++){
ans = max(ans, dp[i][n - i]);
}
cout << ans << '\n';
return 0;
}
dp[i][j]
表示以(i,j)
为终点所能得到的奶酪最多数#include
using namespace std;
int xx[] = {1, 0, -1, 0};
int yy[] = {0, 1, 0, -1};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k;
while(cin >> n >> k) {
if(n == -1 && k == -1) break;
vector<vector<int> > mp(n, vector<int>(n)), dp(n, vector<int>(n));
for(int i=0;i<n;i++) for(int j=0;j<n;j++) {
cin >> mp[i][j];
}
function<int(int, int)> Dfs = [&](int x, int y) {
if(dp[x][y]) return dp[x][y];
int ans = 0;
for(int i=0;i<4;i++) {
for(int j=1;j<=k;j++) {
int dx = x + xx[i] * j;
int dy = y + yy[i] * j;
if(dx >= 0 && dy >= 0 && dx < n && dy < n && mp[dx][dy] > mp[x][y]) {
ans = max(ans, Dfs(dx, dy));
}
}
}
return dp[x][y] = ans + mp[x][y];
};
cout << Dfs(0, 0) << '\n';
}
return 0;
}
#include
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
while(cin >> n && n){
vector<string> mp(n);
for(int i=0;i<n;i++) cin >> mp[i];
vector<vector<int> > dp(n, vector<int>(n));// dp[i][j]表示以(i, j)为左下角的矩形的最大对称大小
int ans = 1;// ans最少是1
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
dp[i][j] = 1;
if(i == 0 || j == n - 1) continue;
int k = dp[i - 1][j + 1];// dp[i][j]可以从dp[i - 1][j + 1]转移过来, 最多能达到dp[i - 1][j + 1] + 1
for(int s=1;s<=k;s++){
if(mp[i - s][j] == mp[i][j + s]) {
dp[i][j] += 1;
}else {
break;
}
}
ans = max(ans, dp[i][j]);
}
}
cout << ans << '\n';
}
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
struct st{
int start;
int end;
int eff;
bool operator < (const st &B)const{
return start < B.start || (start == B.start && end < B.end);
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, r;
cin >> n >> m >> r;
vector<st> s(m + 1);
for(int i=1;i<=m;i++){
cin >> s[i].start >> s[i].end >> s[i].eff;
s[i].end += r;
}sort(s.begin() + 1, s.end());
vector<int> dp(m + 1);
for(int i=1;i<=m;i++){
dp[i] = s[i].eff;
for(int j=1;j<i;j++){
if(s[i].start >= s[j].end){
dp[i] = max(dp[i], dp[j] + s[i].eff);
}
}
}
int ans = 0;
for(int i=1;i<=m;i++){
ans = max(ans, dp[i]);
}
cout << ans;
return 0;
}
对于给定的 a [ i ] a[i] a[i],任意的数+1或者-1,使得 a [ i ] a[i] a[i]变成一个单调不增或者单调不减序列,问操作的最小次数
#include
#include
using namespace std;
const int N = 2e3 + 10;
int a[N], b[N], c[N];
int dp1[N][N];
int dp2[N][N];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=1;i<=n;i++) {
cin >> a[i];
b[i] = a[i];
}sort(b + 1, b + n + 1);
for(int i=1;i<=n;i++) {
c[i] = b[n - i + 1];
dp1[1][i] = abs(a[1] - b[i]);
dp2[1][i] = abs(a[1] - c[i]);
}
for(int i=2;i<=n;i++) {
int tmp1 = dp1[i - 1][1];
int tmp2 = dp2[i - 1][1];
for(int j=1;j<=n;j++) {
tmp1 = min(tmp1, dp1[i - 1][j]);
tmp2 = min(tmp2, dp2[i - 1][j]);
dp1[i][j] = tmp1 + abs(a[i] - b[j]);
dp2[i][j] = tmp2 + abs(a[i] - c[j]);
}
}
int ans = 0x7fffffff;
for(int i=1;i<=n;i++) {
ans = min(ans, dp1[n][i]);
ans = min(ans, dp2[n][i]);
}
cout << ans << '\n';
return 0;
}