Codeforces 149C Division into Teams【贪心】
来源:互联网 发布:mac怎么关闭打开的程序 编辑:程序博客网 时间:2024/04/24 23:00
Petya loves football very much, especially when his parents aren't home. Each morning he comes to the yard, gathers his friends and they play all day. From time to time they have a break to have some food or do some chores (for example, water the flowers).
The key in football is to divide into teams fairly before the game begins. There aren boys playing football in the yard (including Petya), each boy's football playing skill is expressed with a non-negative characteristicai (the larger it is, the better the boy plays).
Let's denote the number of players in the first team as x, the number of players in the second team as y, the individual numbers of boys who play for the first team as pi and the individual numbers of boys who play for the second team asqi. Divisionn boys into two teams is considered fair if three conditions are fulfilled:
- Each boy plays for exactly one team (x + y = n).
- The sizes of teams differ in no more than one (|x - y| ≤ 1).
- The total football playing skills for two teams differ in no more than by the value of skill the best player in the yard has. More formally:
Your task is to help guys divide into two teams fairly. It is guaranteed that a fair division into two teams always exists.
The first line contains the only integer n (2 ≤ n ≤ 105) which represents the number of guys in the yard. The next line containsn positive space-separated integers, ai (1 ≤ ai ≤ 104), thei-th number represents the i-th boy's playing skills.
On the first line print an integer x — the number of boys playing for the first team. On the second line printx integers — the individual numbers of boys playing for the first team. On the third line print an integery — the number of boys playing for the second team, on the fourth line printy integers — the individual numbers of boys playing for the second team. Don't forget that you should fulfil all three conditions:x + y = n, |x - y| ≤ 1, and the condition that limits the total skills.
If there are multiple ways to solve the problem, print any of them.
The boys are numbered starting from one in the order in which their skills are given in the input data. You are allowed to print individual numbers of boys who belong to the same team in any order.
31 2 1
21 2 13
52 3 3 1 1
34 1 3 25 2
Let's consider the first sample test. There we send the first and the second boy to the first team and the third boy to the second team. Let's check all three conditions of a fair division. The first limitation is fulfilled (all boys play), the second limitation on the sizes of groups (|2 - 1| = 1 ≤ 1) is fulfilled, the third limitation on the difference in skills ((2 + 1) - (1) = 2 ≤ 2) is fulfilled.
题目大意:
给你N个数,让你将这N个数分成两堆,使得两堆的数的个数差小于等于1.而且使得两堆的数字价值和相差小于等于原来这N个数中最大的那个数的价值。
保证有解,输出任意可行方案。
思路:
首先将所有数从大到小排序,那么对应交叉分配给两个堆即可。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;struct node{ int val,pos;}a[140000];int cmp(node a,node b){ return a.val>b.val;}int main(){ int n; while(~scanf("%d",&n)) { for(int i=1;i<=n;i++)scanf("%d",&a[i].val),a[i].pos=i; sort(a+1,a+1+n,cmp); vector<int >A; vector<int >B; int tmp=0; for(int i=1;i<=n;i++) { if(tmp==0) { if(i%2==1)A.push_back(a[i].pos); else B.push_back(a[i].pos),tmp=1; } else { if(i%2==1)B.push_back(a[i].pos); else A.push_back(a[i].pos),tmp=1; } } printf("%d\n",A.size()); for(int i=0;i<A.size();i++)printf("%d ",A[i]); printf("\n"); printf("%d\n",B.size()); for(int i=0;i<B.size();i++)printf("%d ",B[i]); printf("\n"); }}
- Codeforces 149C Division into Teams【贪心】
- CodeForces 149C - Division into Teams(贪心)
- Codeforces 149C Division into Teams 【思维】
- Codeforces 149C Division Into Teams 构造
- Codeforces 149C Division into Teams
- cf#106-C. Division into Teams-贪心
- Codeforces 149C Division into Teams(模拟)
- code forces 149C Division into Teams
- Codeforces Round #273(Div2) B. Random Teams 贪心+数学
- [agc009c]Division into Two
- CodeForces 275C 贪心
- codeforces C. Bits 贪心
- codeforces C. Hometask(贪心)
- Codeforces 449C 贪心
- Codeforces 794C【贪心】
- CodeForces 873C(贪心)
- CodeForces 825C(贪心)
- 【CODEFORCES】 B. Random Teams
- java小案例-回忆经典之飞机大战游戏(附源码免费下载)
- 命令行编译运行Java程序时,加载指定目录中的Jar包
- 基于顺序搜索和索引搜索的动态分区分配算法 总结
- 【操作系统】银行家算法避免死锁
- Hibernate不能读取根据中文查询
- Codeforces 149C Division into Teams【贪心】
- poll服务器
- JAVAWEB开发之Hibernate详解(二)——Hibernate的持久化类状态与转换、以及一级缓存详解、关联关系的映射(一对多、多对多、级联)
- Thinking In Java学习笔记
- Android Vendor Test Suite (VTS) 1
- Failed to get the required ADT version number from the SDK. The Android Developer Toolkit may not w
- redis持久化
- java properties文件转义字符
- (自学篇)在Mac上stf--WEB端移动设备管理工制工具--的安装过程