当前位置:网站首页>Codeforces Round #793 (Div. 2)(A-D)
Codeforces Round #793 (Div. 2)(A-D)
2022-07-04 08:34:00 【ccsu_yuyuzi】
目录
A. Palindromic Indices
题意:给你一个回文串,你可以删除一个字符串,保证这个回文串还是一个回文串,问有几种删法
思路:只需要把中间一段相同的字符删除一个即可.
#include<map>
#include<cmath>
#include<set>
#include<queue>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_set>
#include<unordered_map>
#define int long long
using namespace std;
const int N =5e5+10,mod=998244353;
void solve()
{
int n;
string s;
cin>>n>>s;
int cnt=0;
if(s.size()%2==0)
{
int l=(s.size()-1)/2;
for(int i=l;i>=0;i--)
{
if(s[i]==s[l])
cnt++;
else
break;
}
cnt*=2;
}
else
{
int l=s.size()/2;
for(int i=l;i>=0;i--)
{
if(s[i]==s[l])
cnt++;
else
break;
}
cnt=cnt*2-1;
}
cout<<cnt<<"\n";
return ;
}
signed main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}
B. AND Sorting
Problem - B - Codeforceshttps://codeforces.com/contest/1682/problem/B
题意:给你一个数组,有原本的升序的数组得来(原本是0,1,2,3...n-1),存在一个x,当(a[i]&a[j])==x的时候,即可进行交换,问当x最大是多少的时候我们可以从原数组得到题目所给的数组.
思路:要让x作为中转站可以将所有的不同位置上的数字进行两两交换,吧不在原来位置上的数字相&即可:
#include<map>
#include<cmath>
#include<set>
#include<queue>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_set>
#include<unordered_map>
#define int long long
using namespace std;
const int N =5e5+10,mod=998244353;
void solve()
{
int n,x;
cin>>n;
int ans=-1;
for(int i=1;i<=n;i++)
{
cin>>x;
if(ans==-1&&x!=i-1)
{
ans=x;
}
else if(ans!=-1&&x!=i-1)
{
ans&=x;
}
}
cout<<ans<<"\n";
return ;
}
signed main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}
C. LIS or Reverse LIS?
题意:给你一个数组,你可以在它重新排列之后,求得它的正向的严格最长上升子序列的长度还有反向的严格最长上升子序列的长度.两者取最小值,求这个最小值最大是多少.
思路:我们用map记录所有数组元素的个数,map的长度即为他升序的最长长度,然后我们统计这个升序序列中每个元素超过2个的个数,将他们拼在右边,即为反向最长上升子序列的长度,但是由于可能左边的正向最长上升子序列的长度大于右边的反向最长上升子序列的长度.我们可以适当地把左边的一些数字移动到右边去.举个例子:
数组
1 1 1 2 3 4 4 4 4 4 4 5 6 6
我们进行操作后,分别把反向和正向的最长上升子序列放在两边:
1 2 3 4 5 6 6 4 1
这个时候结果的值为3,但是我们发现可以适当地移动一些左边的值到右边,就变成了:
1 2 4 5 6 6 4 3 1
如上操作即可求出答案
#include<map>
#include<cmath>
#include<set>
#include<queue>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_set>
#include<unordered_map>
using namespace std;
const int N =5e5+10,mod=998244353;
map<int,int>ma;
int a[200005];
void solve()
{
ma.clear();
int n,x;
cin>>n;
for(int i=1;i<=n;i++)
cin>>x,ma[x]++;
if(n==1)
{
cout<<"1\n";
return ;
}
int len1=ma.size(),len2=0;
for(auto &it:ma)
{
if(it.second>=2)
len2++;
}
int ans=(len1+len2+1)/2;
cout<<ans<<"\n";
return ;
}
signed main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}
D. Circular Spanning Tree
Problem - D - Codeforceshttps://codeforces.com/contest/1682/problem/D
题意:给你一个字符串,(0-n-1)对应1-n点的度数的奇偶性,当当前的点的度数为奇数的时候,字符表示为'1',反之为'0'.我们要根据这个字符串构造一棵树出来.
思路:首先排除不可能成立的情况.我们知道树存在n-1调边,那么肯定存在偶数个度数为奇数的点,而且这些点度数要大于0.那么除此之外直接输出NO.
然后就是对于树的构造.我们可以把每个度数为奇数的点作为一条链的头,因为度数为奇数的点肯定有偶数个,那么必定有偶数条链,我们直接把链尾相连(都连到同一条链子的链尾,这样可以避免交叉).这样每个链尾本身有度数1,再去链接奇数个点,度数肯定就变成了偶数.即可成立.
#include<map>
#include<cmath>
#include<set>
#include<queue>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_set>
#include<unordered_map>
using namespace std;
vector<int>wei;
void solve()
{
wei.clear();
int n,st=0;
string s;
cin>>n>>s;
int cnt=0,len=s.size();
for(int i=0;i<len;i++)
{
if(s[i]=='1')
cnt++,st=i;
}
if(cnt<2||cnt%2==1)
{
cout<<"NO\n";
return ;
}
cout<<"YES\n";
for(int i=0;i<len;i++)
{
if(s[(st+i+1)%len]=='1')
{
wei.push_back((st+i)%len);
continue;
}
cout<<(st+i)%len+1<<" "<<(st+i+1)%len+1<<"\n";
}
for(int i=1;i<wei.size();i++)
{
cout<<wei[0]+1<<" "<<wei[i]+1<<"\n";
}
return ;
}
signed main()
{
int t;
cin>>t;
while(t--)
solve();
return 0;
}
边栏推荐
- Cancel ctrl+alt+delete when starting up
- Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
- Difference between static method and non static method (advantages / disadvantages)
- Set and modify the page address bar icon favicon ico
- AcWing 244. Enigmatic cow (tree array + binary search)
- deno debugger
- 没有Kubernetes怎么玩Dapr?
- Unity-写入Word
- Fault analysis | MySQL: unique key constraint failure
- 1. Kalman filter - the best linear filter
猜你喜欢
AcWing 244. Enigmatic cow (tree array + binary search)
DM8 tablespace backup and recovery
【性能測試】一文讀懂Jmeter
What if I forget the router password
The second session of the question swiping and punching activity -- solving the switching problem with recursion as the background (I)
[test de performance] lire jmeter
ZABBIX 5.0 monitoring client
Email alarm configuration of ZABBIX monitoring system
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
Système de surveillance zabbix contenu de surveillance personnalisé
随机推荐
[go basics] 1 - go go
团体程序设计天梯赛-练习集 L1-006 连续因子
go-zero微服务实战系列(九、极致优化秒杀性能)
Devops Practice Guide - reading notes (long text alarm)
Moher College phpmailer remote command execution vulnerability tracing
C#实现一个万物皆可排序的队列
Set and modify the page address bar icon favicon ico
2022 electrician (intermediate) examination question bank and electrician (intermediate) examination questions and analysis
WordPress get_ Users() returns all users with comparison queries - PHP
Leetcode 146. LRU cache
Technology sharing | MySQL parallel DDL
Azure ad domain service (II) configure azure file share disk sharing for machines in the domain service
ES6 summary
Leetcode topic [array] -136- numbers that appear only once
How to send pictures to the server in the form of file stream through the upload control of antd
ZABBIX 5.0 monitoring client
Moher college phpMyAdmin background file contains analysis traceability
snipaste 方便的截图软件,可以复制在屏幕上
PHP converts seconds to timestamps - PHP
What determines vacuum permittivity and vacuum permeability? Why do these two physical quantities exist?