Codeforces Round#310 div2
C题:这题说的是套娃,如果做题的时候知道是套娃,那就好理解多了
规则1:套娃A可以放到套娃B里面,当且仅当套娃B没有放在其他套娃里面
规则2:套娃A放在套娃B里面,且套娃B没有放在其他套娃里面,那么可以把A从B中拿出来
问我们最少要操作多少次,才能将套娃全部套起来,拆开和组装都算是一次操作
思路:找到序号为1的套娃的哪一组,然后统计该组有多少个套娃是连在1后面,且每次序号都是加1的,那么这些个套娃是不用拆开的。那么剩下的套娃都是要拆开的
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/* */
int a[ + ];
int main()
{ int n, k, m;
int ans;
int cnt;
while (scanf("%d%d", &n, &k) != EOF)
{
ans = cnt = ;
for (int i = ; i < k; ++i)
{
scanf("%d", &m);
bool flag = false;
for (int j = ; j < m; ++j)
{
scanf("%d", &a[j]);
if (a[j] == )
{
flag = true;
}
} if (flag)
{
cnt = ;
for (int j = ; j < m; ++j)
{
if (a[j] - == a[j - ])
cnt++;
else
break;
}
ans += m - cnt;
flag = false;
}
else
ans += m - ;
}
ans += n - cnt;
printf("%d\n", ans);
}
return ;
}
D题:当时的想法是把岛屿按距离下一个的岛屿的距离,从小到大排序, 并且将桥的长度也从小到大排序,然后进行贪心, 但是问题是
可能贪心的时候,选择了当前的桥A,但是桥B也时候自己(桥A排在桥B前面), 但是呢,到下一个岛屿的时候,桥B对它来说,太长了。
例子:三个岛屿,两座桥,按上面那样子贪心是不行的。
1 10
11 16
20 23
10 15
上面的贪心策略,没有考虑到的信息是岛屿自身的长度。仅考虑了岛屿与岛屿之间的距离
我们规定上界是:两个岛屿之间能建立的最长的桥, 下界是:最短的桥
我们可以将岛屿按照上界进行排序,然后选择桥的时候,找到第一个大于等于下界的桥即可。这样就不会发生上面所说的那种情况(因为上界是递增的)
还有一点就是STL的lower_bound()函数极端情况下,时间复杂度是O(n),会超时
可以将桥的长度存到set中,然后用set自带的lower_bound()函数, 时间复杂度是O(logn)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = << ;
/* */
const int N = + ;
struct Island
{
LL l, r, upper, lower, id;
bool operator<(const Island&rhs)const
{
return upper < rhs.upper;
}
}island[N];
set<pair<LL, int> > b;
int ans[N];
int main()
{
int n, m;
LL a;
scanf("%d%d", &n, &m);
for (int i = ; i < n; ++i)
{
scanf("%I64d%I64d", &island[i].l, &island[i].r);
}
for (int i = ; i < n - ; ++i)
{
island[i].id = i;
island[i].lower = island[i + ].l - island[i].r;
island[i].upper = island[i + ].r - island[i].l;
}
sort(island, island + n - );
for (int i = ; i < m; ++i)
{
scanf("%I64d", &a);
b.insert(make_pair(a, i));
}
for (int i = ; i < n - ; ++i)
{
set<pair<LL, int> >::iterator iter = b.lower_bound(make_pair(island[i].lower, ));
if (iter == b.end())
{
puts("No");
return ;
}
else
{
if (iter->first <= island[i].upper)
{
ans[island[i].id] = iter->second + ;
b.erase(iter);
}
else
{
puts("No");
return ;
}
}
}
puts("Yes");
printf("%d", ans[]);
for (int i = ; i < n - ; ++i)
printf(" %d", ans[i]);
puts("");
return ;
}
E题:
对于向上走的点,那么只有比自己大的x能影响到自己
对于向左走的点,那么只有比自己小的x能影响到自己
用set就行了。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <math.h>
using namespace std;
#pragma warning(disable:4996)
typedef long long LL;
const int INF = <<;
/*
6 5
3 4 U
6 1 L
5 2 U
4 3 U
2 5 U 如果向上走,那么只有比自己大的x[i]向左走能影响到自己
如果向左走,那么只有比自己小的x[i]向上走能影响到自己
*/
const int N = + ;
set<pair<int,int> > s;
set<pair<int, int> >::iterator it;
int x[N], y[N];
int main()
{
int n, q;
char d[];
scanf("%d%d", &n, &q);
s.insert(make_pair(, q+));
s.insert(make_pair(n + , q+));
x[] = y[] = ;
for (int i = ; i <= q; ++i)
{
scanf("%d%d%s", &x[i], &y[i], d);
if (d[] == 'U')
{
it = s.lower_bound(make_pair(x[i], -));
}
else
{
it = s.upper_bound(make_pair(x[i], q+));
//it-- , 找到比自己小的x
it--;
}
if (it->first == x[i])
{
printf("0\n");
continue;
}
s.insert(make_pair(x[i], i));
if (d[] == 'U')
{
printf("%d\n", y[i] - y[it->second]);
//影响是可以传递的
y[i] = y[it->second];
}
else
{
printf("%d\n", x[i] - x[it->second]);
x[i] = x[it->second];
}
}
return ;
}
目标:①div2做4题 ②一次AC,手速快,题意看清
Codeforces Round#310 div2的更多相关文章
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers
题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
- Codeforces Round #361 div2
ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...
- Codeforces Round #626 Div2 D,E
比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...
随机推荐
- android在eclipse中打包(签名包)方法及常见问题解决
打包成apk 右键单击项目名称,选择"Android Tools".再选择"Export Signed Application Package-",例如以下图所 ...
- 4.Swift教程翻译系列——Swift基本运算符
英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 运算符是指一个特殊的符号,能够用来查看.更改值或者相加.比方说加法运算符+能够讲 ...
- Swift - 环形进度条(UIActivityIndicatorView)的用法
Swift中,除了条形进度条外,还有环形进度条,效果图如下: 1,环形进度条的基本属性 (1)Style: Large White:比较大的白色环形进度条 White:白色环形进度条 Gray:灰色环 ...
- java中subString、split、stringTokenizer三种截取字符串方法的性能比较(转)
最近在阅读java.lang下的源码,读到String时,突然想起面试的时候曾经被人问过:都知道在大数据量情况下,使用String的split截取字符串效率很低,有想过用其他的方法替代吗?用什么替代? ...
- JavaScript权威指南科20章 client记忆
20 client记忆 client几种形式存储的: web记忆 cookie IE userData 离线应用 web数据库 文件系统api 20.1 localStorage 和 sessionS ...
- 编写自己的单点登录(SSO)服务
王昱 yuwang881@gmail.com 博客地址http://yuwang881.blog.sohu.com 摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统其中.本文从 ...
- ThinkPhp学习05
原文:ThinkPhp学习05 一.ThinkPHP 3 的CURD介绍 (了解)二.ThinkPHP 3 读取数据 (重点) 对数据的读取 Read $m=new Model('User') ...
- Andy's First Dictionary
Description Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy ...
- 一个Java对象到底占多大内存?(转)
最近在读<深入理解Java虚拟机>,对Java对象的内存布局有了进一步的认识,于是脑子里自然而然就有一个很普通的问题,就是一个Java对象到底占用多大内存? 在网上搜到了一篇博客讲的非常好 ...
- 基于Predictive Parsing的ABNF语法分析器(十三)——rulelist、rule、rulename、define-as和elements
我们来看看rulelist,它是整个ABNF文法的入口,就是说一个ABNF文法就是一个规则列表rulelist.一个rulelist由若干个rule规则组成,每个rule由规则名rulename.定义 ...