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

【枚举,构造】CF1582 C D

Problem - C - Codeforces

题意:

 

思路:

思路很简单,只删除一种,直接枚举删除的是哪一种即可

但是回文子序列的判定我vp的时候写的很答辩,也不知道为什么当时要从中间往两边扫,纯纯自找麻烦

然后就越改越复杂,两小时的时候才过C

所以有新想法的时候还是把new idea整合一下,再去写代码

直接从两边往中间扫就行了,然后把该删的都删掉

Code:

#include <bits/stdc++.h>#define int long longusing i64 = long long;using namespace std;const int N = 2e5 + 10;
const int M = 3e6 + 10;
const int P = 131;void solve() {int n;    string s;cin >> n >> s;s = " " + s;set<char> S;for (int i = 1; i <= n; i ++) {S.insert(s[i]);}int ans = 1e9;for(auto c: S) {int l = 1, r = n;int cnt = 0;while(l <= r) {if (s[l] == s[r]) {l ++;r --;}else if (s[l] == c) {cnt ++;l ++;}else if (s[r] == c) {cnt ++;r --;}else {cnt = 1e9;break;}}ans = min(ans, cnt);}if (ans == 1e9) {cout << -1 << "\n";}else {cout << ans << "\n";}
}
signed main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1;cin >> t;while(t --) {solve();}return 0;
}

 

Problem - D - Codeforces

题意:

思路:

构造题,看样例发现什么都看不出来

先去考虑特殊的情况

如果N = 2,那么

a * x + b * y = 0

x = - b

y = a 

如果N = 3

有三种情况,以一种举例:

x = - c

y = - c

z = a + b

当然这里的c和-c可以替换成b和-b,a和-a,所以是三种情况

然后考虑将特殊情况“组合”到一般情况中去

可以发现,如果n是奇数,那么就分组成3+2+2+2+....的形式

否则就可以是2+2+2+....的形式

这样就做完了

这道题的启发是:可以用特殊情况“组合”成一般情况进行考虑

Code:

#include <bits/stdc++.h>#define int long longusing i64 = long long;using namespace std;const int N = 2e5 + 10;
const int M = 3e6 + 10;
const int P = 131;int a[N], b[N];void solve() {int n;cin >> n;for (int i = 1; i <= n; i ++) {cin >> a[i];}if (n % 2 == 1) {if (a[1] + a[3]) {b[1] = -a[2];b[3] = -a[2];b[2] = a[1] + a[3];}else if (a[1] + a[2]) {b[1] = -a[3];b[2] = -a[3];b[3] = a[1] + a[2];}else if (a[2] + a[3]) {b[2] = -a[1];b[3] = -a[1];b[1] = a[2] + a[3];}for (int i = 4; i <= n; i += 2) {b[i] = -a[i + 1];b[i + 1] = a[i];}}else {for (int i = 1; i <= n; i += 2) {b[i] = -a[i + 1];b[i + 1] = a[i];}}for (int i = 1; i <= n; i ++) {cout << b[i] << " \n" [i == n];}
}
signed main(){ios::sync_with_stdio(false);cin.tie(nullptr);int t = 1;cin >> t;while(t --) {solve();}return 0;
}

http://www.lryc.cn/news/111388.html

相关文章:

  • POJ 3169 Layout BellmanFord Dijkstra
  • 数据库管理员知识图谱
  • 中兴服务器支持百度“文心一言”,助力AI产业发展
  • STM 如何通过网络 time.windows.com获取时间
  • 数据结构——红黑树
  • 【C++】数据结构与算法:常用排序算法
  • 【C++】Bullet3代码存档
  • 弘扬“两弹一星”精神,勇攀科学技术高峰——道本科技商业大学党日活动圆满落幕
  • Java中创建对象的几种方式
  • Python(三)
  • android 如何分析应用的内存(十五)——Visual Studio Code 调试Android应用
  • 宁波银行最新内推码 MK4913
  • postgresql|数据库|MySQL数据库向postgresql数据库迁移的工具pgloader的部署和初步使用
  • 【Python从小白到高手】---函数基础
  • postman----传参格式(json格式、表单格式)
  • Uni-Dock:GPU 分子对接使用教程
  • 【Python】数据分析+数据挖掘——掌握Python和Pandas中的单元格替换操作
  • Godot 4 源码分析 - 增加格式化字符串功能
  • C#中XML文档与Treeview控件操作的数据同步
  • 【Java Web基础】mvn命令、Maven的安装与配置
  • 加强Web应用程序安全:防止SQL注入
  • 【云原生】k8s中Contrainer 生命周期回调/策略/指针学习
  • electron+vue3全家桶+vite项目搭建【25】使用electron-updater自动更新应用
  • SQL 表别名 和 列别名
  • 面试之快速学习c++11-函数模版的默认模版参数,可变模版,tuple
  • Visual Studio Code 常见的配置、常用好用插件以及【vsCode 开发相应项目推荐安装的插件】
  • 源码编译安装gcc
  • pc文件上传
  • Vue3_对响应式对象解构赋值之后失去响应性
  • 3d 地球与卫星绕地飞行