猿辅导第一题, AC

AC,每次选择人数最大的三个角色, 然后组成一组, 然后每个角色减1,放回数组重新排序(直接排序应该超时,我用的优先队列), 然后再选择人数最多的, 如此往复。

下面是AC的代码, 注意,每次只能减1个人。
为什么每次减1呢, (那么你们为什么要每次选最大的呢?) 减1是因为最大的减1之后,会变成不是最大的。
下面的样例:
1
1 2 2 2 3

[2, 2, 2, 3]
[2, 1, 1, 2]
[1, 1, 0, 1]
[0, 0, 0, 0]
每次减1,答案是3.
如果直接减去2,答案是2,并不是最优的分配。


#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 10000 + 10;
int a[N];

int main()
{
	int t;
	int n;

	cin >> t;
	for(int k = 0; k < t; ++k)
	{
		priority_queue<int> q;
		cin >> n;
		for(int i = 0; i < n; ++i)
		{
			cin >> a[i];
			if(a[i] == 0)
				continue;
			q.push(a[i]);
		}

		int ans = 0;
		while(q.size() >= 3)
		{
			int a1 = q.top();
			q.pop();
			int a2 = q.top();
			q.pop();
			int a3 = q.top();
			q.pop();
			/* cout << a1 << " " << a2 << " " << a3 << endl; */
                        //每次只能减1哦哦,重要!!!
			int group = 1;
			ans += group;

			if(a1 - group > 0)
				q.push(a1 - group);
			if(a2 - group > 0)
				q.push(a2 - group);
			if(a3 - group > 0)
				q.push(a3 - group);
		}
		cout << ans << endl;

	}
	return 0;
}




#猿辅导##笔试题目#
全部评论
我也是呀,但是依然是0
点赞
送花
回复
分享
发布于 2019-08-24 17:37
import heapq num = int(input().strip()) result = [] for i in range(num):     temp = [int(x) for x in input().strip().split(' ')]     if temp[0]<3:         result.append(0)     else:         res = []         tp = []         temp = temp[1:]         temp.sort()         for x in temp:             if x > 0:                 heapq.heappush(tp, -x)         while(len(tp)>=3):             x1 = heapq.heappop(tp)             x2 = heapq.heappop(tp)             x3 = heapq.heappop(tp)             res.append(-x3)             x1-=x3             x2-=x3             if x1<0:                 heapq.heappush(tp, x1)             if x2<0:                 heapq.heappush(tp, x2)         result.append(sum(res)) for x in result:     print(x) 我们写的一样吧 我是0
点赞
送花
回复
分享
发布于 2019-08-24 17:39
滴滴
校招火热招聘中
官网直投
过95,最后一个用例超时就很难受
点赞
送花
回复
分享
发布于 2019-08-24 17:40
public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int c = sc.nextInt();         while (c-- != 0) {             int t = sc.nextInt();             if (t < 3) {                 System.out.println(0);                 continue;             }             PriorityQueue<Integer> pq = new PriorityQueue<Integer>((o1, o2) -> {                 int x = (int) o1;                 int y = (int) o2;                 return y - x;             });             for (int i = 0; i < t; i++) {                 pq.add(sc.nextInt());             }             long count = 0;             while (true) {                 int t1 = pq.poll();                 int t2 = pq.poll();                 int t3 = pq.poll();                 if (t3 != 0) {                     count += 1;                     pq.add(t1 - 1);                     pq.add(t2 - 1);                     pq.add(t3 - 1);                 } else {                     break;                 }             }             System.out.println(count);         }     } 通过90.有case没过,我找不到。
点赞
送花
回复
分享
发布于 2019-08-24 17:40
我的思路是每次选最小的一个跟最大的两个,但是没做出来,不知道对不对😂
点赞
送花
回复
分享
发布于 2019-08-24 17:42
为啥每次只能减1?
点赞
送花
回复
分享
发布于 2019-08-24 17:47
每次从大到小三个一组嘛,为啥不能直接减这组里的最小值
点赞
送花
回复
分享
发布于 2019-08-24 17:48
!!!看了你的思路我恍然大悟!!我思路和你一模一样,但是我没有用Set!!!每次都sort,哇  太蠢了!!!
点赞
送花
回复
分享
发布于 2019-08-24 17:53
恍然大悟。。。这个贪心不能太贪心啊
点赞
送花
回复
分享
发布于 2019-08-24 18:00
也不用每次减一吧,只要注意第三大的小于第四大的时候就要重新找第三大。一个一个减太慢了
点赞
送花
回复
分享
发布于 2019-08-24 18:03
有没有用python过了的。 同样的逻辑用python过了0。。。。。
点赞
送花
回复
分享
发布于 2019-08-24 18:07
vector<int> problemTwo(vector<vector<int>>&num) { vector<int>ret; int iSize = num.size(); if (iSize == 0) return ret; for (int i = 0; i < iSize; i++) { int jMax = num[i].size(); if (jMax < 3) { ret.push_back(0); continue; } else { sort(num[i].begin(), num[i].end()); int iret = 0; for (int j = 0; j < jMax; j++) { if (num[i][j] == 0) continue; else { if (j + 2 < jMax) { iret += num[i][j]; num[i][j + 1] -= num[i][j]; num[i][j + 2] -= num[i][j]; num[i][j] = 0; } } } ret.push_back(iret); } } return ret; } 这个思路没错吧,先排序,然后减三个中最小的那个!不知道是不是时间超时了,没通过。
点赞
送花
回复
分享
发布于 2019-08-24 18:21

相关推荐

点赞 评论 收藏
转发
点赞 评论 收藏
转发
2 15 评论
分享
牛客网
牛客企业服务