浙江省赛C.Array in the Pocket(贪心+线段树)
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4102
题意:
给出一个长度为n的数组,重排它们,让相同位置的数不同,并且字典序最小
数据范围:
$1 \le n \le 10^5$
$1 \le a_i \le n$
分析:
不可能构造的情况:
- 有一个数的数量大于$n$的一半
可以构造的情况:
- 当前位置一定要填$x$,如果后面的坑位数量等于剩余的非$x$数量(线段树维护)
- 填第一小的数或者第二小的数(set维护)
ac代码:
#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
using namespace std;
const int maxn=1e5+10;
const int maxm=1e7+10;
const int mod=1e9+7;
set<int>se;
pa tree[maxn*4];
int lazy[maxn*4],num[maxn],book[maxn],n;
int see(int x)
{
set<int>::iterator i=se.begin();
if(x==2)i++;
return (*i);
}
void build(int st,int en,int rt)
{
if(st==en)
{
if(num[st])
tree[rt]=make_pair(n-book[st]*2,st);
else
tree[rt]=make_pair(1e9,st);
return ;
}
int md=(st+en)/2;
build(st,md,rt*2);
build(md+1,en,rt*2+1);
tree[rt]=min(tree[rt*2],tree[rt*2+1]);
}
void updata(int l,int r,int x,int st,int en,int rt)
{
if(r<st||l>en)
return ;
if(l<=st&&r>=en)
{
tree[rt].first+=x;
lazy[rt]+=x;
return ;
}
if(lazy[rt])
{
tree[rt*2].first+=lazy[rt];
tree[rt*2+1].first+=lazy[rt];
lazy[rt*2]+=lazy[rt];
lazy[rt*2+1]+=lazy[rt];
lazy[rt]=0;
}
int md=(st+en)/2;
updata(l,r,x,st,md,rt*2);
updata(l,r,x,md+1,en,rt*2+1);
tree[rt]=min(tree[rt*2],tree[rt*2+1]);
}
void intit()
{
memset(lazy,0,sizeof(lazy));
for(int i=1;i<=n;i++)book[i]=0;
}
void del(int x)
{
book[x]--;
if(book[x]==0)se.erase(x);
updata(1,n,-1,1,n,1);
updata(x,x,1,1,n,1);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
intit();
for(int i=1; i<=n; i++)
{
int x;
scanf("%d",&x);
book[x]++;
num[i]=x;
}
build(1,n,1);
if(tree[1].first<0)
{
printf("Impossible\n");
continue;
}
for(int i=1;i<=n;i++)
if(book[i])se.insert(i);
for(int i=1; i<=n; i++)
{
updata(num[i],num[i],1,1,n,1);
//int g=se(1);
if(tree[1].first==0)
{
printf("%d",tree[1].second);
del(tree[1].second);
}
else
{
int g=see(1);
if(g==num[i])g=see(2);
printf("%d",g);
del(g);
}
if(i==n)printf("\n");
else printf(" ");
}
}
return 0;
}
浙江省赛C.Array in the Pocket(贪心+线段树)的更多相关文章
- BZOJ4391 High Card Low Card [Usaco2015 dec](贪心+线段树/set库
正解:贪心+线段树/set库 解题报告: 算辣直接甩链接qwq 恩这题就贪心?从前往后从后往前各推一次然后找一遍哪个地方最大就欧克了,正确性很容易证明 (这里有个,很妙的想法,就是,从后往前推从前往后 ...
- 2018 ACMICPC上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 H - A Simple Problem with Integers (线段树,循环节) 链接:https://ac.nowcoder.co ...
- 【题解】P1712 [NOI2016]区间(贪心+线段树)
[题解]P1712 [NOI2016]区间(贪心+线段树) 一个observe是,对于一个合法的方案,将其线段长度按照从大到小排序后,他极差的来源是第一个和最后一个.或者说,读入的线段按照长度分类后, ...
- UVALive 8519 Arrangement for Contests 2017西安区域赛H 贪心+线段树优化
题意 等价于给一个数列,每次对一个长度为$K$的连续区间减一 为最多操作多少次 题解: 看样例猜的贪心,10分钟敲了个线段树就交了... 从1开始,找$[i,i+K]$区间的最小值,然后区间减去最小值 ...
- Codeforces 675E Trains and Statistic(DP + 贪心 + 线段树)
题目大概说有n(<=10W)个车站,每个车站i卖到车站i+1...a[i]的票,p[i][j]表示从车站i到车站j所需买的最少车票数,求所有的p[i][j](i<j)的和. 好难,不会写. ...
- poj 2010 Moo University - Financial Aid (贪心+线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 骗一下访问量.... 题意大概是:从c个中选出n个 ...
- Codeforces 626G Raffles(贪心+线段树)
G. Raffles time limit per test:5 seconds memory limit per test:256 megabytes input:standard input ou ...
- BZOJ1828[USACO 2010 Mar Gold 2.Barn Allocation]——贪心+线段树
题目描述 输入 第1行:两个用空格隔开的整数:N和M * 第2行到N+1行:第i+1行表示一个整数C_i * 第N+2到N+M+1行: 第i+N+1行表示2个整数 A_i和B_i 输出 * 第一行: ...
- BZOJ 5249: [2018多省省队联测]IIIDX(贪心 + 线段树)
题意 这一天,\(\mathrm{Konano}\) 接到了一个任务,他需要给正在制作中的游戏 \(\mathrm{<IIIDX>}\) 安排曲目 的解锁顺序.游戏内共有\(n\) 首曲目 ...
随机推荐
- Earth Wind and Fire CodeForces - 1148E (构造)
大意: $n$个石子, 第$i$个石子初始位置$s_i$, 每次操作选两个石子$i,j$, 要求$s_i<s_j$, 任取$d$, 满足$0\le 2d\le s_j-s_i$, 将$s_i,s ...
- chartjs显示数值标签插件:chartjs-plugin-datalabels
Getting Started #Installation #npm npm install chartjs-plugin-datalabels --save This plugin can al ...
- gdb-example-ncurses
gdb-example-ncurses http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html 1. The Prob ...
- Graphics与Canvas
Graphics: 1. java.awt.Graphics;2.android.graphics Canvas:1.java.awt.Canvas;2.android.graphics.Canvas ...
- 使用display:table实现两列自适应布局
在张鑫旭大神那边看到的方法,我自己写了一遍,稍微添加了一些自己的风格特色. IE6/7不支持这个属性,从IE8开始支持这个属性,对于IE6/7可以用display:inline-block解决. ta ...
- java Spring boot项目简单说明
前言 一直从事.NET开发,但一直有种想去探索Java世界的冲动,今天终于有时间来尝试一下,以下是自己探索过程的简要记录. 一.开发工具 开发工具选用 IntelliJ IDEA社区版(免费),安装教 ...
- C++ STL 之 函数对象
重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象,也叫仿函数(functor),其实就是重载“()”操作符,使得类对象可以像函数那样调用.注意 ...
- Dubbo 配置参数
关闭启动检查 在dubbo多模块项目启动的时候为了并行启动多个服务,缩短启动时间,需要解除模块之间的依赖检测 dubbo.consumer.check=false @Reference(check = ...
- springboot集成websocket的两种实现方式
WebSocket跟常规的http协议的区别和优缺点这里大概描述一下 一.websocket与http http协议是用在应用层的协议,他是基于tcp协议的,http协议建立链接也必须要有三次握手才能 ...
- SpringCloud之Feign声明式调用原理及配置
1 什么是Feign Feign是一种声明式.模板化的HTTP客户端(仅在Application Client中使用).声明式调用是指,就像调用本地方法一样调用远程方法,无需感知操作远程http请求. ...