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

11.22 NOIP 模拟赛 T1. 破乱的诗歌

思路

场上考虑的其实很有道理来着

考虑左右字符集已经相同的情况是简单的
否则一定要把左右字符集调整到相同

那我们首先不难发现一个至少要使用的区间, 计算方法比较复杂, 但是宗旨是把左右字符集调整到相同且可匹配的必要花费
然后我们应该拓展长的那一端, 直到字符集相同之后可以考虑断开, 以此来获得最优解

#include <bits/stdc++.h>
#define int long long
#define rs rrt, mid + 1, r
#define mid ((l + r) >> 1)
#define ls lrt, l, mid
#define lrt rt << 1
#define rrt lrt | 1char s[1000005];
std::vector <int> pos[26];
int n, cnt[26], num[26], id[26];
int ans = 0, m;signed main () {scanf("%lld%s", &n, s + 1); m = (n + 1) / 2;for (int i = 1; i <= n; i++) cnt[s[i] - 'a']++;for (int i = 1; i <= n; i++) pos[s[i] - 'a'].push_back (i);int L = n / 2 + 1, R = m;memset (num, 0, sizeof num);for (int i = 1; i <= n / 2; i++) num[s[i] - 'a']++;for (int i = 0; i < 26; i++) if (num[i] > cnt[i] / 2)L = std::min (L, pos[i][cnt[i] / 2]);memset (num, 0, sizeof num);for (int i = n; i > m; i--) num[s[i] - 'a']++;for (int i = 0; i < 26; i++) if (num[i] > cnt[i] / 2)R = std::max (R, pos[i][cnt[i] / 2 - 1]);ans = R - L + 1;/*先换过来*/std::vector<int> chg1, chg2; chg1.clear(), chg2.clear();memset (num, 0, sizeof num);for (int i = 1; i <= n / 2; i++) num[s[i] - 'a']++;for (int i = 1; i <= n / 2; i++) if (num[s[i] - 'a'] > cnt[s[i] - 'a'] / 2) chg1.push_back(i), num[s[i] - 'a']--;memset (num, 0, sizeof num);for (int i = n; i > m; i--) num[s[i] - 'a']++;for (int i = n; i > m; i--) if (num[s[i] - 'a'] > cnt[s[i] - 'a'] / 2) chg2.push_back(i), num[s[i] - 'a']--;for (int i = 0; i < chg1.size(); i++) { int p1 = chg1[i], p2 = chg2[i]; std::swap(s[p1], s[p2]); }memset (num, 0, sizeof num);for (int i = 1; i <= n / 2; i++) num[s[i] - 'a']++;for (int i = n; i > m; i--) num[s[i] - 'a']--;int ccnt0 = 0;for (int i = 0; i < 26; i++) ccnt0 += (num[i] == 0);
//    std::cout << ccnt0 << ' ';if (R - m > n / 2 - L + 1) {
//        std::cout << "#1: ";int len = R - m;memset (num, 0, sizeof num);int cnt0 = 0;for (int i = m + 1; i <= R; i++) num[s[i] - 'a']++;for (int i = n / 2 - len + 1; i <= n / 2; i++) num[s[i] - 'a']--;for (int i = 0; i < 26; i++) cnt0 += (num[i] == 0);int l = n / 2 - len + 1, r = R;while (cnt0 != 26 && l > 1 && r < n) {l--, r++; if (s[l] != s[r]) {if (num[s[l] - 'a'] == 0) cnt0--;if (num[s[r] - 'a'] == 0) cnt0--;if (++num[s[l] - 'a'] == 0) cnt0++;if (--num[s[r] - 'a'] == 0) cnt0++;}ans++;}while (l > 1 && r < n) {l--, r++;if (s[l] != s[r]) {num[s[l] - 'a']++, num[s[r] - 'a']--; cnt0 = 24;ans++;while (cnt0 != 26 && l > 1 && r < n) {l--, r++; if (s[l] != s[r]) {if (num[s[l] - 'a'] == 0) cnt0--;if (num[s[r] - 'a'] == 0) cnt0--;if (++num[s[l] - 'a'] == 0) cnt0++;if (--num[s[r] - 'a'] == 0) cnt0++;}ans++;}}}printf("%lld", ans);} else {
//        std::cout << "#2: ";int len = n / 2 - L + 1;memset (num, 0, sizeof num);int cnt0 = 0;for (int i = m + 1; i <= m + len; i++) num[s[i] - 'a']--;for (int i = L; i <= n / 2; i++) num[s[i] - 'a']++;for (int i = 0; i < 26; i++) cnt0 += (num[i] == 0);int l = L, r = m + len;// std::cout << l << ' ' << r << '\n';while (cnt0 != 26 && l > 1 && r < n) {l--, r++; if (s[l] != s[r]) {if (num[s[l] - 'a'] == 0) cnt0--;if (num[s[r] - 'a'] == 0) cnt0--;if (++num[s[l] - 'a'] == 0) cnt0++;if (--num[s[r] - 'a'] == 0) cnt0++;}ans++;}while (l > 1 && r < n) {l--, r++;if (s[l] != s[r]) {num[s[l] - 'a']++, num[s[r] - 'a']--; cnt0 = 24;ans++;while (cnt0 != 26 && l > 1 && r < n) {l--, r++; if (s[l] != s[r]) {if (num[s[l] - 'a'] == 0) cnt0--;if (num[s[r] - 'a'] == 0) cnt0--;if (++num[s[l] - 'a'] == 0) cnt0++;if (--num[s[r] - 'a'] == 0) cnt0++;}ans++;}}}printf("%lld", ans);}return 0;
}

总结

你说的对, 但是码量真大吧

这个有点吃状态的

http://www.gsyq.cn/news/60398.html

相关文章:

  • 莆田一对一家教辅导榜单更新:2025口碑最好的补习机构
  • C++ - 简单实现std::unique_ptr
  • 学习Linux需要买云服务器吗
  • 2025 最新推荐!常州连接线线束厂家权威榜单:品控标准、定制能力与头部合作案例全景测评 LED / 电动工具 / 汽车零部件 / 家用电器电子连接线线束 / 汽车专用线束公司推荐
  • 9 OpenCV中的形态学
  • 2025 年 11 月珊瑚绒厂家推荐排行榜,珊瑚绒布料,珊瑚绒面料,珊瑚绒布,双面珊瑚绒,柔软亲肤保暖面料公司精选
  • AI搜索营销新蓝海:五家领先GEO服务商全景透视与战略布局指南
  • 2025年AI搜索时代五大GEO优化服务商全景解析:核心优势与行业适配指南
  • NOIP 模拟赛 9 比赛总结
  • 2025 最新信息平台推荐!工程项目、招投标、招采、政府采购信息查询平台口碑榜,覆盖拟在建项目精准对接服务
  • 规范驱动开发:AWS Kiro如何重塑AI编程新范式
  • 2025 最新兽药厂家权威推荐榜:技术专利 + 服务能力双维度测评,优质企业全解析
  • 【隐语Secretflow】轻量级隐私计算任务编排框架Kuscia架构解析
  • 2025年行业内复购率高的真空包装袋批发厂家口碑推荐榜,真空包装袋推荐排行榜单精选综合实力TOP企业
  • MWD脉冲器电路关键技术与挑战
  • tignerVNC
  • 深入解析:英集芯 IP5326 集成Type-C协议的2.4A充放电移动电源SOC
  • 视频汇聚平台EasyCVR打造阳光药房远程可视化智慧监管体系
  • 2025年北京回收二手红木家具公司权威推荐榜单:回收红木家具高价/回收阔叶黄檀家具/回收缅甸花梨家具源头公司精选
  • 2025澳大利亚研究生留学中介哪个好
  • 2025年湖北阴囊湿疹怎么治疗护理权威推荐榜单:湖北附睾结核怎么治疗/湖北脓尿怎么治疗/湖北肾盂肾炎怎么治疗综合医院特色门诊精选
  • 大象《Thinking in Projects》读书笔记3
  • PDF超级助手软件下载安装教程_免费PDF编辑工具使用指南
  • python-IPO模型
  • 2025厦门十大正规留学机构有哪些
  • windows下 自动检测网络状态,并重连至指定wifi的脚本
  • map---显示地区地图
  • 2025年武汉优质的华新水泥生产厂家推荐榜单,华新水泥有哪些鑫俊熙层层把关品质优
  • 计算机视觉:pyqt5+yoloV5目标检测平台 python实战 torch 目标识别 大数据项目 目标跟踪(建议收藏)✅ - 指南
  • 2025年北京阅卷考试软件公司权威推荐榜单:自动阅卷软件/网上阅卷的软件/答题卡扫描源头公司精选