首页 > 其他分享 >[CF1599A] Weights

[CF1599A] Weights

时间:2023-09-04 16:11:56浏览次数:33  
标签:ch string weight CF1599A Weights putting balance weights

题目描述

You are given an array $ A $ of length $ N $ weights of masses $ A_1 $ , $ A_2 $ ... $ A_N $ . No two weights have the same mass. You can put every weight on one side of the balance (left or right). You don't have to put weights in order $ A_1 $ ,..., $ A_N $ . There is also a string $ S $ consisting of characters "L" and "R", meaning that after putting the $ i-th $ weight (not $ A_i $ , but $ i-th $ weight of your choice) left or right side of the balance should be heavier. Find the order of putting the weights on the balance such that rules of string $ S $ are satisfied.

输入格式

The first line contains one integer $ N $ ( $ 1 \leq N \leq 2*10^5 $ ) - the length of the array $ A $ The second line contains $ N $ distinct integers: $ A_1 $ , $ A_2 $ ,..., $ A_N $ ( $ 1 \leq A_i \leq 10^9 $ ) - the weights given The third line contains string $ S $ of length $ N $ consisting only of letters "L" and "R" - string determining which side of the balance should be heavier after putting the $ i-th $ weight of your choice

输出格式

The output contains $ N $ lines. In every line, you should print one integer and one letter - integer representing the weight you are putting on the balance in that move and the letter representing the side of the balance where you are putting the weight. If there is no solution, print $ -1 $ .

由于样例没有 -1,所以大概率不存在无解情况。

神奇的,考虑把 \(a\) 排序后,对于所有 \(a_i\),将 \(i\) 为偶数的丢一堆, \(i\) 为奇数丢一堆。

具体哪堆丢左边哪堆丢右边看 \(S_n\),因为发现将其从大到小放时,一定是最后丢的那个所在的堆更大,可以用差分证明。

然后考虑把 \(n\) 的问题化为 \(n-1\) 的问题。正着不好做,反着做。发现此时如果拿掉最大的那个砝码所在的地方,天平情况一定会反转,如果拿掉最小的那个砝码,天平情况一定不变。就能从 \(n\) 变为 \(n-1\)。

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5;
const char g[]={'L','R'};
char s[N];
int a[N],n,l=1,r,p[N];
int read()
{
	int s=0;
	char ch=getchar();
	while(ch<'0'||ch>'9')
		ch=getchar();
	while(ch>='0'&&ch<='9')
		s=s*10+ch-48,ch=getchar();
	return s;
}
int main()
{
	r=n=read();
	for(int i=1;i<=n;i++)
		a[i]=read();
	sort(a+1,a+n+1);
	scanf("%s",s+1);
	for(int i=n;i^1;i--)
	{
		if(s[i]==s[i-1])
			p[i]=l++;
		else
			p[i]=r--;
	}
	p[1]=l;
	for(int i=1;i<=n;i++)
		printf("%d %c\n",a[p[i]],g[(p[i]&1)^(s[n]=='R')^(n&1)]);
}

标签:ch,string,weight,CF1599A,Weights,putting,balance,weights
From: https://www.cnblogs.com/mekoszc/p/17677342.html

相关文章

  • CF1844G Tree Weights
    题面传送门这个真的很容易想到吗?首先定\(1\)为根,设每个点的深度是\(d_i\),则两个点之间的距离是\(d_{i}+d_{i+1}-2d_{LCA(i,i+1)}\)。题目中相当于给出了\(n-1\)个方程和\(n-1\)个限制,要解出一个\(n-1\)元的方程。因为限制是从\(1\ton-1\)给出的,所以不可能包含,因此......
  • Experience Replay with Likelihood-free Importance Weights
    发表时间:2020文章要点:这篇文章提出LFIW算法用likelihood作为experience的采样权重(likelihood-freedensityratioestimator),reweightexperiencesbasedontheirlikelihoodunderthestationarydistributionofthecurrentpolicy,这种方式鼓励让经常访问的状态有更小的误差......
  • 关于深度学习中的两个概念weights和checkpoint
    WEIGHT和checkpoint都是深度学习中的概念,但它们的含义和作用有所不同。WEIGHT通常指的是神经网络中的参数。在训练过程中,神经网络的参数会不断更新以提高模型的准确性。这些参数通常被存储在称为“权重”的数组中。因此,当我们保存模型的权重时,我们实际上是将神经网络的参数保存到......
  • CF1599A. Weights
    题意给出n个物品,第i个重量a[i](互不相同)每次任意选一个物品放到秤的左右两边,使得放完之后左>右或左<右给出a[i]和大小关系s[i],构造方案题解必定有解把a排序,假设当前选了LRLRLR,发现在最后加L可以瞬间反转,在最前加R可以保持不变即,当前选了一段连续的a[i],放的顺序为...LRL......
  • 37. CF-Weights Distributing
    链接这是一个比较经典的题目。容易想到求出两段路径重合的部分,然后贪心的放权值。那么跑三次最短路,枚举重合部分的端点即可。正解没什么好说的。这题有趣的地方在于,如果......
  • [ARC144E]GCD of Path Weights
    ProblemStatementYouaregivenadirectedgraph$G$with$N$verticesand$M$edges.Theverticesarenumbered$1,2,\ldots,N$.The$i$-thedgeisdirected......
  • 将自有数据集下yolov训练结果(*.weights) 在进行部署
      原始的*.weigths是由darknet训练的(目前也存在tensorflow和其它库的改写版本,这里不做介绍),AlexeyAB版的darknet(​​https://github.com/AlexeyAB/darkne......
  • 论文理解【RL - Exp Replay】—— 【LFIW】Experience Replay with Likelihood-free I
    摘要:经验回放(ExperienceReplay),即使用过去的经验来加速价值函数的时序差分(TD)学习,是深度强化学习的关键组成部分。对重要的经验进行优先排序或重新加权已经被证明可以提高TD......
  • ABC 214D Sum of Maximum Weights(并查集模拟删边)
    ABC214DSumofMaximumWeights(并查集模拟删边)SumofMaximumWeights​ 给出有\(n\;(2\len\le1e5)\)个点的一棵树,定义\(f(x,y)\)表示从节点x到节点y的最短......
  • 卷积神经网络(CNN)(local receptive fields & 局部接收域 & 局部感受野、shared weights
    文章目录​​localreceptivefield​​​​Sharedweights​​​​spatialortemporalsubsampling​​localreceptivefield局部感受野,也叫感受视野域。这个localrece......