标签:枚举 summary float type 贪心 second n+1 mit ios
\[
Time Limit: 1 s\quad Memory Limit: 256 MB
\]
计算每一个 \(A\) 后面有多少个连续的 \(P\),然后取最大。
view
/***************************************************************
> File Name : a.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2020/1/5 21:56:38
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
char s[maxn];
int main() {
// freopen("in", "r", stdin);
scanf("%d", &T);
while(T--) {
scanf("%d", &n);
scanf("%s", s+1);
s[n+1] = 'A';
int ans = 0;
for(int i=1; i<=n; i++) {
if(s[i] == 'P') continue;
for(int j=i+1; j<=n+1; j++) {
if(s[j] == 'A') {
ans = max(ans, j-i-1);
break;
}
}
}
printf("%d\n", ans);
}
return 0;
}
\[
Time Limit: 3 s\quad Memory Limit: 256 MB
\]
\(N^2\) 枚举两个字符串,此时可以直接确定第三个字符串是什么样,然后找有多少个即可。
view
/***************************************************************
> File Name : b.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2020/1/5 22:14:08
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 2e3 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
unordered_map<string ,int> mp;
string s[maxn];
ll calc(string s1, string s2) {
int cnt = 0;
string s3 = "";
for(int i=0; i<m; i++) {
if(s1[i] != s2[i]) s3 += 'S'+'E'+'T'-s1[i]-s2[i];
else s3 += s1[i];
}
return mp[s3];
}
int main() {
// freopen("in", "r", stdin);
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++) {
cin >> s[i];
mp[s[i]]++;
}
ll ans = 0;
for(int i=1; i<=n; i++ ){
for(int j=i+1; j<=n; j++) {
ans += calc(s[i], s[j]);
}
}
printf("%lld\n", ans/3);
return 0;
}
\[
Time Limit: 1 s\quad Memory Limit: 256 MB
\]
一开始想歪了,以为贪心就可以了,然后越走越远。。。
令 \(dp[i][j][k][0/1]\) 表示从 \(1-i\) 位置,手上还有 \(j\) 个奇数和 \(k\) 个偶数,并且第 \(i\) 位置当奇数或者偶数的最小代价。然后暴力转移即可。
view
/***************************************************************
> File Name : c.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2020/1/5 23:35:34
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int x, y;
int a[maxn];
ll dp[105][105][105][2];
int main() {
// freopen("in", "r", stdin);
scanf("%d", &n);
x = (n+1)/2;
y = n/2;
for(int i=1; i<=n; i++) {
scanf("%d", &a[i]);
if(a[i]) {
if(a[i]%2) x--;
else y--;
}
}
for(int i=1; i<=n; i++) for(int j=0; j<=x; j++) for(int k=0; k<=y; k++)
dp[i][j][k][0] = dp[i][j][k][1] = INF;
if(a[1]) dp[1][x][y][a[1]%2] = 0;
else {
dp[1][x-1][y][1] = 0;
dp[1][x][y-1][0] = 0;
}
for(int i=1; i<n; i++) {
for(int j=0; j<=x; j++) {
for(int k=0; k<=y; k++) {
if(a[i+1]) {
if(dp[i][j][k][0] != INF) {
dp[i+1][j][k][a[i+1]%2] = min(dp[i+1][j][k][a[i+1]%2], dp[i][j][k][0]+(a[i+1]%2==1));
}
if(dp[i][j][k][1] != INF) {
dp[i+1][j][k][a[i+1]%2] = min(dp[i+1][j][k][a[i+1]%2], dp[i][j][k][1]+(a[i+1]%2==0));
}
} else {
if(dp[i][j][k][0] != INF) {
if(j) dp[i+1][j-1][k][1] = min(dp[i+1][j-1][k][1], dp[i][j][k][0]+1);
if(k) dp[i+1][j][k-1][0] = min(dp[i+1][j][k-1][0], dp[i][j][k][0]);
}
if(dp[i][j][k][1] != INF) {
if(j) dp[i+1][j-1][k][1] = min(dp[i+1][j-1][k][1], dp[i][j][k][1]);
if(k) dp[i+1][j][k-1][0] = min(dp[i+1][j][k-1][0], dp[i][j][k][1]+1);
}
}
}
}
}
printf("%lld\n", min(dp[n][0][0][0], dp[n][0][0][1]));
return 0;
}
\[
Time Limit: 1 s\quad Memory Limit: 256 MB
\]
第 \(i\) 个节点是其所有子树节点中第 \(c[i]\) 大的,那么把这所有的节点标号从 \(1-sz\),也就意味着需要有一部分子树标号从 \(1->c[i]-1\),\(i\) 标号成 \(c[i]\),剩下子树节点标号为 \(c[i]+1->sz\),那么可以用 \(vector\) 从下往上处理,将 \(i\) 的所有子树的 \(vector\) 拼接起来,然后再把 \(i\) 插入到 \(c[i]\) 位置。这样既保证了每颗子树的相对顺序,又保证的 \(i\) 这个节点是合法的。
而不合法的情况只有一种,就是 \(c[i]\) 比整颗子树都大。
view
/***************************************************************
> File Name : d.cpp
> Author : Jiaaaaaaaqi
> Created Time : 2020/1/7 21:34:09
***************************************************************/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <unordered_map>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define dbg(x) cout << #x << " = " << (x) << endl
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
int a[maxn], c[maxn];
vector<int> g[maxn];
vector<int> dfs(int u) {
vector<int> ans;
for(auto v : g[u]) {
vector<int> tmp = dfs(v);
ans.insert(ans.end(), tmp.begin(), tmp.end());
}
if(ans.size() < c[u]) exit(0*puts("NO"));
ans.insert(ans.begin()+c[u], u);
return ans;
}
int main() {
// freopen("in", "r", stdin);
scanf("%d", &n);
int root = 0;
for(int i=1, p; i<=n; i++) {
scanf("%d%d", &p, &c[i]);
if(p) g[p].pb(i);
else root = i;
}
vector<int> ans = dfs(root);
for(int i=0; i<ans.size(); i++) a[ans[i]] = i+1;
printf("YES\n");
for(int i=1; i<=n; i++) printf("%d%c", a[i], i==n ? '\n':' ');
return 0;
}
Codeforces Round #612 (Div. 2)
标签:枚举 summary float type 贪心 second n+1 mit ios
原文地址:https://www.cnblogs.com/Jiaaaaaaaqi/p/12163953.html