Codeforces Round #546 (Div. 2) ABCDE 题解
codeforces 1136A:
题意:一本书有n个章节,每个章节的分别在li到ri页,小明读完书后将书折在第k页,问还有多少章节没有读
题解:控制k在li~ri的范围内后输出n-i即可
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
LL read() {
int x = , f = ; char ch = getchar();
while(ch < '' || ch > '') {
if(ch == '-')f = -;
ch = getchar();
}
while(ch >= '' && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x * f;
}
const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
int l[maxn];
int r[maxn];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, k;
scanf("%d", &n);
for(int i = ; i < n; i++) {
scanf("%d%d", &l[i], &r[i]);
}
scanf("%d", &k);
for(int i = ; i < n; i++) {
if(l[i] <= k && k <= r[i]) {
cout << n - i << endl;
return ;
}
}
return ;
}
codeforces 1136B:
题意:有n个井盖,井盖上有一颗石头,你每次可以做以下三种操作中的一种
1.将石头丢在隔壁的井盖上
2.走向相邻的井盖
3.如果井盖上没有石头,将井盖打开
你初始时在第k个井盖上,问你最少需要多少次操作可以打开全部的井盖
题解:每个井盖都需要进行3次操作才能到这个井盖上,首先我们需要到这个井盖上,然后我们需要搬石头,再然后我们要打开井盖,但是我们会走重复的路径,所以要想操作数最少,我们应该走重复的路最少,即min(n-k,k-1)
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
LL read() {
int x = , f = ; char ch = getchar();
while(ch < '' || ch > '') {
if(ch == '-')f = -;
ch = getchar();
}
while(ch >= '' && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x * f;
}
const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f; int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n,k;
scanf("%d%d", &n, &k);
cout << min(k - , n - k) + * n << endl;
return ;
}
codeforces 1136C:
题意:给你一个矩阵A和一个矩阵B,问你矩阵A和矩阵B是否是同类矩阵(转置矩阵也是同类的
题解:记录一下两个矩阵对角线上的元素是否相等即可
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
LL read() {
int x = , f = ; char ch = getchar();
while(ch < '' || ch > '') {
if(ch == '-')f = -;
ch = getchar();
}
while(ch >= '' && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x * f;
}
const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
int a[][];
int b[][];
vector<int> vec1[], vec2[];
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
scanf("%d", &b[i][j]);
}
}
for(int i = ; i < n; i++) {
for(int j = ; j < m; j++) {
vec1[i + j].push_back(a[i][j]);
vec2[i + j].push_back(b[i][j]);
}
}
for(int i = ; i < n + m - ; i++) {
sort(vec1[i].begin(), vec1[i].end());
sort(vec2[i].begin(), vec2[i].end());
if(vec1[i] != vec2[i]) {
cout << "NO" << endl;
return ;
}
}
cout << "YES" << endl;
return ;
}
codeforces 1136D:
题意:有一队人,其中有n对人可以两两交换,你在队伍的尾部,问你你最多可以向前移动多少步
题解:用一个vector记录你可以交换的人,然后判断你是否能与vector里面的人进行交换,如果你可以与vector里面所有的人交换的话,你就可以移动一步
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
LL read() {
int x = , f = ; char ch = getchar();
while(ch < '' || ch > '') {
if(ch == '-')f = -;
ch = getchar();
}
while(ch >= '' && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x * f;
}
const double eps = 1e-;
const int mod = 1e9 + ;
const int maxn = 2e6 + ;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3f;
set<pair<int, int> > s;
int a[maxn];
vector<int> vec;
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n, m;
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
}
vec.push_back(a[n]);
for(int i = , u, v; i < m; i++) {
scanf("%d%d", &u, &v);
s.insert(make_pair(u, v));
}
int ans = ;
for(int i = n - ; i >= ; i--) {
int flag = ;
for(int j = ; j < vec.size(); j++) {
if(s.count(make_pair(a[i], vec[j])) == ) {
flag = ;
break;
}
}
if(flag) {
ans++;
} else {
vec.push_back(a[i]);
}
} cout << ans << endl;
return ;
}
codeforces 1136E:
题意:给你一个长度为n的序列a和长度为n-1的序列k,序列a在任何时候都满足如下性质,a[i+1]>=ai+ki,如果更新后a[i+1]<ai+ki了,那么a[i+1]=ai+ki
现在给你q次操作
操作1:将位置为pos的元素+x
操作2:询问区间l,r的区间和
题解:非常明显的线段树题,我们不好维护的是,如果更新后,当前数字变大到不满足限制条件时,我后面的元素也要做出相应的更新
那么我们就将a序列先减去k序列,这样的a序列也是满足限制条件了,然后我们记录下k的前缀和的前缀和,避免询问时缺少k的贡献,
数学推导如下
序列a满足单调不减性,
则∑ai 同样满足单调不减性,
当我们对位置为pos的元素进行更新时,
如果后面的元素 a[pos+R]<a[pos],则该元素要被覆盖,
所以我们二分右端点,将区间【pos,R】覆盖为a[pos]+x即可
为了避免重复计算ki对a的贡献所以我们覆盖区间时可以用如下技巧
用c来记录k的前缀和的前缀和
每次覆盖时,我们将区间【l,r】覆盖为(a[pos]+x-k[pos])*(r-l+1)+c[r+1]-c[l]
这样就不会使得k的贡献计算错了
#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"
#define debug3(x,y,z) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<" "<<#z<<" "<<z<<"]\n"
LL read() {
int x = , f = ; char ch = getchar();
while(ch < '' || ch > '') {
if(ch == '-')f = -;
ch = getchar();
}
while(ch >= '' && ch <= '') {
x = x * + ch - '';
ch = getchar();
}
return x * f;
}
const int maxn = 2e5 + ;
LL sum[maxn << ];
LL lazy[maxn << ];
LL a[maxn];
LL k[maxn];
LL sub[maxn];
LL c[maxn];
LL NUL = -1e18;
void push_up(int rt) {
sum[rt] = (LL)sum[rt << ] + sum[rt << | ];
}
void push_down(int l, int r, int rt) {
if(lazy[rt] == NUL) return;
int mid = (l + r) >> ;
lazy[rt << ] = lazy[rt];
lazy[rt << | ] = lazy[rt];
sum[rt << ] = (LL)(mid - l + ) * lazy[rt] + c[mid + ] - c[l];
sum[rt << | ] = (LL)(r - mid) * lazy[rt] + c[r + ] - c[mid + ];
lazy[rt] = NUL;
}
void build(int l, int r, int rt) {
lazy[rt] = NUL;
if(l == r) {
sum[rt] = a[l];
return;
}
int mid = (l + r) >> ;
build(l, mid, rt << );
build(mid + , r, rt << | );
push_up(rt);
}
void update(int L, int R, LL val, int l, int r, int rt) {
if(L <= l && r <= R) {
sum[rt] = (LL)(r - l + ) * val + c[r + ] - c[l];
lazy[rt] = val;
return;
}
int mid = (l + r) >> ;
push_down(l, r, rt);
if(L <= mid) update(L, R, val, l, mid, rt << );
if(R > mid) update(L, R, val, mid + , r, rt << | );
push_up(rt);
}
LL query(int L, int R, int l, int r, int rt) {
if(L <= l && r <= R) {
//debug2(rt,sum[rt]);
return sum[rt];
}
int mid = (l + r) >> ;
push_down(l, r, rt);
LL ans = ;
if(L <= mid) ans += query(L, R, l, mid, rt << );
if(R > mid) ans += query(L, R, mid + , r, rt << | );
return ans;
}
int main() {
#ifndef ONLINE_JUDGE
FIN
#endif
int n;
scanf("%d", &n);
for(int i = ; i < n; i++) {
scanf("%lld", &a[i]);
}
for(int i = ; i < n; i++) {
scanf("%lld", &k[i]);
k[i] = k[i] + k[i - ];
}
for(int i = ; i <= n; i++) {
c[i] = c[i - ] + k[i - ];
}
build(, n - , );
int q;
cin >> q; while(q--) {
char op[];
int l, r, pos;
LL val;
cin >> op; if(op[] == '+') {
cin >> pos >> val;
pos--;
int l = pos;
int r = n - ;
LL tmp = query(pos, pos, , n - , );
while(l < r) {
int mid = (l + r + ) / ;
if(tmp + val + k[mid] - k[pos] > query(mid, mid, , n - , )) {
l = mid;
} else {
r = mid - ;
}
}
// debug3(pos, r, tmp + val - k[pos]);
update(pos, r, tmp + val - k[pos], , n - , );
} else {
cin >> l >> r;
cout << query(l - , r - , , n - , ) << endl;;
// printf("%lld\n", query(l - 1, r - 1, 0, n - 1, 1));
}
}
}
Codeforces Round #546 (Div. 2) ABCDE 题解的更多相关文章
- Codeforces Round #460 (Div. 2) ABCDE题解
原文链接http://www.cnblogs.com/zhouzhendong/p/8397685.html 2018-02-01 $A$ 题意概括 你要买$m$斤水果,现在有$n$个超市让你选择. ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- Codeforces Round #546 (Div. 2) 题解
Codeforces Round #546 (Div. 2) 题目链接:https://codeforces.com/contest/1136 A. Nastya Is Reading a Book ...
- Codeforces Round #261 (Div. 2)[ABCDE]
Codeforces Round #261 (Div. 2)[ABCDE] ACM 题目地址:Codeforces Round #261 (Div. 2) A - Pashmak and Garden ...
- # Codeforces Round #529(Div.3)个人题解
Codeforces Round #529(Div.3)个人题解 前言: 闲来无事补了前天的cf,想着最近刷题有点点怠惰,就直接一场cf一场cf的刷算了,以后的题解也都会以每场的形式写出来 A. Re ...
- Codeforces Round #557 (Div. 1) 简要题解
Codeforces Round #557 (Div. 1) 简要题解 codeforces A. Hide and Seek 枚举起始位置\(a\),如果\(a\)未在序列中出现,则对答案有\(2\ ...
- Codeforces Round #540 (Div. 3) 部分题解
Codeforces Round #540 (Div. 3) 题目链接:https://codeforces.com/contest/1118 题目太多啦,解释题意都花很多时间...还有事情要做,就选 ...
- Codeforces Round #538 (Div. 2) (A-E题解)
Codeforces Round #538 (Div. 2) 题目链接:https://codeforces.com/contest/1114 A. Got Any Grapes? 题意: 有三个人, ...
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
随机推荐
- 关于购买Redis服务器:腾讯云、阿里云还是华为云?
个人分类: redis使用 编辑 新年伊始,很多商家都开始进行新年产品大促销,在分布是缓存Redis领域,几家大公司也是打得如火如荼,各有千秋啊. 现在市场上比较有口碑的商家有腾讯云.阿里云.华为云三 ...
- 算法笔记(c++)-使用递归函数逆序一个栈
---恢复内容开始--- 使用递归函数逆序一个栈 题目:使用递归函数,不借助其他数据结构逆序一个栈. 我的思路:使用递归函数保存栈中变量. 递归函数分两个,一个获取并移除栈底元素,另一个负责逆序.其实 ...
- Hyperledger Fabric中的Identity
Hyperledger Fabric中的Identity 什么是Identity 区块链网络中存在如下的角色:peers, orderers, client application, administ ...
- DCOM初步窥探二
1.COM进程透明性表现在“组件对象和客户程序可以拥有各自的空间,也可以共享同一个进程空间”. COM负责把客户的调用正确传到组件对象中,并保证参数传递的正确性. 组件对象和客户代码不必考虑调用传递的 ...
- 软件工程团队项目第一个Sprint评论
(1)跑男:话说我没怎么听懂这个游戏是怎么玩的,可能是由于这是第一组,所以我没有反应过来把,界面设计的还可以,但是像设置,选关,帮助真心没看懂.有一种感觉就是,这个游戏是由一堆的漂亮的图片拼起来的,还 ...
- POJ 2484(对称博弈)
题目链接:http://poj.org/problem?id=2484 这道题目大意是这样的,有n个硬币围成一圈,两个人轮流开始取硬币(假设他们编号从1到n),可以选择取一枚或者取相邻的两枚(相邻是指 ...
- oracle与DB2的一些架构
首先,我们需要理解 Oracle 使用的架构,并理解它与 DB2 的不同之处.图 1 展示了 Oracle 的系统结构.将该图与 图 2 进行比较,后者显示了 DB2 的系统结构.在阅读本文的时候,为 ...
- Swift-闭包使用及解决循环引用问题
Swift中闭包使用参考OC中block使用,基本一致 // 闭包类型 首先写(参数列表)->(返回值类型) func loadData(callBack : (jsonData:String) ...
- Redis的sentinel机制(sentinel节点IP为:192.168.23.10) “哨兵”
万一主节点打击,主从模型将会停止工作,为了解决这个问题,Redis提供了一个sentinel(哨兵),以此来实现主从切换的功能,一旦主节点宕机了,sentinel将会在从节点中挑一个作为主节点.与zo ...
- nginx 二进制安装
Nginx的安装方法 1:yum安装 默认是1.6版本,且在epel源中 2:源码包编译安装 源码下载:http://nginx.org/en/download.html,下载1.8稳定版本 ...