题目描述
Bessie the cow is a huge fan of card games, which is quite surprising, given her lack of opposable thumbs. Unfortunately, none of the other cows in the herd are good opponents. They are so bad, in fact, that they always play in a completely predictable fashion! Nonetheless, it can still be a challenge for Bessie to figure out how to win.
Bessie and her friend Elsie are currently playing a simple card game where they take a deck of 2N cards, conveniently numbered 1…2N, and divide them into N cards for Bessie and N cards for Elsie. The two then play N rounds, where in each round Bessie and Elsie both play a single card. In the first N/2 rounds, the player with the highest card earns a point, and in the last N/2 rounds, the rules switch and the player who plays the lowest card wins a point.
Given that Bessie can predict the order in which Elsie will play her cards, please determine the maximum number of points Bessie can win.
输入描述:
The first line of input contains the value of N (; will be even).The next N lines contain the cards that Elsie will play in each of the successive rounds of the game. Note that it is easy to determine Bessie's cards from this information.
输出描述:
Output a single line giving the maximum number of points Bessie can score.
示例1
输入4
1
8
4
3
输出
2
说明
Here, Bessie must have cards 2, 5, and 6, and 7 in her hand, and she can use these to win at most 2 points, by saving her '2' card until one of the hands in the second half of the game.
解答
显然推断两人手上的牌,自己手上的牌较大的一半放在前面用,较小的一半放在后面用,贪心算一下积分
#include <map> #include <set> #include <ctime> #include <cmath> #include <queue> #include <stack> #include <vector> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <sstream> #include <iostream> #include <algorithm> #include <functional> using namespace std; #define For(i, x, y) for(int i=x;i<=y;i++) #define _For(i, x, y) for(int i=x;i>=y;i--) #define Mem(f, x) memset(f,x,sizeof(f)) #define Sca(x) scanf("%d", &x) #define Sca2(x,y) scanf("%d%d",&x,&y) #define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define Scl(x) scanf("%lld",&x); #define Pri(x) printf("%d\n", x) #define Prl(x) printf("%lld\n",x); #define CLR(u) for(int i=0;i<=N;i++)u[i].clear(); #define LL long long #define ULL unsigned long long #define mp make_pair #define PII pair<int,int> #define PIL pair<int,long long> #define PLL pair<long long,long long> #define pb push_back #define fi first #define se second typedef vector<int> VI; int read(){int x = 0,f = 1;char c = getchar();while (c<'0' || c>'9'){if (c == '-') f = -1;c = getchar();} while (c >= '0'&&c <= '9'){x = x * 10 + c - '0';c = getchar();}return x*f;} const double eps = 1e-9; const int maxn = 1e5 + 10; const int INF = 0x3f3f3f3f; const int mod = 1e9 + 7; int N,M,K; bool vis[maxn]; int a[maxn],b[maxn]; int main(){ Sca(N); for(int i = 1; i <= N ; i ++){ Sca(a[i]); vis[a[i]] = 1; } int cnt = 0; for(int i = 2 * N; i >= 1 ; i --) if(!vis[i]) b[++cnt] = i; int m = N / 2; sort(a + 1,a + 1 + m); sort(b + 1,b + 1 + m); sort(a + 1 + m,a + 1 + N); sort(b + 1 + m,b + 1 + N); int ans = 0,p = 1; for(int i = 1; i <= m ; i ++){ if(b[i] > a[p]){ ans++; p++; } } p = N; for(int i = N; i >= m + 1; i --){ if(b[i] < a[p]){ p--; ans++; } } Pri(ans); return 0; }
来源:Hugh_Locke