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条路,每条路 ...
随机推荐
- JAVA生成验证码代码
生成base64格式图片验证码 /** * 验证码的候选内容 */ private char codeSequence[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', ...
- docker 入门3 - 服务 【翻译】
入门,第 3 部分:服务 先决条件 安装 Docker 版本 1.13 或更高版本. 获取 Docker Compose.在适用于 Mac 和 Docker 桌面的 Windows 上,它已预安装,因 ...
- C# 委托 、事件、同步、异步知识点归纳
一.委托 基本用法: 1.声明一个委托类型.委托就像是‘类'一样,声明了一种委托之后就可以创建多个具有此种特征的委托.(特征,指的是返回值.参数类型) public delegate void Som ...
- Session Timer机制分析
Session Timer机制分析 功能介绍 会话初始化协议(SIP)并没有为所建立的会话定义存活机制.尽管用户代理可以通过会话特定的机制判断会话是否超时,但是代理服务器却做不到这点.如此一来,代理服 ...
- 【php设计模式】观察者模式
当对象间存在一对多关系时,则使用观察者模式.比如,当一个对象被修改时,则会自动通知它的依赖对象.观察者模式属于行为型模式. <?php class Subject{ private $obser ...
- 前端 vue/react 或者 js 导入/导出 xlsx/xls (带样式)表格的功能
第一种导出表格的功能: yarn add xlsx script-loader file-saver xlsx-style 效果展示 xlsx-style的bug修复:node_module/xlsx ...
- .NET 反射应用
object request = null; string requestObjClassName = "命名空间" + 类型.ToString(); Type type = Ty ...
- ubuntu安装交叉编译器
# sudo apt-get install gcc-arm-linux-gnueabi # sudo apt-get install g++-arm-linux-gnueabi 官方下载 https ...
- WeakReference 与 SoftReference 区别
装载自:http://flyneil.iteye.com/blog/1345177 WeakReference与SoftReference都可以用来保存对象的实例引用,这两个类与垃圾回收有关. Wea ...
- 深入SpringBoot注解原理及使用
首先,先看SpringBoot的主配置类: @SpringBootApplication public class StartEurekaApplication { public static voi ...