当前位置: 首页 > news >正文

2025-11-19

CF

Problem - 1418C - Codeforces(dp+贪心好题!)(1500)

dp操作,要分开判断先手和后手

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
int a[N],dp[N][2];
//dp[i][0] 最后一次是后手,dp[i][1] 最后一次是先手
//dp[][]记录先手取 1的最小数量
int inf = 1e9;void solve()
{int n;cin >> n;for (int i = 1; i <= n;i++){cin >> a[i];}for (int i = 0; i <= n;i++){dp[i][0] = inf;dp[i][1] = inf;}dp[0][0] = 0;dp[1][1] = a[1];for (int i = 2; i <= n;i++){dp[i][1] = min(dp[i - 1][0] + a[i], dp[i - 2][0] + a[i] + a[i - 1]);dp[i][0] = min(dp[i - 1][1], dp[i - 2][1]);}cout << min(dp[n][0], dp[n][1]) << endl;
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--){solve();}
}

一个很妙的贪心解法
找全为1的长度段
分成3个1,自己拿2个,朋友skip一个,这满足最小
0的话留给朋友就行了

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
int a[N];void solve()
{int n;int cnt = 0,ans=0;cin >> n;cin >> a[1];for (int i = 2; i <= n;i++){cin >> a[i];if(a[i]==1){cnt++;}else{ans+=cnt/3;cnt = 0;}}ans += cnt / 3;cout << ans + a[1] << endl;
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--){solve();}
}

Problem - 1753A2 - Codeforces(贪心)(双指针)

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
int a[N];void solve()
{int n;cin >> n;for (int i = 1; i <= n;i++)cin >> a[i];int sum = 0;for (int i = 1; i <= n;i++){sum += a[i];}if(sum%2){cout << -1 << endl;return;}int l = 1, r;vector<pair<int, int>> ans;while(l<=n){if(a[l]==0){ans.push_back({l, l});l++;continue;}r = l + 1;while(a[r]==0)r++;if(a[l]==a[r]){if((r-l+1)%2==0)ans.push_back({l, r});else{ans.push_back({l, l});ans.push_back({l + 1, r});}}else{if((r-l+1)%2==1){ans.push_back({l, r});}else{ans.push_back({l, l});ans.push_back({l + 1, r});}}l = r + 1;}cout << ans.size() << endl;for(auto x:ans){cout << x.first << " " << x.second << endl;}
}int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);int T;cin >> T;while (T--){solve();}
}

Problem - 1307C - Codeforces(贪心)

一道考验观察能力的题
需要发现最大可能要不1个字符,要不2个字符
所以只要贪心就行啦

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
LL c, cnt;int main()
{ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);string s;cin >> s;LL maxx = 0;for (int t = 0; t < 26; t++){for (int j = 0; j < 26; j++){cnt = 0, c = 0;for (int i = 0; i < s.size(); i++){if (s[i] - 'a' == j)cnt += c;if (s[i] - 'a' == t)c++;}maxx = max(maxx, cnt);}maxx = max(maxx, c);}cout << maxx << endl;
}

碎碎念

今天考完人工智能导论了,很忙的期末周马上开始了

http://www.jsqmd.com/news/44959/

相关文章:

  • docker 自用手册
  • Gemini 3发布与小试牛刀
  • kilocode_idea端测试
  • 【触想智能】工业一体机在户外使用要注意的问题分享
  • #20232329 2025-2026-1 《网络与系统攻防技术》实验八实验报告
  • IDEA Debug使用方法
  • 高性能计算实践- 遥遥领先!看看 streaming store 在矩阵转置中有多少提升
  • linux ftp 端口查看
  • 详细介绍:JavaEE初阶7.0
  • noip10
  • 关于jupyterNotebook 的虚拟环境配置
  • 什么?Viggle Ai Pro版会员免费送?
  • linux ftp 用户权限
  • linux ftp 用户名 密码
  • linux ftp 用户及目录
  • linux ftp 用户创建
  • [豪の算法奇妙冒险] 代码随想录算法训练营第一天 | 704-二分查找、27-移除元素、977-有序数组的平方
  • 完整教程:【C语言实战(44)】C语言打造全能简易计算器:突破运算极限
  • Google 王炸!Gemini 3 Pro 上线:前端能力、代码理解全面进化。
  • 完整教程:GPTBots 工作流:让AI从“会说“到“会做“的技术演进引言:企业AI化的瓶颈在哪里?
  • html-webpack-plugin扩展创建:自定义钩子构建
  • Android中EditText同时支持textMultiLine与imeOptions(action/actionSend/...)
  • Day43(13)-基本上都是在敲SQL-db04
  • 空间变换层和自注意力机制
  • linux ftp 客户端安装
  • MacX Video Converter Pro for Mac v6.8.2 安装视频转换器安装步骤(附安装包)
  • 数字分身---沃伦巴菲特
  • SPYSE团队独家专访:构建互联网基础设施搜索引擎的技术实践
  • 数学的大厦(四):减法与整数
  • 深入解析:Kotlin 高阶函数在回调设计中的最佳实践