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 题意: 给定大 ...
随机推荐
- hdu1869 六度分离(Floyd)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1869 转载请注明出处:http://blog.csdn.net/u012860063?viewmode ...
- 在屏幕上建立ALV
在屏幕上创建两个文本元素空件.一个推出按钮控件.一个定制控制按钮 代码所示: *&------------------------------------------------------- ...
- javascript (八) 语法格式
JavaScript 对大小写敏感. JavaScript 对大小写是敏感的. 当编写 JavaScript 语句时,请留意是否关闭大小写切换键. 函数 getElementById 与 getEle ...
- vc 加载bmp位图并显示的方法
方法一.显示位图文件 HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,_T(“xxx.bmp”),Image_Bitmap,0,0,Lr_CreateDibSectio ...
- Atitit.jquery 版本号新特性attilax总结
Atitit.jquery 版本号新特性attilax总结 1. Jq1.4 1 2. 1.5 1 3. 1.6 3 4. Jq1.7 3 ⒉提升了事件委派时的性能有了大幅度的提升.尤其是在ie7下: ...
- Spring Bean范围 示例
Spring 该目的是通过默认单身创建的对象 设定Bean范围.由Bean美元Scope财产 Scope取值范围: Singleton:单例 proptotype:非单例 Request:创建该Bea ...
- 原型链(__proto__)
前面详细的解释了new的几个步骤,其中随意带过了一下原型链的概念,如果细读那篇文章,基本对原型也能有所理解. 原型有两个关键属性,一个是 __proto__ 一个是 prototype ,了解了这两个 ...
- timeout connect 10000 # default 10 second time out if a backend is not found
timeout connect <timeout> timeout contimeout <timeout> (deprecated) Set the maximum time ...
- [转]PHP 5.2~5.6 对照以及功能具体解释
[分享]PHP 5.2~5.6 对照以及功能具体解释 作者:流水理鱼wwek 来源:http://www.iamle.com/archives/1530.html 截至眼下(2014.2), PHP ...
- 响应VC++ 标题栏右边的关闭按钮“红叉”
击标题栏右边的关闭按钮“红叉”时,程序会向窗口发送WM_CLOSE消息,因此可以截取此消息在窗口关系前做一些提示或者是不允许点击时关闭程序 case WM_CLOSE: if (...) { Post ...