题解 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. golang中的锁竞争问题

    索引:https://www.waterflow.link/articles/1666884810643 当我们打印错误的时候使用锁可能会带来意想不到的结果. 我们看下面的例子: package ma ...

  2. Netty学习记录-入门篇

    你如果,缓缓把手举起来,举到顶,再突然张开五指,那恭喜你,你刚刚给自己放了个烟花. 模块介绍 netty-bio: 阻塞型网络通信demo. netty-nio: 引入channel(通道).buff ...

  3. 题解 UVA10285 最长的滑雪路径 Longest Run on a Snowboard

    Solution 双倍经验 就是记搜嘛. 搞一个二维数组记录一下当前的最长滑雪路径,其他和普通 dfs 没什么两样. 向 \(4\) 个方向搜索,如果高度符合就 \(+1\) . 多测要注意数组初始化 ...

  4. 从 Wepy 到 UniApp 变形记

    作者:vivo 互联网前端团队-Wan Anwen.Hu Feng.Feng Wei.Xie Tao 进入互联网"下半场",靠"人海战术"的研发模式已经不再具备 ...

  5. MAUI 初体验 联合 WinForm 让家里废弃的手机当做电脑副品用起来

    软件效果图 软件架构草图 效果解释:运行 winform 端后 使用 ctrl+c 先复制任何词语,然后ctrl+空格 就可以将翻译结果显示在 安卓,IOS,windows 甚至 mac 任意客户端 ...

  6. Jekyll于windows中使用

    安装 安装Ruby http://rubyinstaller.org/downloads/ 于其中选择最新的带dev套件的. 在安装时,安装目录不能有空格,检查是否已经安装成功 ruby -v gem ...

  7. ubuntu 基本指令

    系统相关 df: disk free 用以显示系统上文件系统磁盘的使用情况 # 以M/G单位显示硬盘空间大小 df -h apt: advanced packaging tool 包管理工具 apt ...

  8. JAVA缓存规范 —— 虽迟但到的JCache API与天生不俗的Spring Cache

    大家好,又见面了. 本文是笔者作为掘金技术社区签约作者的身份输出的缓存专栏系列内容,将会通过系列专题,讲清楚缓存的方方面面.如果感兴趣,欢迎关注以获取后续更新. 有诗云"纸上得来终觉浅,绝知 ...

  9. 关于model,modelsmanager,db以及phql之间关系的梳理

    摘要: model在前,db在model后面作为驱动支持.phql是phalcon自创的查询语言,主要特性是在sql语句中用模型名替代表名,其聪明地解析模型类,包括关联关系等,还支持参数绑定.mode ...

  10. 【iOS逆向】某茅台App算法分析还原

    1.目标 某茅台软件的actParam算法分析还原. 2.使用工具 mac系统 frida-ios-dump:砸壳 已越狱iOS设备:脱壳及frida调试 IDA Pro:静态分析 Charles:抓 ...