题目链接:

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(贪心)的更多相关文章

  1. Codeforces 1148E Earth Wind and Fire

    分析 必要条件: ① $\sum_{i=1}^{n} s_i = \sum_{i=1}^{n} t_i$ 预处理: 将 $s, t$ 从小到大排序. 尝试一 首尾匹配.例子 s = 2, 2, 4, ...

  2. Codeforces 1148 E - Earth Wind and Fire

    E - Earth Wind and Fire 思路: 栈模拟 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC opti ...

  3. 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 ...

  4. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  5. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  6. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  7. Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心

    C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...

  8. Codeforces Testing Round #12 B. Restaurant 贪心

    B. Restaurant Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/problem ...

  9. Codeforces 437D The Child and Zoo(贪心+并查集)

    题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...

随机推荐

  1. 【转载】Jmeter接口测试+压力测试

     jmeter是apache公司基于java开发的一款开源压力测试工具,体积小,功能全,使用方便,是一个比较轻量级的测试工具,使用起来非常简单.因为jmeter是java开发的,所以运行的时候必须先要 ...

  2. ORACLE触发器的自治事务的注意事项

    直接上代码: Create OR replace Trigger TR_ROBXMX_CLDJBHHX After INSERT OR UPDATE OR DELETE ON ROBXMX1 --要监 ...

  3. [转载]linux的top命令中cpu信息的含义

    https://www.cnblogs.com/wjoyxt/p/4918742.html 原文很好,我就不摘录了.

  4. elmentUI为table中的单元格添加事件

    <el-main> <el-tabs v-model="curTab" type="card"> <!-- tab签 --> ...

  5. Jerry和您聊聊Chrome开发者工具

    Chrome开发者工具是Jerry日常工作使用的三大调试器之一.虽然工具名称前面带了个"开发者", 但是它对非开发人员仍然有用.不信? 用Chrome打开我们常用的网站,按F12, ...

  6. 13_Hive优化

    Hive优化 要点:优化时,把hive sql当做map reduce程序来读,会有意想不到的惊喜. 理解hadoop的核心能力,是hive优化的根本. 长期观察hadoop处理数据的过程,有几个显著 ...

  7. 大幅提升Delphi Datasnap数据传输效率的方法

    方法一:增加TCP读写缓存的大小       DataSnap Server中负责TCP/IP通讯的组件是TDSTCPServerTransport,它默认的TCP/IP读写缓冲区的大小为32KB,由 ...

  8. idou老师带教你学Istio 03: istio故障注入功能的介绍和使用

    故障注入测试 故障注入测试顾名思义就是当被测试应用部分组件或功能出现潜在故障时其本身的容错机制是否正常工作,以达到规避故障保证正常组件或功能的使用.Istio提供了HTTP故障注入功能,在http请求 ...

  9. Python&Selenium 数据驱动【unittest+ddt+xml】

    一.摘要 本博文将介绍Python和Selenium做自动化测试的时候,基于unittest框架,借助ddt模块使用xml文件作为数据文件作为测试输入 二.xml文件 <?xml version ...

  10. Redis入门(三)——主从服务器配置

    当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能.redis提供了一个master,多个sl ...