题解 Codeforces Round #569 (Div. 2)

rank:1306/11165 rate: +43 1424 → 1467

Codeforces Round #569 (Div. 2)

A. Alex and a Rhombus

热身题。解决这道题需要的知识:读懂题面

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; int n,cnt; inline int read();
inline ll readll(); int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif n=read(); if(n==1){
cout<<1<<endl;
return 0;
} for(int i=-(n-1);i<=(n-1);++i){
for(int j=-(n-1);j<=(n-1);++j){
if(abs(i)+abs(j)<=(n-1)) cnt++;
}
} cout<<cnt<<endl; return 0;
} inline int read(){
char tmp=getchar(); int sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return flag?-sum:sum;
} inline ll readll(){
char tmp=getchar(); ll sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return flag?-sum:sum;
}

B. Nick and Array

刚开始以为是什么牛批的数位dp,后来才发现是贪心。考场的时候自己尝试贪心了一下,但被system check hack掉了

再总结一下思路:首先为了绝对值最大,把所有的正数转换为整数;然后为了避免得到负数,倘若有奇数个负数,就将最大的那个取反

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; const int MAX=1e5+5,INF=0x3f3f3f3f; int n,minn,neg,pos;
int num[MAX]; inline int read();
inline ll readll(); int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif n=read();
for(int i=1;i<=n;++i) num[i]=read();
for(int i=1;i<=n;++i) {if(num[i]>=0) num[i]=-num[i]-1; if(num[i]<0) neg++;}
for(int i=1;i<=n;++i) {if(abs(num[i])>minn) minn=abs(num[i]),pos=i;} if(neg%2==1) num[pos]=-num[pos]-1; for(int i=1;i<=n;++i) printf("%d ",num[i]); return 0;
} inline int read(){
char tmp=getchar(); int sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return flag?-sum:sum;
} inline ll readll(){
char tmp=getchar(); ll sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return flag?-sum:sum;
}

C. Valeriy and Deque

有点忘了题面了..不过难度不大,好像是关于循环的。

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll; const int MAX=1e5+5,INF=0x3f3f3f3f; int n,q,tar,pos;
int a[MAX],line[MAX],rec[MAX][2]; deque <int> ord; inline int read();
inline ll readll(); int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif n=read(); q=read();
for(int i=1;i<=n;++i){
a[i]=read();
if(a[i]>tar) tar=a[i],pos=i;
} for(int i=1;i<=n;++i) ord.push_back(a[i]);
for(int i=1;i<=pos-1;++i){
int a=ord.front(); ord.pop_front();
int b=ord.front(); ord.pop_front();
if(a>b){
ord.push_front(a); ord.push_back(b);
rec[i][0]=a; rec[i][1]=b;
}
else{
ord.push_front(b); ord.push_back(a);
rec[i][0]=a; rec[i][1]=b;
}
}
ord.pop_front();
for(int i=1;i<n;++i){
line[i]=ord.front(); ord.pop_front();
} for(int k=1;k<=q;++k){
ll q=readll();
if(q<pos){
printf("%d %d\n",rec[q][0],rec[q][1]);
}
else{
q-=(pos-1);
q%=(n-1);
printf("%d %d\n",tar,line[q?q:n-1]);
}
} return 0;
} inline int read(){
char tmp=getchar(); int sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return flag?-sum:sum;
} inline ll readll(){
char tmp=getchar(); ll sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return flag?-sum:sum;
}

D. Tolik and His Uncle

刚读这道题的时候意为是某种神级搜索题。嗯?矢量?不可重复?状压?广搜?..总之考场一顿思索没有结果。题解:规律题 去您的规律..

感觉官方题解解释的就很清晰,附上地址

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int MAX=1e6+5; int n,m; inline int read(); int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif n=read(); m=read(); for(int j=1;j<=m/2;++j){
for(int i=1;i<=n;++i){
printf("%d %d\n%d %d\n",i,j,n-i+1,m-j+1);
}
}
if(m%2==1){
int l=1,r=n,tar=m/2+1;
while(l<=r){
if(l<=r) printf("%d %d\n",l,tar),l++;
if(l<=r) printf("%d %d\n",r,tar),r--;
}
} return 0;
} inline int read(){
char tmp=getchar(); int sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return sum;
}

E. Serge and Dining Room

线段树的玄学题。到现在为止虽然通过了,但个人感觉对正解理解非常不透彻,还是说这道题就是这么的迷?

贪心的发现:同学的顺序没有卵用。在保证钱的种类数和个数一定时,答案是唯一的

考虑一下“菜i可不可以被购买”和“价格大于等于i的菜个数减去钱大于等于i的同学数”之间的关系

菜i可以被购买

那么一定满足价格大于等于i的菜个数大于钱大于等于i的同学数

菜i不可以被购买

那么一定不满足价格大于等于i的菜个数大于钱大于等于i的同学数 吗..?

非也。

!这就是一个菜不可以被购买,但是却满足差大于等于1的情况。那该怎么办..

考量一下,发现这种情况出现的必要条件就是右侧存在答案更优的菜。也就是说类似于二分答案的单调性。

维护权值线段树,遇到菜则价格处+1,遇到同学则价格处-1

我们用线段树维护一下每一个节点的后缀和,并为每一个区间维护一个max。不断经可能向右取max大于等于1的菜,即有可能成为答案。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ST segment_tree:: const int MAX=4000005,TOP=1000000; namespace segment_tree{
int pre[MAX],sum[MAX],lazy[MAX],maxx[MAX]; void build(int p,int l,int r){
if(l==r) {sum[p]=maxx[p]=pre[l]; return;}
int mid=(l+r)>>1;
build(p<<1,l,mid); build(p<<1|1,mid+1,r);
sum[p]=sum[p<<1]+sum[p<<1|1];
maxx[p]=max(maxx[p<<1],maxx[p<<1|1]);
} void add(int p,int l,int r,int del){
sum[p]+=(r-l+1)*del;
lazy[p]+=del;
maxx[p]+=del;
} void pushdown(int p,int l,int r){
int mid=(l+r)>>1;
add(p<<1,l,mid,lazy[p]);
add(p<<1|1,mid+1,r,lazy[p]);
lazy[p]=0;
} void modify(int p,int l,int r,int L,int R,int del){
if(L<=l&&r<=R) return add(p,l,r,del);
int mid=(l+r)>>1;
pushdown(p,l,r);
if(L<=mid) modify(p<<1,l,mid,L,R,del);
if(mid+1<=R) modify(p<<1|1,mid+1,r,L,R,del);
sum[p]=sum[p<<1]+sum[p<<1|1];
maxx[p]=max(maxx[p<<1],maxx[p<<1|1]);
} int qsum_sum(int p,int l,int r,int L,int R){
if(L<=l&&r<=R) return sum[p];
pushdown(p,l,r);
int mid=(l+r)>>1,ans=0;
if(L<=mid) ans+=qsum_sum(p<<1,l,mid,L,R);
if(mid+1<=R) ans+=qsum_sum(p<<1|1,mid+1,r,L,R);
return ans;
} int query(int p,int l,int r){
if(l==r) return l;
pushdown(p,l,r);
int mid=(l+r)>>1;
if(maxx[p<<1|1]>=1) return query(p<<1|1,mid+1,r);
else if(maxx[p<<1]>=1) return query(p<<1,l,mid);
else return -1;
}
} int n,m,q;
int a[TOP],b[TOP]; inline int read(); int main(){
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif n=read(); m=read();
for(int i=1;i<=n;++i) a[i]=read(),ST pre[a[i]]++;
for(int i=1;i<=m;++i) b[i]=read(),ST pre[b[i]]--;
for(int i=TOP;i>=1;--i) ST pre[i]=ST pre[i]+ST pre[i+1];
ST build(1,1,TOP); q=read();
for(int i=1;i<=q;++i){
int type,pos,x; type=read(); pos=read(); x=read();
switch(type){
case 1:
ST modify(1,1,TOP,1,a[pos],-1);
ST modify(1,1,TOP,1,x,1);
a[pos]=x;
printf("%d\n",ST query(1,1,TOP));
break;
case 2:
ST modify(1,1,TOP,1,b[pos],1);
ST modify(1,1,TOP,1,x,-1);
b[pos]=x;
printf("%d\n",ST query(1,1,TOP));
break;
}
} return 0;
} inline int read(){
char tmp=getchar(); int sum=0; bool flag=false;
while(tmp<'0'||tmp>'9'){
if(tmp=='-') flag=true;
tmp=getchar();
}
while(tmp>='0'&&tmp<='9'){
sum=(sum<<1)+(sum<<3)+tmp-'0';
tmp=getchar();
}
return flag?-sum:sum;
}

Codeforces Round #569 (Div. 2)的更多相关文章

  1. Codeforces Round #569 (Div. 2)A. Alex and a Rhombus

    A. Alex and a Rhombus 题目链接:http://codeforces.com/contest/1180/problem/A 题目: While playing with geome ...

  2. Codeforces Round #569 (Div. 2) C. Valeriy and Deque

    链接: https://codeforces.com/contest/1180/problem/C 题意: Recently, on the course of algorithms and data ...

  3. Codeforces Round #569 (Div. 2) B. Nick and Array

    链接: https://codeforces.com/contest/1180/problem/B 题意: Nick had received an awesome array of integers ...

  4. Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle

    A. Alex and a Rhombus time limit per test1 second memory limit per test256 megabytes inputstandard i ...

  5. Codeforces Round #569 Div. 1

    A:n-1次操作后最大值会被放到第一个,于是暴力模拟前n-1次,之后显然是循环的. #include<bits/stdc++.h> using namespace std; #define ...

  6. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  7. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  8. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  9. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  10. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

随机推荐

  1. 齐博x1标签之无刷新显示更多

    示范代码如下: <div class="ListMoreInfos"> {qb:tag name="news_list_page_listdata02&quo ...

  2. 11.mixins混合类

      一.混合类(mixins) 使用基于类的视图,最大的优势之一就创建可复用的代码 我们编写的非常类似的代码,可以抽象出来,将这部分代码放到mixin类系列中,然后作为父类提供子类继承使用 from ...

  3. 京东云开发者| Redis数据结构(二)-List、Hash、Set及Sorted Set的结构实现

    1 引言 之前介绍了Redis的数据存储及String类型的实现,接下来再来看下List.Hash.Set及Sorted Set的数据结构的实现. 2 List List类型通常被用作异步消息队列.文 ...

  4. 事件循环Event Loop

    在 事件循环 期间的某个时刻,运行时会从最先进入队列的消息开始处理队列中的消息.被处理的消息会被移出队列,并作为输入参数来调用与之关联的函数.正如前面所提到的,调用一个函数总是会为其创造一个新的栈帧. ...

  5. 题解 CF630L Cracking the Code

    前言 为什么没有人暴力快速幂啊,Ta不香嘛/kel 题意 设读入为 \(abcde\) ,求 \(acedb^5\mod{10^5}\) 的结果. \(\sf {Solution}\) 显然暴力啊. ...

  6. 28分钟学会Linux三剑客

    把简单的技术学到极致就是高手. 前言 Linux三剑客指的是grep.sed以及awk命令的使用,这三个命令功能异常强大,大到没朋友.grep命令主打"查找",sed命令主打&qu ...

  7. selenium被某些网页检测不允许正常访问、登录等,解决办法

    网站通过什么方式检测 function b() { return "$cdc_asdjflasutopfhvcZLmcfl_"in u || d.webdriver } 通过上方的 ...

  8. [排序算法] 堆排序 (C++)

    堆排序解释 什么是堆 堆 heap 是一种近似完全二叉树的数据结构,其满足一下两个性质 1. 堆中某个结点的值总是不大于(或不小于)其父结点的值: 2. 堆总是一棵完全二叉树 将根结点最大的堆叫做大根 ...

  9. C温故补缺(十三):可变参数

    可变参数 stdarg.h 头文件提供了实现可变参数功能的函数和宏.具体步骤如下: 定义一个函数,最后一个参数为省略号,省略号前面可以设置自定义参数,一般传入参数的个数. int func(int n ...

  10. Spring Security(6)

    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来- Spring Security使用MySQL保存cookie记录虽然方便,但是目前更多的主流互联网应用都是用NoSQL来保存非业务数据的,Spr ...