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条路,每条路 ...
随机推荐
- 关于RESTful API
添几篇文章: 一.What Is REST? 二.RESTful API最佳实践 三.MS Azure——API Design 这些文章里面也推荐了一些关联文章,微软那篇最详细,非常值得一读.
- Kafka实际使用过程中遇到的一些问题及解决方法
Kafka实际使用过程中遇到的一些问题及解决方法: 1.关于Kafka的分区: 开始使用Kafka的时候,没有分区的概念,以为类似于传统的MQ中间件一样,就直接从程序中获取Kafka中的数据. 后来程 ...
- JavaScript数组方法之reduce
又见到数组方法了,在前面已经的多次写到过数组方法,甚至都使用原生方法重构了一遍数组的各个方法,可是随着数组方法reduce的应用,发现reduce真的是妙用无穷啊!还是很值得再拿出来说一遍的. 我们再 ...
- LeetCode 866. Prime Palindrome
866. Prime Palindrome(回文素数) 题目: 求出大于或等于 N 的最小回文素数. 回顾一下,如果一个数大于 1,且其因数只有 1 和它自身,那么这个数是素数. 例如,2,3,5,7 ...
- python3 多线程 采集 xpath
#!/usr/bin/python # -*- coding: UTF-8 -*- '''Thread3 多线程测试采集''' import threading,time,queue,Mongo_ut ...
- caffe笔记
1. 训练 cifar10 示例 ① cd caffe.1.0.0 ./data/cifar10/get_cifar10.sh #获取图片 ② ./examples/cifar10/cre ...
- Ubuntu/centos/redhat/SUSE sipp安装(带rtp支持,3.5.1版本)
1.ubuntu 12.04 apt-get install ncurses-dev apt-get install libpcap-dev ./configure --with-pcap make ...
- Oracle 调试存储过程
调试过程对找到一个存过的bug或错误是非常重要的,Oracle作为一款强大的商业数据库,其上面的存过少则10几行,多则上千行,免不了bug的存在,存过上千行的话,找bug也很费力,通过调试可以大大减轻 ...
- 使用nodejs实现OData的batch操作在Marketing Cloud里读取contact信息
我们先来看看Marketing Cloud系统里的contact信息: 一共1218374条数据. 我们用如下的nodejs代码通过OData来获取这些数据: var request = requir ...
- @ComponentScan注解及其XML配置
开发中会经常使用包扫描,只要标注了@Controller.@Service.@Repository,@Component 注解的类会自动加入到容器中,ComponentScan有注解和xml配置两种方 ...