codeforces#1148E. Earth Wind and Fire(贪心)
题目链接:
http://codeforces.com/contest/1148/problem/E
题意:
给出两个长度为$n$的序列,将第一个序列变成第二个序列,顺序不重要,只需要元素完全相同即可
只有一种修改操作
- $a_i=a_i+d,a_j=a_j-d$满足 $a_j-a_i\geq 2 \cdot d$
数据范围:
$1\leq n\leq 3e^{5}$
分析:
先说结论:
- 对$a,b$数组排序,结果一定是$a_i$变成$b_i$,也就是一一对应的关系
- 有些数需要增加,有些数需要减少,第一个增加的数和第一个减少的数对应操作
- 如果(需要减少的数的目标位置)小于(需要增大的数的目标位置),那么就输出$NO$
在草稿纸上模拟几次就好了,$QAQ$
ac代码:
#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
using namespace std;
const int maxn=3e5+10;
struct Ans
{
int a,b,c;
}ans[maxn*5];
int cnt;
struct Move
{
int id,now,to;
bool operator <(const Move &a)const
{
if(to==a.to)return id<a.id;
return to<a.to;
}
};
pa a[maxn];
int b[maxn];
set<Move>se1,se2;
int main()
{
int n;
scanf("%lld",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].first);
a[i].second=i;
}
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+n);
for(int i=1;i<=n;i++)
{
//cout<<a[i].first<<" "<<b[i]<<endl;
if(a[i].first<b[i])
se1.insert((Move){a[i].second,b[i]-a[i].first,b[i]});
else if(a[i].first>b[i])
se2.insert((Move){a[i].second,a[i].first-b[i],b[i]});
//cout<<se1.size()<<" "<<se2.size()<<endl;
}
while(1)
{
//cout<<se1.size()<<" "<<se2.size()<<endl;
//cout<<"sadf0"<<endl;
if(se1.size()==0&&se2.size()==0)break;
if(se1.size()==0||se2.size()==0)
{
printf("NO\n");
return 0;
}
Move x=(*se1.begin());
Move y=(*se2.begin());
if(x.to>y.to)
{
printf("NO\n");
return 0;
}
// cout<<x.id<<" "<<y.id<<endl;
se1.erase(x);
se2.erase(y);
if(x.now==y.now)
{
ans[++cnt]=(Ans){x.id,y.id,x.now};
}
else if(x.now<y.now)
{
ans[++cnt]=(Ans){x.id,y.id,x.now};
y.now-=x.now;
se2.insert(y);
}
else if(x.now>y.now)
{
ans[++cnt]=(Ans){x.id,y.id,y.now};
x.now-=y.now;
se1.insert(x);
}
}
printf("YES\n");
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++)
printf("%d %d %d\n",ans[i].a,ans[i].b,ans[i].c);
return 0;
}
codeforces#1148E. Earth Wind and Fire(贪心)的更多相关文章
- Codeforces 1148E Earth Wind and Fire
分析 必要条件: ① $\sum_{i=1}^{n} s_i = \sum_{i=1}^{n} t_i$ 预处理: 将 $s, t$ 从小到大排序. 尝试一 首尾匹配.例子 s = 2, 2, 4, ...
- Codeforces 1148 E - Earth Wind and Fire
E - Earth Wind and Fire 思路: 栈模拟 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC opti ...
- 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 ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Codeforces Testing Round #12 B. Restaurant 贪心
B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
随机推荐
- ubuntu+nginx+uwsgi部署django web项目
前言 将本地开发的django项目部署至linux上的uwsgi服务器,并配置nginx,完成基于ubuntu+nginx+uwsgi的上线运行.下面整理相关步骤. 服务器配置virtualenv 如 ...
- windows phone 下拉刷新
在windows phone 中采用数据列表时为了保证用户体验常遇到加载数据的问题.这个问题普遍到只要你用到数据列表就要早晚面对这个问题. 很多人会说这个问题已经有解决方案. 其实真正问题并不在于如何 ...
- persistence.xml模板配置
1.右键创建的persistence.xml 2.选择2.0版本的模板 3.复制右侧代码,写入xml文件中 <?xml version="1.0" encoding=&quo ...
- db2数据库的备份与还原
前言: 数据备份的重要性: 提高系统的高可用性和灾难可恢复性:(在数据库系统崩溃的时候,没有数据库备份怎么办!) 使用数据库备份还原数据库是数据库系统崩溃时提供数据恢复最小代价的最优方案:(总不能让客 ...
- pxc5.7 报错:WSREP_SST: [ERROR] xtrabackup_checkpoints missing
PXC 5.7 WSREP_SST: [ERROR] xtrabackup_checkpoints missing PXC5.7,在启动其中的一个节点,碰到了 [ERROR] xtrabackup_c ...
- 底部版权时间自动变化,网页在线qq咨询
<p><small>© 众筹网<script>document.write(new Date().getFullYear());</script> &l ...
- 图像处理---《Mat对象 与 IplImage对象》
图像处理---<认识 Mat对象> Mat对象 与 IplImage对象 (1)Mat对象:OpenCV2.0之后引进的图像数据结构.自动分配内存.不存在内存泄漏的问题,是面向对象的数据结 ...
- git 版本落后推送错误
http://114.215.90.144/student_association/student.git ! [rejected] master -> master (non- ...
- C#信号量(Semaphore,SemaphoreSlim)
Object->MarshalByRefObject->WaitHandle->Semaphore 1.作用: 多线程环境下,可以控制线程的并发数量来限制对资源的访问 2.举例: S ...
- Spring入门篇——第2章 Spring IOC容器
第2章 Spring IOC容器 介绍Spring IOC容器的基本概念和应用 2-1 IOC及Bean容器 自己的理解:什么是IOC?就是利用配置文件(外部容器)来创建对象. 在IOC容器中,所有对 ...