打牌
题目背景:
分析:DP or 贪心
本喵表示,这种小数据死都拍不出错,大数据随随便便就错的题,我也是喵了个咪的······打了对拍,打了暴力,然后拍了几个小时,卵用······不吐槽了,本喵今天这个题只有30分···贪心错成智障······
然后,标算还是贪心,首先我们知道出对子肯定更加好,我们直接记录一个cnt数组,cnt[x]表示x有多少个,然后先将1,2能出对子的尽量对子,然后从3开始往后枚举,如果前面两个都有多余的一张单排,则取出当前的一张出一个顺子,因为这样做,如果当前也有单牌,则答案会加1,如果当前本来是顺子,那么答案不会变,并且为后面制造了多1的,可能性,所以扫一遍就好啦,听说还能能够DP做,状态表示为,dp[i][j]为,当前枚举到i然后有j张的最优解,因为不能开下二维数组,那么可以用vector模拟动态数组来转移即可。
贪心:
Source:
/*
created by scarlyw
*/
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cctype>
#include <vector>
#include <queue>
#include <map>
#include <set>
inline char read() {
static const int IN_LEN = 1024 * 1024;
static char buf[IN_LEN], *s, *t;
if (s == t) {
t = (s = buf) + fread(buf, 1, IN_LEN, stdin);
if (s == t) return -1;
}
return *s++;
}
/*
template<class T>
inline void R(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = read(); !isdigit(c); c = read()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1);
if (iosig) x = -x;
}
//*/
const int OUT_LEN = 1024 * 1024;
char obuf[OUT_LEN], *oh = obuf;
inline void write_char(char c) {
if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf;
*oh++ = c;
}
template<class T>
inline void W(T x) {
static int buf[30], cnt;
if (x == 0) write_char('0');
else {
if (x < 0) write_char('-'), x = -x;
for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48;
while (cnt) write_char(buf[cnt--]);
}
}
inline void flush() {
fwrite(obuf, 1, oh - obuf, stdout);
}
///*
template<class T>
inline void R(T &x) {
static bool iosig;
static char c;
for (iosig = false, c = getchar(); !isdigit(c); c = getchar()) {
if (c == -1) return ;
if (c == '-') iosig = true;
}
for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0');
if (iosig) x = -x;
}
//*/
const int MAXN = 1000000 + 10;
int n, x, ans;
int cnt[MAXN];
int main() {
// freopen("poke.in", "r", stdin);
// freopen("poke.out", "w", stdout);
R(n);
for (int i = 1; i <= n; ++i) R(x), cnt[x]++;
ans += cnt[1] / 2, cnt[1] %= 2, ans += cnt[2] / 2, cnt[2] %= 2;
for (int i = 3; i < MAXN; ++i) {
if ((cnt[i - 2] & 1) && (cnt[i - 1] & 1) && cnt[i])
ans++, cnt[i - 2]--, cnt[i - 1]--, cnt[i]--;
ans += cnt[i] / 2, cnt[i] %= 2;
}
std::cout << ans;
return 0;
}
DP(膜拜thhyj大神代码):
Source:
#include<bits/stdc++.h>
using namespace std;
inline void R (int &v){
static char ch;
v = 0;
bool p = 0;
do {
ch = getchar();
if(ch == '-') p = 1;
} while(!isdigit(ch));
while(isdigit(ch)) {
v = (v + (v << 2) << 1) + (ch^'0');
ch = getchar();
}
if(p) v = -v;
}
int n;
int a[1000005];
vector<int> dp[1000005];
int tong[1000005];
int main() {
freopen("poke.in","r",stdin);
freopen("poke.out","w",stdout);
R(n);
int ma = 0;
for(register int i = 1; i <= n; ++i) {
R(a[i]);
tong[a[i]]++;
ma = max(ma, a[i]);
}
dp[0].push_back(0);
int temp;
for(register int i = 1; i <= ma; ++i) {
dp[i].push_back(dp[i - 1][tong[i - 1]]);
for(register int j = 1; j <= tong[i]; ++j ) {
dp[i].push_back(dp[i][j - 1]);
if(j > 1) dp[i][j] = max(dp[i][j], dp[i][j - 2] + 1);
if(i > 2 && j && tong[i - 2] && tong[i - 1]) {
dp[i][j] = max(dp[i][j], dp[i - 2][tong[i - 2] - 1] +
((tong[i - 1] - 1) >> 1) + ((j + 1) >> 1));
}
}
}
printf("%d",dp[ma][tong[ma]]);
return 0;
}