当前位置: 首页 > 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.gsyq.cn/news/54561.html

相关文章:

  • docker 自用手册
  • Gemini 3发布与小试牛刀
  • kilocode_idea端测试
  • 详细介绍:JavaEE初阶7.0
  • 什么?Viggle Ai Pro版会员免费送?
  • linux ftp 用户权限
  • 完整教程:GPTBots 工作流:让AI从“会说“到“会做“的技术演进引言:企业AI化的瓶颈在哪里?
  • html-webpack-plugin扩展创建:自定义钩子构建
  • Android中EditText同时支持textMultiLine与imeOptions(action/actionSend/...)
  • 空间变换层和自注意力机制
  • MacX Video Converter Pro for Mac v6.8.2 安装视频转换器安装步骤(附安装包)
  • 深入解析:Kotlin 高阶函数在回调设计中的最佳实践
  • 信息化、数字化、智能化、智慧化、数智化,到底啥区别 - 智慧园区
  • 洛谷 B4413:[GESP202509 三级] 数组清零
  • 中大型超市智能运营导购系统:AI 精准推送,滞销品库存加速 19%!
  • linux ftp shell
  • 全国计算机等级考试——二级JAVA完整大题题库【五十三道】
  • 【C + +】unordered_set 和 unordered_map 的用法、区别、性能全解析 - 实践
  • Spring Boot迅速集成MiniMax、CosyVoice实现文本转语音
  • 完整教程:微信生态新机遇:视频号推客模式助力商家突围
  • win10/win11系统默认应用或文件打开方式重启后被自动重置的解决办法
  • 2025 上海办公室 商铺装修核心服务商 TOP5 解析报告:双场景适配能力与品质选型全景指南
  • 2025CCPC济南站游记
  • PQ v.Next Alpha阶段发布
  • 三分稀疏图染色的多项式时间证明
  • 251119
  • CCF GESP 五级真题考频与知识点速查表
  • 爱玩机工具箱s22.1下载
  • 2025-11-19 早报新闻
  • 2025有限元分析/计算/测试服务商口碑榜:长春六耳科技领跑,技术深耕者成行业标杆