CodeForces Round #296 Div.2
A. Playing with Paper
如果a是b的整数倍,那么将得到a/b个正方形,否则的话还会另外得到一个(b, a%b)的长方形。
时间复杂度和欧几里得算法一样。
#include <iostream>
#include <cstdio>
using namespace std; //const int maxn = ; int main()
{
//freopen("in.txt", "r", stdin); long long a, b, ans = ;
scanf("%I64d%I64d", &a, &b);
while(a % b != )
{
ans += a / b;
long long ta = b;
long long tb = a % b;
a = ta; b = tb;
}
ans += a / b;
printf("%I64d\n", ans); return ;
}
代码君
B. Error Correct System
代码略矬。。
贪心,建一个有向图G[a][b] = i,代表第i个字符“想”从字母a变成字母b,也就是说s1[i] == a, s2[i] == b,如果s1中第i个字母从a变成b后,题中定义的那个Hamming distance就会减小1.如果有G[a][b] 和 G[b][a]的话,直接将这两个字符交换就好了,Hamming distance一共减少2。
否则,退而求其次,交换一下只满足G[a][b] G[b][c]的两个位置的字符,这样Hamming distance减小1.
再否则,,,直接输出-1 -1
#include <iostream>
#include <cstdio>
using namespace std; const int maxn = + ; int n;
char s1[maxn], s2[maxn]; int G[][]; int main()
{
//freopen("in.txt", "r", stdin); scanf("%d", &n); getchar();
gets(s1+); gets(s2+); int ans = , p1 = -, p2 = -;
for(int i = ; i <= n; i++) if(s1[i] != s2[i]) ans++; for(int i = ; i <= n; i++) if(s1[i] != s2[i])
{
int x = s1[i]-'a', y = s2[i]-'a';
G[x][y] = i;
if(G[y][x]) { p1 = i; p2 = G[y][x]; ans -= ; break; }
} bool flag = false;
if(p1 + p2 == -)
{
for(int i = ; i < ; i++)
{
for(int j = ; j < ; j++) if(G[i][j])
{
for(int k = ; k < ; k++) if(G[j][k])
{
flag = true;
ans--;
p1 = G[i][j]; p2 = G[j][k];
break;
}
if(flag) break;
}
if(flag) break;
}
} printf("%d\n%d %d\n", ans, p1, p2); return ;
}
代码君
C. Glass Carving
果然,熟练使用STL是很有必要的,减小编码复杂度,速度上也不会太慢。
话说有一个人对着一块玻璃横着或者竖着切来切去。
每次切完都要询问一下最大的一块玻璃的面积是多少。
将所有切割的位置放在一个集合中,还有所有相邻位置之间的距离放在一个多重集合里面。
每一次切割位置x,都将a<x<b这个区间分成了两个,同时将x加入到对应的切割位置集合;然后更新一下距离集合,具体就是删除b-a这段距离(如果有多个,只要删除一个就好了),然后分别插入(x-a) 和 (b-x)这两段被分隔开的距离。
#include <bits/stdc++.h>
using namespace std; int main()
{
//freopen("in.txt", "r", stdin); set<int> sh, sv;
multiset<int> mh, mv;
set<int>::iterator l, r; int w, h, n;
scanf("%d%d%d", &w, &h, &n); getchar();
sh.insert(); sh.insert(h);
sv.insert(); sv.insert(w);
mh.insert(h); mv.insert(w); while(n--)
{
char op; int cut;
scanf("%c %d", &op, &cut); getchar();
if(op == 'H')
{
l = sh.lower_bound(cut);
r = l; l--;
sh.insert(cut);
mh.insert(cut-(*l));
mh.insert((*r)-cut);
mh.erase(mh.find((*r) - (*l)));
}
else
{
l = sv.lower_bound(cut);
r = l; l--;
sv.insert(cut);
mv.insert(cut - (*l));
mv.insert((*r) - cut);
mv.erase(mv.find((*r) - (*l)));
} printf("%I64d\n", (long long)(*mh.rbegin()) * (*mv.rbegin()));
} return ;
}
代码君
最后再说一点题外话:这次是只要删除multiset中一个元素,只要S.erase(S.find(val))就行了。
如果我们想要删除所有multiset中值为val的元素该怎么办?
有一个equal_range的成员函数,调用S.equal_range(val)就会返回一个pair<multiset::iterator, multiset::iterator>类型的值ret,它代表了一个区间,这个区间中就包含了multiset中所有值为val的元素。最后删除的时候,直接S.erase(ret.first, ret.second)
下面是http://www.cplusplus.com/reference/set/multiset/equal_range/ 中的实例:
// multiset::equal_elements
#include <iostream>
#include <set> typedef std::multiset<int>::iterator It; // aliasing the iterator type used int main ()
{
int myints[]= {,,,,,};
std::multiset<int> mymultiset (myints, myints+); // 2 16 30 30 30 77 std::pair<It,It> ret = mymultiset.equal_range(); // ^ ^ mymultiset.erase(ret.first,ret.second); std::cout << "mymultiset contains:";
for (It it=mymultiset.begin(); it!=mymultiset.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; return ;
}
代码君
CodeForces Round #296 Div.2的更多相关文章
- Codeforces Round #296 (Div. 1) C. Data Center Drama 欧拉回路
Codeforces Round #296 (Div. 1)C. Data Center Drama Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xx ...
- Codeforces Round #296 (Div. 1) E. Triangles 3000
http://codeforces.com/contest/528/problem/E 先来吐槽一下,一直没机会进div 1, 马力不如当年, 这场题目都不是非常难,div 2 四道题都是水题! 题目 ...
- 字符串处理 Codeforces Round #296 (Div. 2) B. Error Correct System
题目传送门 /* 无算法 三种可能:1.交换一对后正好都相同,此时-2 2.上面的情况不可能,交换一对后只有一个相同,此时-1 3.以上都不符合,则不交换,-1 -1 */ #include < ...
- 水题 Codeforces Round #296 (Div. 2) A. Playing with Paper
题目传送门 /* 水题 a或b成倍的减 */ #include <cstdio> #include <iostream> #include <algorithm> ...
- Codeforces Round #296 (Div. 2) A. Playing with Paper
A. Playing with Paper One day Vasya was sitting on a not so interesting Maths lesson and making an o ...
- Codeforces Round #296 (Div. 2) A B C D
A:模拟辗转相除法时记录答案 B:3种情况:能降低2,能降低1.不能降低分别考虑清楚 C:利用一个set和一个multiset,把行列分开考虑.利用set自带的排序和查询.每次把对应的块拿出来分成两块 ...
- Codeforces Round #296 (Div. 1) B - Clique Problem
B - Clique Problem 题目大意:给你坐标轴上n个点,每个点的权值为wi,两个点之间有边当且仅当 |xi - xj| >= wi + wj, 问你两两之间都有边的最大点集的大小. ...
- Codeforces Round #296 (Div. 1) B. Clique Problem 贪心
B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #296 (Div. 1) A. Glass Carving Set的妙用
A. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- java集合类(二)List学习
接上篇 java集合类(一) List接口继承了Collection接口和Iterable接口,即同样含有Collection和 Iterable的特性,还有方法,其基本方法有: 1)有关添加: b ...
- Problem 1007 幸运数 线段树成段更新
题目链接: 题目 Problem 1007 幸运数 Time Limit: 2000 mSec Memory Limit : 131072 KB 问题描述 皮特的幸运数是2和5.只由幸运数字2和5组成 ...
- ASP.NET WebAPI2 发布之后404 Note Found
方法一:首先确保服务器安装.Net FrameWork 4.0 并且注册IIS 如果先安装.net framework4.0,再安装IIS,则会出现4.0的ISAPI没有注册的情况. 运行:C:\Wi ...
- 使用NodeJs,实现数据抓取
学习笔记 前言 近期做一个数据抓爬工具,最开始使用的是C#控制台应用,同时正则表达式去过滤数据,看着还行,可每次运行都依附于.net framework很是不爽,于是想整点其他的方法.本人还是比较喜欢 ...
- asp.net 获取客户机IP地址
/// <summary> ///get client IP /// </summary> /// <returns></returns> public ...
- 地图索引 R-tree
http://blog.csdn.net/v_JULY_v/article/details/6530142 984年,加州大学伯克利分校的Guttman发表了一篇题为“R-trees: a dynam ...
- 【leetcode】Add Two Numbers(middle) ☆
You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...
- GET和POST的区别,就是明信片和信封的区别
- URAL 1073 Square Country(DP)
题目链接 题意 :这个人要投资地,每块地都是正方形并且边长都是整数,他希望他要买的地尽量的少碎块.每买一块地要付的钱是边长的平方,而且会得到一个一份证书,给你一个钱数,让你求出能得到的证书个数. 思路 ...
- Centos安装桌面环境
刚开始装系统的时候,没有选Gnome或者KDE,现在想装个玩玩. 简单的安装可以参考这个:http://huruxing159.iteye.com/blog/744750 centos安装是是使用li ...