Codeforces #640 div4 F~G (构造二连弹)
2024-10-21 10:14:24
题意:求一个只由\(01\)组成的字符串,使得它所有长度为\(2\)的子串满足:每对子串的数字和为\(0,1,2\)的个数为\(a,b,c\).
题解:我们先考虑子串数字和为\(1\)的情况,构造出一个\(10\)的循环串,然后在它的头部尾部适当添加\(1\)和\(0\),使得a和c也满足即可.需要特判\(b=0\)的情况,并且\(b\)奇偶不同构造情况也不同.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 1e6 + 10;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int> PII;
typedef pair<long,long> PLL; int t;
int a,b,c;
string s;
int main() {
ios::sync_with_stdio(false);
cin>>t;
while(t--){
s="";
cin>>a>>b>>c;
if(b==0){
if(c){
c++;
while(c--) s+="1";
}
if(a){
a++;
while(a--) s+="0";
}
}
else if(b%2==1){
int tmp=(b+1)/2;
while(tmp--) s+="10";
while(c--) s="1"+s;
while(a--) s+="0";
}
else{
int tmp=b/2;
while(tmp--) s+="10";
while(c--) s="1"+s;
while(a--) s+="0";
s+="1";
}
cout<<s<<endl;
} return 0;
}
题意:给你一个数\(n\),构造一个\(1\le p \le n\)的序列,对于\(\forall\;1\le i < n\)有:\(2 \le |p_{i}-p_{i+1}| \le 4\).如果不存在,输出\(-1\).
题解:易知:当且仅当\(n=2\;or\;n=3\)时不满足条件.
首先先将\(1\)~\(n\)中所有的偶数输出,然后:
1.如果\(n\)是偶,那么就先输出第二大和最大的奇数,剩下的奇数递减输出即可.
2.如果n是奇,那么就先输出第三大的奇数,剩下的奇数递减输出即可.
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define me memset
const int N = 2e4;
const int mod = 1e9 + 7;
using namespace std;
typedef pair<int,int> PII;
typedef pair<long,long> PLL; int t;
int n; int main() {
ios::sync_with_stdio(false);
cin>>t;
while(t--){
cin>>n;
if(n==2 || n==3) puts("-1");
else{
for(int i=2;i<=n;i+=2){
printf("%d ",i);
}
if(n%2==0){
printf("%d %d ",n-3,n-1);
for(int i=n-5;i>=1;i-=2){
printf("%d ",i);
}
}
else if(n%2==1){
printf("%d ",n-4);
printf("%d ",n);
for(int i=n-2;i>=1;i-=2){
if(i!=n-4)
printf("%d ",i);
}
}
puts("");
}
} return 0;
}
最新文章
- 李洪强iOS面试题之-iOS选择题
- 基于GTID Replication主从数据不一致操作
- InstallShield2008脚本安装制作方法Setup
- xfire找不到services.xml
- Nginx 变量漫谈(五)
- 解决Ubuntu DNS解析慢、上网速度慢
- 使用SVN clang: error: linker command failed with exit code 1 (use -v to see invocation)
- IOS开发-UI学习-UISlider(滑动条)的使用
- .NET方法无限传参数技术
- VC使用双缓冲制作绘图控件
- Android动画曲线库AndroidEasingFunctions
- linux的PAM认证和shadow文件中密码的加密方式
- confluence中org.apache.tomcat.util.net.NioEndpoint$Acceptor.run Socket accept failed的解决方法
- C# 线程间互相通信 AutoResetEvent和ManualResetEvent
- Ubuntu 下安装LEMP环境 实战
- 雷林鹏分享:jQuery EasyUI 扩展
- Java多线程之syncrhoized内置互斥锁的用法详解
- Intersection(Check)
- Service Mesh服务网格新生代——Istio
- Java发送HTTP POST请求示例