A. Avoiding Zero(前缀和+贪心)Codeforces Global Round 11

原题链接: https://codeforces.com/contest/1427/problem/A

在这里插入图片描述
测试样例

input
4
4
1 -2 3 -4
3
0 0 0
5
1 -1 1 -1 1
6
40 -31 -9 0 13 -40
output
YES
1 -2 3 -4
NO
YES
1 1 -1 1 -1
YES
-40 13 40 0 -9 -31

Note

Explanation of the first testcase: An array with the desired properties is b=[1,−2,3,−4]. For this array, it holds:

  • The first element of b is 1.
  • The sum of the first two elements of b is −1.
  • The sum of the first three elements of b is 2.
    The sum of the first four elements of b is −2.

Explanation of the second testcase: Since all values in a are 0, any rearrangement b of a will have all elements equal to 0 and therefore it clearly cannot satisfy the second property described in the statement (for example because b1=0). Hence in this case the answer is NO.

Explanation of the third testcase: An array with the desired properties is b=[1,1,−1,1,−1]. For this array, it holds:

  • The first element of b is 1.
  • The sum of the first two elements of b is 2.
  • The sum of the first three elements of b is 1.
  • The sum of the first four elements of b is 2.
  • The sum of the first five elements of b is 1.

Explanation of the fourth testcase: An array with the desired properties is b=[−40,13,40,0,−9,−31]. For this array, it holds:

  • The first element of b is −40.
  • The sum of the first two elements of b is −27.
  • The sum of the first three elements of b is 13.
  • The sum of the first four elements of b is 13.
  • The sum of the first five elements of b is 4.
  • The sum of the first six elements of b is −27.

题意: 给你一个长度为 n n n的整数序列 a a a,现在需要你对该序列进行重新排列使得前缀和不为 0 0 0,若不存在这样的排列,则输出“NO“。

解题思路: 我们想要让所有的前缀和 p r e pre pre都不为 0 0 0,那么该如何进行处理呢?对于 p r e [ i ] pre[i] pre[i],它是等于 p r e [ i − 1 ] + a [ i ] pre[i-1]+a[i] pre[i1]+a[i]。所以每个前缀和其实和上一个前缀和有关系,换句话说,如果我用一个 a n s ans ans遍历一遍排列好的数组, a n s ans ans累加元素和。如果出现 a n s = 0 ans=0 ans=0,则说明不可行。 那么我们根据贪心原则,我们要使得 a n s ≠ 0 ans≠0 ans=0,我们要么现在前面一直放正数,然后再放负数,要么现在前面一直放负数,然后一直放正数。(注意: 0 0 0是可以放放中间任意位置的,都没有影响) 这个时候我们就发现一个奇妙的东西了。如果正数和等于负数和,那么是不可能的,因为长度为 n n n的前缀和一定为 0 0 0,那么其他的情况是否一定满足呢?答案是一定的,我们一定要使得前缀和不为 0 0 0,那么如果正数和大于负数和,那么我们先一直放正数,然后放 0 0 0,然后放负数,就相当于是递减排序,这样前缀和是永远大于0的。如果负数和大于正数和,那么我们先一直放负数,然后放 0 0 0,然后放正数,就相当于是递增排序,这样前缀和是永远小于0的。

AC代码

/*
*邮箱:unique_powerhouse@qq.com
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h>	//POJ不支持

#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair

using namespace std;

const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//

int t,n;
int main(){
	//freopen("in.txt", "r", stdin);//提交的时候要注释掉
	IOS;
	while(cin>>t){
		while(t--){
			cin>>n;
			vector<int> num(n);
			int pos=0,neg=0;//pos统计正数和,neg统计负数和。一定要初始化。
			rep(i,0,n-1){
				cin>>num[i];
				if(num[i]>0){
					pos+=num[i];
				}
				else if(num[i]<0){
					neg+=num[i];
				}
			}
			//目的使得前缀和不为0,故要么前面全是正数使得和最大,再用负数去减。要么前面全是负数使得和最小,再用正数去加。
			if(pos+neg==0){
				//这种必不可能,无论你怎么排列,长度为n的前缀和必为0.
				cout<<"NO"<<endl;
			}
			//那么其他就可行。
			else if(pos+neg>0){
				//说明正数和大,先放正数,再放负数,必满足,我们直接从大到小排列即可。
				sort(num.begin(),num.end(),greater<int>() );
				cout<<"YES"<<endl;
				rep(i,0,n-1){
					cout<<num[i]<<" ";
				}
				cout<<endl;
			}
			else{
				//说明负数和大,先放负数,再放正数,必满足,我们直接从小到大排列即可。
				sort(num.begin(),num.end());
				cout<<"YES"<<endl;
				rep(i,0,n-1){
					cout<<num[i]<<" ";
				}
				cout<<endl;
			}
		}
	}
	return 0;
}

# Global Planner Parameters # maximum distance to the goal point goal_distance_tolerance: 0.1 # maximum allowed numerical error for goal position xy_goal_tolerance: 0.2 # maximum allowed numerical error for goal orientation yaw_goal_tolerance: 0.3 # weight for the heuristic function used in the A* algorithm # higher values prefer straighter paths, lower values prefer paths with less turning heuristic_weight: 3.0 # minimum distance to travel before attempting to replan min_replan_distance: 1.0 # minimum amount of time to wait before attempting to replan min_replan_time: 1.0 # tolerance on the robot's heading (in radians) when planning # during rotation commands this is an additional error that gets added to # yaw_goal_tolerance heading_lookahead: 0.325 # minimum lookahead to do during path planning. A shorter lookahead is more # cautious (especially in tight spaces) but may be more effective at avoiding # collisions with complex obstacles min_lookahead_distance: 0.4 # maximum lookahead to do during path planning max_lookahead_distance: 2.0 # if true, the global planner will only plan one step at a time # rather than to the final goal state intermediate_planning: false # what topic the planner should use for status feedback planner_frequency: 0.5 planner_topic: "planner_status" # how close the robot must be to the global plan before updating it with # a new one plan_update_distance: 0.5 # how often the planner should be allowed to make new plans. A value of 0 # means plans will be made as often as possible planner_patience: 5.0 # penalty for robot rotation during path planning. A higher penalty will # cause the planner to prefer straighter paths with less turns rotation_penalty: 0.8 # maximum absolute rotation speed allowed while navigating along the global # plan max_rotation_speed: 1.0 # maximum speed to travel along the global plan max_velocity: 0.6 # If true, the global planner will try to avoid obstacles with a combination # of steering and braking. Otherwise, it will only steer around obstacles braking_enabled: true # how many times to retry a goal update if the previous attempt resulted in a # collision or other error. # If set to -1, it will retry indefinitely goal_update_retries: 3 # Whether or not to use the extrinsic rotation control method in the planner use_extrinsic_rotation: true # Enabling this parameter causes the global planner to use # differential constraints for smoother trajectories use_differential_constraints: true # Enabling this parameter causes the planner to assume the # robot is driving on the right-hand side of the street drive_on_right: true # Timeout for the planning process (in seconds). If planning takes longer # than this, the planner will abort and return a failure status planning_timeout: 5.0 # If set, this parameter limits the maximum planning distance the # planner will use. Set to -1 for no limit. max_planning_distance: -1 # Enabling this parameter causes the planner to ignore the robot's ground # clearance when planning. ignore_ground_clearance: false # Whether the planner should try to avoid going backwards avoid_going_backwards: false # The maximum distance (in meters) that the planner will consider changing # the orientation of the robot to better follow the path. Set to -1 to # disable this behavior. max_orientation_change: 0.9 # The minimum distance (in meters) that the planner will consider # detecting a change in orientation of the robot to better follow the path. # Set to -1 to disable this behavior. min_orientation_change: 0.5 # Whether the planner should attempt to use the current local plan when planning. # If set to true, the planner will attempt to connect the current local plan # to the new plan. If set to false, the planner will always start from the robot's # current pose. use_local_plan: true # Whether the planner should attempt to use the current local costmap when planning. # If set to true, the planner will use the local costmap to build an estimate # of the robot's surroundings. If set to false, the planner will only use the # global costmap. use_local_costmap: true # Whether the planner should use the old behavior of setting waypoints to # the right of the global plan. This behavior causes the robot to execute # the plan with a rightward shift. However, it can be problematic if the plan # encounters obstacles on the left side. use_typical_rightward_shift: false # Whether the planner should use a zero velocity as a way to avoid oscillations. # If set to true, the planner will stop the robot and wait for the current goal # to either become unreachable or within the goal tolerance. stop_when_goal_rejected: false # The time (in seconds) that the planner will stop and wait (in case of oscillations) # before trying to replan. stop_and_wait_time: 2.0 # Distance (in meters) to be left before the end of the path. This can be useful # when the robot should stop at a certain distance from the goal pose. path_distance_offset: 0 # Maximum allowed speed deviation from the global plan (in m/s). max_allowed_speed_deviation: 1.0 # Maximum allowed angular deviation from the global plan (in rad). max_allowed_angular_deviation: 1.57 # Whether or not to use a linear navigation function to bias global plan costs toward closer parts of the map. use_linear_navigation_function: false # Whether or not to use a terrain independent cost scale to bias global plan costs toward flatter regions. use_terrain_independent_cost_scale: true # The maximum number of obstacles to check against during planning. max_obstacle_check_count: 500 # If true, the planner will skip planning during the first update cycle after initialization. skip_initial_planning: false # Scaling factor for the distances used in the Adaptive Sampling Path algorithm as_scaler: 1.0 # The maximum length of the Adaptive Sampling Path segments as_max_segment_length: 1.0 # The turning radius for the robot used in the prediction step of the Adaptive Sampling Path algorithm as_robot_radius: 0.3 # The number of areas forward used for heading smoothing in the Adaptive Sampling Path algorithm. # Set to 0 if heading smoothing is not desired. as_heading_smoothing_areas: 0 # The maximum distance the Adaptive Sampling Path algorithm will plan for. # Set to -1 for no limit. as_max_global_plan_distance: -1 # How many layers of costmaps to plan in. A higher value will allow the global # planner to take into account more layers of static and dynamic obstacles. # 0 means only use the base global costmap. planning_layers: 0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HeZephyr

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值