A.暴力把每个位置的字符改成另外25个字符,判断是否回文。

#include<bits/stdc++.h>
using namespace std; string s; int main()
{
ios::sync_with_stdio(false);
cin >> s;
for(int i = ;i < s.length();i++)
{
for(char j = 'a';j <= 'z';j++)
{
if(s[i] == j) continue;
string ss = s;
ss[i] = j;
string sss = ss;
reverse(sss.begin(),sss.end());
if(sss == ss)
{
cout << "YES" << endl;
return ;
}
}
}
cout << "NO" << endl;
return ;
}

B.先判断是否都能变成相同串,若可以,则有一个特点,最后的串肯定与初始某个串相同,直接暴力就可以了。

#include<bits/stdc++.h>
using namespace std; int n,a[][];
string s[];
map<string,int> mp; int main()
{
ios::sync_with_stdio(false);
cin >> n;
for(int i = ;i <= n;i++) cin >> s[i];
int cnt = ;
for(int i = ;i < s[].length();i++)
{
string ss = s[].substr(i)+s[].substr(,i);
mp[ss] = ;
}
for(int i = ;i <= n;i++)
{
if(s[i].length() != s[].length() || !mp.count(s[i]))
{
cout << - << endl;
return ;
}
}
int ans =1e9;
for(int i = ;i <= n;i++)
{
int sum = ;
for(int j = ;j <= n;j++)
{
if(i == j) continue;
for(int k = ;k < s[i].length();k++)
{
string ss = s[j].substr(k)+s[j].substr(,k);
if(ss == s[i])
{
sum += k;
break;
}
}
}
ans = min(ans,sum);
}
cout << ans << endl;
return ;
}

C.这道题首先注意负数和0的gcd,可以用__gcd尝试一下。

先判断初始的gcd是否大于1,若大于1,直接YES。

否则,剩下的数我们只用考虑它们的奇偶性。

相邻两个分为4中情况:

  ①奇奇  直接操作这两个位置  ans+1

  ②偶偶  不用动

  ③奇偶  操作两次变成奇奇  ans+2

  ④偶奇  考虑到最少的操作次数,把奇与其后面的数操作,若奇已经是最后一位,偶奇操作两次,变成奇奇

#include<bits/stdc++.h>
using namespace std; int n,a[]; int main()
{
ios::sync_with_stdio(false);
cin >> n;
cin >> a[];
int t = a[];
for(int i = ;i <= n;i++)
{
cin >> a[i];
t = __gcd(t,a[i]);
}
if(t > )
{
cout << "YES" << endl << << endl;
return ;
}
int ans = ;
for(int i = ;i < n;i++)
{
if(a[i]% && a[i+]% == )
{
ans += ;
}
else if(a[i]% && a[i+]%)
{
ans++;
a[i+]++;
}
}
if(a[n]%) ans += ;
cout << "YES" << endl << ans << endl;
return ;
}

D.我们对个数分奇偶。

若是奇数个:

  对A排序,A1对应下标加入,剩下偶数个数,两两分,每组取B中最大的那个下标。

若是偶数个:

  对A排序,A1对应下标加入,剩下n-2个跟上面相同操作,An对应下标加入。

这样操作的结果肯定是符合要求的。

#include<bits/stdc++.h>
using namespace std; int n,b[],c[];
struct xx
{
int x,id;
friend bool operator <(xx a,xx b)
{
return a.x > b.x;
}
}a[]; vector<int> ans; int main()
{
ios::sync_with_stdio(false);
cin >> n;
long long sum1 = ,sum2 = ;
for(int i = ;i <= n;i++) cin >> a[i].x,a[i].id = i;
for(int i = ;i <= n;i++) cin >> b[i];
sort(a+,a++n);
ans.push_back(a[].id);
for(int i = ;i <= n;i += )
{
if(i == n) ans.push_back(a[i].id);
else
{
if(b[a[i].id] > b[a[i+].id]) ans.push_back(a[i].id);
else ans.push_back(a[i+].id);
}
}
cout << ans.size() << endl;
for(int i = ;i < ans.size();i++) cout << ans[i] << " ";
cout << endl;
return ;
}

另外还有种做法,随机大法,好厉害的样子。

#include<bits/stdc++.h>
using namespace std; int n,a[],b[],c[]; int main()
{
ios::sync_with_stdio(false);
cin >> n;
long long sum1 = ,sum2 = ;
for(int i = ;i <= n;i++) cin >> a[i],sum1 += a[i];
for(int i = ;i <= n;i++) cin >> b[i],sum2 += b[i];
int k = n/+;
for(int i = ;i <= n;i++) c[i] = i;
while()
{
long long x1 = ,x2 = ;
for(int i = ;i <= k;i++)
{
x1 += a[c[i]];
x2 += b[c[i]];
}
if(x1* > sum1 && x2* > sum2)
{
cout << k << endl;
for(int i = ;i <= k;i++) cout << c[i] << ' ';
cout << endl;
return ;
}
random_shuffle(c+,c++n);
}
return ;
}

Codeforces_798的更多相关文章

随机推荐

  1. 1069 微博转发抽奖 (20分)C语言

    小明 PAT 考了满分,高兴之余决定发起微博转发抽奖活动,从转发的网友中按顺序每隔 N 个人就发出一个红包.请你编写程序帮助他确定中奖名单. 输入格式: 输入第一行给出三个正整数 M(≤ 1000). ...

  2. C++装饰器模式

    UML图: #include <iostream> #include <string> #include <windows.h> using namespace s ...

  3. MST + 树形 dp

    Genghis Khan(成吉思汗)(1162-1227), also known by his birth name Temujin(铁木真) and temple name Taizu(元太祖), ...

  4. Proxmox VE:自建虚拟化方案

    Proxmox VE 简介 Proxmox Virtual Environment,或 Proxmox VE,是来自德国的开源虚拟化方案.软件和社区支持都是免费的,企业用户则可以通过订阅制获得付费商业 ...

  5. python爬虫——selenium+firefox使用代理

    本文中的知识点: python selenium库安装 firefox geckodriver的下载与安装 selenium+firefox使用代理 进阶学习 搭建开发环境: selenium库 fi ...

  6. 数学基础系列(六)----特征值分解和奇异值分解(SVD)

    一.介绍 特征值和奇异值在大部分人的印象中,往往是停留在纯粹的数学计算中.而且线性代数或者矩阵论里面,也很少讲任何跟特征值与奇异值有关的应用背景. 奇异值分解是一个有着很明显的物理意义的一种方法,它可 ...

  7. RTC时间设置

    1.命令行输入date,查看系统时间. 2.命令行输入 date -s "2019-01-21 16:03:00" 修改系统时间. 3.命令行输入 hwclock -w 将修改后的 ...

  8. spring boot配置spring-data-jpa的时候报错CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError

    org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager f ...

  9. Python Selenium定位元素常用解决办法

       在做web应用的自动化测试时,定位元素是必不可少的,这个过程经常会碰到定位不到元素的情况(报selenium.common.exceptions.NoSuchElementException), ...

  10. 深入Node模块Buffer-学会操作二进制

    Buffer 作为 nodejs 中重要的概念和功能,为开发者提供了操作二进制的能力.本文记录了几个问题,来加深对 Buffer 的理解和使用: 认识缓冲器 如何申请堆外内存 如何计算字节长度 如何计 ...