贪心——The Martian Challenge 2017 #G. Pick Your Team
来源:互联网 发布:js this对象 编辑:程序博客网 时间:2024/05/22 15:50
题目链接: http://codeforces.com/gym/101306/problem/G
题意:给出N个士兵的力量值,地球人和火星人轮流拿(地球人先),给出火星人拿取士兵的先后顺序。求地球人怎么拿使得最后双方的差最大。求这个最大差值。
分析:我们可以以火星人拿的顺序作为参考,假设地球人每次拿这个顺序下剩下的第一个,火星人拿第二个,并且在每次火星人拿的时候,比较一下当前的士兵力量值和之前地球人拿过的士兵的力量的最小值,如果前者大于后者,则交换这两个士兵的。这样就能保证在轮流拿的前提下,差值最大。
AC代码:
/************************************************************************* > File Name: test.cpp > Author: Akira > Mail: qaq.febr2.qaq@gmail.com ************************************************************************/#include<bits/stdc++.h>typedef long long LL;typedef unsigned long long ULL;typedef long double LD;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))using namespace std;#define MaxN 100001#define MaxM MaxN*10#define INF 1E9+7#define PI 3.1415926535897932384626const int mod = 1E9+7;const double eps = 1e-6;#define bug cout<<88888888<<endl;#define debug(x) cout << #x" = " << x << endl;int n;struct Node{ int val,ord;}node[110];int vis[110];int main(){ //std::ios::sync_with_stdio(false); scanf("%d", &n); for(int i=1;i<=n;i++) scanf("%d", &node[i].val); for(int i=1;i<=n;i++) {int x;scanf("%d", &x);node[x].ord = i;} sort(node+1,node+n+1,[](Node x,Node y){return x.ord<y.ord;}); int ans = 0; for(int i=2;i<=n;i+=2) { vis[i-1] = 1; int MIN = INF,loc; for(int j=1;j<i;j++) { if(vis[j] && MIN > node[j].val) { MIN = node[j].val; loc = j; } } if( node[i].val > MIN ) { vis[loc] = 0; vis[i] = 1; } } for(int i=1;i<=n;i++) { if(vis[i]) ans += node[i].val; else ans -= node[i].val; } printf("%d\n", ans); //system("pause");}
0 0
- 贪心——The Martian Challenge 2017 #G. Pick Your Team
- The Martian Challenge 2017 E. Secret Passage
- G - Arranging Your Team
- Challenge Assumptions—Especially Your Own
- Challenge Assumptions— Especially Your Own
- hdu 3720——Arranging Your Team
- POJ 2070 Filling Out the Team G++
- 贪心算法—Problem G
- HDU5543——Pick The Sticks(动态规划)
- Making your FIFA 16 final Team with the Legends
- pick off your glasses
- Mikado - pick your transcript
- SDKD 2017 Spring Team Training B G题 -The Debut Album or URAL
- Knowing Your Team
- June Challenge 2017 | Chef and the Feast
- July Challenge 2017 | Whats in the Name
- Magenta——a project from the Google Brain team
- oakley.com Are you up to the 'Love Your River' challenge
- CodeForces
- Pixhawk遥控器无法检测&bootloader烧写
- DNS原理
- bootstrap selectpicker 通过代码指定选中值
- 1054 电梯
- 贪心——The Martian Challenge 2017 #G. Pick Your Team
- [LibreOffice]Calc文档开发_001:Calc文档的创建
- C++中的IO库
- 蓝桥杯-队列操作(java)
- bash笔记(2)
- 关于AS用点9图时遇到的错误的解决方法Error:Execution failed for task ':app:mergeDebugResources'
- 电路笔记
- LeetCode
- sgd与momentum等训练参数方案参考网址