#301 (div.2) B. School Marks

本文介绍了一道通过贪心法解决的编程题,详细解释了解题思路:首先确保中位数y的要求得到满足,然后将剩余部分设置为1。文章提供了完整的C++代码实现,并强调了理解题目要求的重要性。

1.题目描述:点击打开链接

2.解题思路:本题利用贪心法解决。比赛的时候看错了,把y理解成了最小值,花了半天去写一个错误的代码,最后才发现y指的是中位数。。教训颇为惨痛!本题可以先满足中位数y的要求,剩下的都设为1即可。给定了n,那么可知,至多只能有(n-1)/2个数要小于中位数y。因此可以事先统计一下输入的数中有xc1个是小于y的,同时累加和sum。如果sum>x或c1>(n-1)/2,则无解。否则,至少还要添加max(0,(n+1)/2-(k-c1))个中位数。添加完毕之后,由于sum不能超过x,因此只用将剩下的所有数设为1即可。如果最终sum>x,那么无解,否则输出解。

3.代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define N 1000+5
int n, k, p, x, y;
vector<int>a;

int main()
{
	//freopen("t.txt", "r", stdin);
	while (~scanf("%d%d%d%d%d", &n, &k, &p, &x, &y))
	{
		int sum = 0;
		a.clear();
		int c1 = 0;
		for (int i = 0; i < k; i++)
		{
			int sx;
			scanf("%d", &sx);
			if (sx < y)c1++;
			sum += sx;
		}
		if (sum>x || c1>(n - 1) / 2)puts("-1");//c1不能超过最大限制(n-1)/2
		else{
			int m = max(0, (n + 1) / 2 - k + c1);//还需要添加的中位数是0和(n+1)-(k-c1)中的较大者
			for (int i = 0; i < m; i++)
				a.push_back(y);
			sum += m*y;
			if (sum + n - k - m>x)puts("-1");
			else{
				for (int i = 0; i < n - k - m; i++)
					a.push_back(1);
				int len = a.size();
				for (int i = 0; i < len; i++)
					printf("%d%c", a[i], i == len - 1 ? '\n' : ' ');
			}
		}
	}
	return 0;
}

from argparse import ArgumentParser import cv2 from face_detection import FaceDetector from mark_detection import MarkDetector from pose_estimation import PoseEstimator from utils import refine # Parse arguments from user input. parser = ArgumentParser() parser.add_argument("--video", type=str, default=None, help="Video file to be processed.") parser.add_argument("--cam", type=int, default=0, help="The webcam index.") args = parser.parse_args() print(__doc__) print("OpenCV version: {}".format(cv2.__version__)) def run(): # Before estimation started, there are some startup works to do. # Initialize the video source from webcam or video file. video_src = args.cam if args.video is None else args.video cap = cv2.VideoCapture(video_src) print(f"Video source: {video_src}") # Get the frame size. This will be used by the following detectors. frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # Setup a face detector to detect human faces. face_detector = FaceDetector("assets/face_detector.onnx") # Setup a mark detector to detect landmarks. mark_detector = MarkDetector("assets/face_landmarks.onnx") # Setup a pose estimator to solve pose. pose_estimator = PoseEstimator(frame_width, frame_height) # Measure the performance with a tick meter. tm = cv2.TickMeter() # Now, let the frames flow. while True: # Read a frame. frame_got, frame = cap.read() if frame_got is False: break # If the frame comes from webcam, flip it so it looks like a mirror. if video_src == 0: frame = cv2.flip(frame, 2) # Step 1: Get faces from current frame. faces, _ = face_detector.detect(frame, 0.7) # Any valid face found? if len(faces) > 0: tm.start() # Step 2: Detect landmarks. Crop and feed the face area into the # mark detector. Note only the first face will be used for # demonstration. face = refine(faces, frame_width, frame_height, 0.15)[0] x1, y1, x2, y2 = face[:4].astype(int) patch = frame[y1:y2, x1:x2] # Run the mark detection. marks = mark_detector.detect([patch])[0].reshape([68, 2]) # Convert the locations from local face area to the global image. marks *= (x2 - x1) marks[:, 0] += x1 marks[:, 1] += y1 # Step 3: Try pose estimation with 68 points. pose = pose_estimator.solve(marks) tm.stop() # All done. The best way to show the result would be drawing the # pose on the frame in realtime. # Do you want to see the pose annotation? pose_estimator.visualize(frame, pose, color=(0, 255, 0)) # Do you want to see the axes? # pose_estimator.draw_axes(frame, pose) # Do you want to see the marks? # mark_detector.visualize(frame, marks, color=(0, 255, 0)) # Do you want to see the face bounding boxes? # face_detector.visualize(frame, faces) # Draw the FPS on screen. cv2.rectangle(frame, (0, 0), (90, 30), (0, 0, 0), cv2.FILLED) cv2.putText(frame, f"FPS: {tm.getFPS():.0f}", (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255)) # Show preview. cv2.imshow("Preview", frame) if cv2.waitKey(1) == 27: break if __name__ == &#39;__main__&#39;: run()
最新发布
03-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值