Codeforces Round #306 (Div. 2) ABCDE(构造)
题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5。
题解:看起来很简单,但是一直错,各种考虑不周全,最后只能很蠢的暴力,把所有的AB和BA的位置求出来,能有一对AB和BA不重叠即可。
#include <bits/stdc++.h>
using namespace std; char a[];
vector<int> ab;
vector<int> ba;
int main()
{
while (~scanf("%s", a)) {
int len = strlen(a);
ab.clear();
ba.clear(); for (int i = ; i < len - ; ++i) {
if (a[i] == 'A' && a[i + ] == 'B') {
ab.push_back(i);
}
if (a[i] == 'B' && a[i + ] == 'A') {
ba.push_back(i);
}
} if (ab.size() == || ba.size() == ) {
puts("NO");
} else if (ab.size() > && ba.size() > ) {
puts("YES");
} else {
int flag = false;
for (int i = ; i < ab.size(); ++i) {
for (int j = ; j < ba.size(); ++j) {
if (!(ab[i] + == ba[j]) && !(ab[i] - == ba[j])) flag = true;
}
}
if (flag) puts("YES");
else puts("NO");
}
}
return ;
}
题意:输入n, l, r, x,代表有n题,每题有一个难度,选其中部分题(大于2题),要求难度和在[l, r]之间,最大难度最小难度的差不超过x。1 ≤ n ≤ 15
题解:暴力,dfs求全部情况。复杂度2^15
#include <bits/stdc++.h> using namespace std; int a[];
int n, l, r, x;
int vis[];
int ans; bool ok()
{
int cnt = ;
for (int i = ; i < n; ++i) if (vis[i]) cnt++;
if (cnt < ) return false;
int low = ;
int high = ;
int sum = ;
for (int i = ; i < n; ++i) {
if (!vis[i]) continue;
low = min(low, a[i]);
high = max(high, a[i]);
sum += a[i];
}
if (high - low >= x && sum <= r && sum >= l) return true;
return false;
} void dfs(int pos)
{
if (pos == n) {
if (ok()) ans++;
return ;
}
vis[pos] = ;
dfs(pos + );
vis[pos] = ;
dfs(pos + );
} int main()
{
while (cin >> n >> l >> r >> x) {
for (int i = ; i < n; ++i) cin >> a[i];
ans = ;
dfs();
cout << ans << endl;
}
return ;
}
题意:给一个不超过100位的没有前导零的非负整数,问可不可能移出一部分数字,事剩下至少一位非负整数能被8整除。
题解:能被8整除的数有一个特性:一个整数的末3位若能被8整除,则该数一定能被8整除。求出所有的三位以内能被8整除的数,分别试一下就ok了。
#include <bits/stdc++.h>
using namespace std; int ans[];
char a[];
//一个整数的末3位若能被8整除,则该数一定能被8整除。
bool ok(int x)
{
int num[];
int cnt = ;
while (x) {
num[cnt++] = x % ;
x /= ;
}
reverse(num, num + cnt);
int len = strlen(a);
int pos = ;
int i;
for (i = ; i < cnt; ++i) {
while (a[pos] - '' != num[i] && pos < len) {
pos++;
}
if (pos >= len) break;pos++;
}
if (i == cnt) return true;
return false;
} int main()
{
int idx = ;
ans[] = ;
for (int i = ; i < ; ++i) {
if (i * > ) break;
ans[idx++] = i * ;
} scanf("%s", a);
for (int i = ; i < idx; ++i) {
if (ok(ans[i])) {
printf("YES\n%d\n", ans[i]);
return ;
}
}
int len = strlen(a);
for (int i = ; i < len; ++i) {
if (a[i] == '') {
printf("YES\n0\n");
return ;
}
}
printf("NO\n"); return ;
}
题意:给一个k(1 ≤ k ≤ 100) ,问是否存在一个无向图至少有一个桥且所有点的度都是k。如果存在给出任意一种解。
题解:对于k+1个点的完全图,所有点的度都是k。考虑两堆k+1的点,然后再用两个点组成桥,每个点和其中一个堆连k-1就可以了。每次桥上的点连堆中两个点,然后删除这两个点的连线,度数不变。同时也可以发现只有k-1为偶数是,即k为奇数时才有解。
#include <cstdio> using namespace std; int main()
{
int k;
while (~scanf("%d", &k)) {
if ((k & ) == ) {
puts("NO");
} else if (k == ) {
puts("YES\n2 1\n1 2");
} else {
puts("YES");
printf("%d %d\n", * k + , k * k + * k);
for (int i = ; i < k; ++i) {
printf("%d %d\n", * k + , i);
printf("%d %d\n", * k + , i + k + );
}
printf("%d %d\n", * k + , * k + );
for (int i = ; i <= k + ; ++i) {
for (int j = i + ; j <= k + ; ++j) {
if (i < k - && (i & ) && i + == j) continue;
printf("%d %d\n", i, j);
printf("%d %d\n", k + + i, k + + j);
}
}
}
}
return ;
}
E. Brackets in Implications(构造)
题意:给定01间运算0->0=1 0->1=1 1->0=0 1->1=1 默认从左到右运算 问能不能通过加括号使得表达式值变为0
题解:很容易发现最后一位必须是0,倒数第二位如果是1,那么不用加括号直接为0。如果倒数第二位为0,那么需要在前面找到一个0,两个0即中间的1组成1,然后同上。两个0和中间的1组成1的方法可以为(0(11...110))。
#include <iostream>
#include <stdio.h> using namespace std; int a[];
int n;
int main()
{
cin >> n;
for (int i = ; i < n; ++i)
scanf("%d", a + i); if (n == ) {
if (a[] == ) puts("YES\n0");
else puts("NO");
} else if (n == ) {
if (a[] == && a[] == ) puts("YES\n1->0");
else puts("NO");
} else if (a[n - ] == ) {
puts("NO");
} else {
if (a[n - ] == ) {
puts("YES");
for (int i = ; i < n - ; ++i) {
printf("%d->", a[i]);
}
puts("");
} else {
int pos = -;
for (int i = n - ; i >= ; --i) {
if (a[i] == ) {
pos = i;
break;
}
}
if (pos == -) puts("NO");
else {
puts("YES");
for (int i = ; i < pos; ++i) {
printf("%d->", a[i]);
}
printf("(0->(");
for (int i = pos + ; i < n - ; ++i) {
printf("%d->", a[i]);
}
puts("0))->0");
}
}
}
return ;
}
Codeforces Round #306 (Div. 2) ABCDE(构造)的更多相关文章
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- 数学/找规律/暴力 Codeforces Round #306 (Div. 2) C. Divisibility by Eight
题目传送门 /* 数学/暴力:只要一个数的最后三位能被8整除,那么它就是答案:用到sprintf把数字转移成字符读入 */ #include <cstdio> #include <a ...
- DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad
题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...
- 水题 Codeforces Round #306 (Div. 2) A. Two Substrings
题目传送门 /* 水题:遍历一边先找AB,再BA,再遍历一边先找BA,再AB,两种情况满足一种就YES */ #include <cstdio> #include <iostream ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #306 (Div. 2) E. Brackets in Implications 构造
E. Brackets in Implications Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- Codeforces Round #306 (Div. 2) D. Regular Bridge 构造
D. Regular Bridge Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/550/pro ...
- Codeforces Round #306 (Div. 2)A B C D 暴力 位/暴力 暴力 构造
A. Two Substrings time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
随机推荐
- Convert.ToString和ToString的区别
Convert.ToString能处理字符串为null的情况,不抛出异常. ToString方法不能处理字符串为null的情况,会抛出异常.如:“未将对象引用设置到对象的实例”.
- VS2015下的Android开发系列01——开发环境配置及注意事项
概述 VS自2015把Xamarin集成进去后搞Android开发就爽了,不过这安装VS2015完成的时候却是长了不知道多少.废话少说进正题,VS2015安装时注意把Android相关的组件勾选安装, ...
- mysql查询结果中文显示成了问号
在mysql的配置文件my.ini中的[mysqld]项中加这两句 character-set-server = utf8 collation-server = utf8_general_ci 在任务 ...
- BZOJ 3993 [SDOI 2015] 星际战争 解题报告
首先我们可以二分答案. 假设当前二分出来的答案是 $Ans$ ,那么我们考虑用网络流检验: 设武器为 $X$,第 $i$ 个武器的攻击力为 $B_i$: 设机器人为 $Y$,第 $i$ 个机器人的装甲 ...
- Java集合框架的知识总结(1)
说明:先从整体介绍了Java集合框架包含的接口和类,然后总结了集合框架中的一些基本知识和关键点,并结合实例进行简单分析. 1.综述 所有集合类都位于java.util包下.集合中只能保存对象(保存对象 ...
- easyui源码翻译1.32--Tree(树)
前言 使用$.fn.tree.defaults重写默认值对象.下载该插件翻译源码 树控件在web页面中一个将分层数据以树形结构进行显示.它提供用户展开.折叠.拖拽.编辑和异步加载等功能. 源码 /** ...
- /MD, /MDD, /ML, /MT,/MTD(使用运行时库)
1. VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd) 2. C 运行时库 ...
- 【原创】FPGA开发手记(二) VGA接口
以下内容均以Xilinx的Nexys3作为开发板 1.VGA接口介绍 首先,先看电路图(3*5为例): 标准VGA一共15个接口,但是实际应用的接口信号只用五个:HSYNC,行同步信号:VSYNC,场 ...
- 【HDOJ】1285 确定比赛名次
拓扑排序,居然要考虑重边,使用STL实现. #include <iostream> #include <cstdio> #include <cstring> #in ...
- Hadoop家族学习路线图
主要介绍Hadoop家族产品,常用的项目包括Hadoop, Hive, Pig, HBase, Sqoop, Mahout, Zookeeper, Avro, Ambari, Chukwa,新增加的项 ...