Kuma Rider久违的第二场训练,这场很水,又在vj的榜单上看到第一场的大哥了,2小时ak,大哥牛啤!
A.水
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
const double eps = 1e-; int T;
char s[];
int main(void)
{
scanf("%d", &T);
getchar();
while (T--)
{
gets(s);
int len = strlen(s);
int p;
char luo[];
for(int i=;i<=;i++)luo[i]=;
bool fu = false;
int num1 = ;
for (p = ; p < len; p++)
{
//printf("%d %c\n", p, s[p]);
if (s[p] == '-')fu = true;
else if (s[p] >= ''&&s[p] <= '')
{
num1 *= ;
num1 += s[p] - '';
}
else if (s[p] == ' ')continue;
else
{
int tot = ;
while (s[p] != ' ')
{
luo[tot++] = s[p]; p++; }
break;
}
//printf("%d %c\n", p, s[p]);
} if (fu)num1 = -num1;
int num2 = ;
fu = false;
for (p = p + ; p < len; p++)
{
if (s[p] == '-')fu = true;
else if (s[p] >= ''&&s[p] <= '')
{
num2 *= ;
num2 += s[p] - '';
}
}
if (fu)num2 = -num2; if (luo[] == '<')
{
if (luo[] == '=')
{
if (num1 <= num2)printf("true\n");
else printf("false\n");
}
else
{
if (num1 < num2)printf("true\n");
else printf("false\n");
}
}
else if (luo[] == '>')
{
if (luo[] == '=')
{
if (num1 >= num2)printf("true\n");
else printf("false\n");
}
else
{
if (num1 > num2)printf("true\n");
else printf("false\n");
}
}
else if (luo[] == '!')
{
if (num1 != num2)printf("true\n");
else printf("false\n");
}
else
{
if (num1 == num2)printf("true\n");
else printf("false\n");
} } }
B.水
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
const double eps = 1e-; int main(void) {
int t;
scanf("%d", &t);
while (t--) {
int a;
int sum = ;
int fla = ;
for (int i = ; i < ; i++) {
scanf("%d", &a);
if (a <= ) fla = ;
sum += a;
}
if (fla || sum != ) printf("NO\n");
else printf("YES\n");
}
return ;
}
C.背包
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
const double eps = 1e-;
const int inf = 0x3f3f3f3f; int a[];
int dp[];
int sum;
int main(void) {
int t;
scanf("%d", &t);
while (t--)
{
int K, m, n;
sum = ;
scanf("%d%d%d", &n, &m, &K);
for (int i =; i <= K; i++)
{
scanf("%d", &a[i]);
sum += a[i];
}
int sh = n - sum;
if (m <= sh)printf("0\n");
else
{
int need = m - sh;
memset(dp, inf, sizeof(dp));
dp[] = ;
for (int i = ; i <= K; i++)
{
for (int j = sum; j >= a[i]; j--)
{
dp[j] = min(dp[j - a[i]] + , dp[j]);
}
}
int ans = inf;
for (int i = need; i <= sum; i++)
{
ans = min(ans, dp[i]);
}
printf("%d\n", ans); } }
}
D.判断数独对不对(队友没有用函数,暴力得就很真实)
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
const double eps = 1e-;
const int inf = 0x3f3f3f3f;
//
//int a[105];
//int dp[33768];
//int sum;
//int main(void) {
// int t;
// scanf("%d", &t);
// while (t--)
// {
// int K, m, n;
// sum = 0;
// scanf("%d%d%d", &n, &m, &K);
// for (int i =1; i <= K; i++)
// {
// scanf("%d", &a[i]);
// sum += a[i];
// }
// int sh = n - sum;
// if (m <= sh)printf("0\n");
// else
// {
// int need = m - sh;
// memset(dp, inf, sizeof(dp));
// dp[0] = 0;
// for (int i = 1; i <= K; i++)
// {
// for (int j = need; j >= 0; j--)
// {
// dp[j] = min(dp[j - a[i]] + 1, dp[j]);
// }
// }
// printf("%d\n", dp[need]);
//
// }
//
// }
//}
// char ch[][];
int vis[];
int main(void) {
int t;
scanf("%d", &t);
while (t--) {
getchar();
int fla = ;
for (int i = ; i <= ; i++) {
scanf("%s", ch[i]+);
}
for (int i = ; i <= ; i++) {
memset(vis, , sizeof(vis));
int sum = ;
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
if (sum != ) fla = ;
}
for (int i = ; i <= ; i++) {
memset(vis, , sizeof(vis));
int sum = ;
for (int j = ; j <= ; j++) {
if (!vis[ch[j][i] - '']) {
vis[ch[j][i] - ''] = ;
sum++;
}
}
if (sum != ) fla = ;
}
int sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <=; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
sum = ;
memset(vis, , sizeof(vis));
for (int i = ; i <= ; i++) {
for (int j = ; j <= ; j++) {
if (!vis[ch[i][j] - '']) {
vis[ch[i][j] - ''] = ;
sum++;
}
}
}
if (sum != ) fla = ;
if (fla) printf("Invalid\n");
else {
printf("Valid\n");
}
}
}
E.树状数组
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std; const int maxn = ;
int c[maxn], a[maxn];
int n;
int lowbit(int x) {
return x&(-x);
}
void update_onepos(int pos, int x) {
while (pos <= n) {
c[pos] += x;
pos += lowbit(pos);
}
}
int getsum_onepos(int pos) {
int sum = ;
while (pos > ) {
sum += c[pos];
pos -= lowbit(pos);
}
return sum;
}
int getsum_range(int x1, int x2) {
return getsum_onepos(x2) - getsum_onepos(x1-);
}
int ax[];
int main(void) {
int t;
scanf("%d", &t);
while (t--) {
int nn;
n = -;
scanf("%d", &nn);
memset(c, , sizeof(c));
for (int i = ; i < nn; i++) {
scanf("%d", &ax[i]);
n = max(ax[i], n);
}
n += ;
for (int i = ; i < nn; i++) {
update_onepos(ax[i], );
}
int sum = ;
for (int i = ; i < nn; i++) {
sum += getsum_range(max(, ax[i] - ), ax[i])-;
sum += getsum_range(ax[i] + , ax[i] + );
}
printf("%d\n", sum / );
}
return ;
}
F.并查集,最后统计根节点的问号数量
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
typedef long long LL;
const int maxv = ;
const LL mod = 1e9 + ; int T, n, m;
char s[maxv];
bool hav ;
struct Node
{
int pos;
int fa;
char ch;
}tree[]; int find(int nx)
{
if (tree[nx].fa == nx)return nx; int ff = find(tree[nx].fa);
tree[nx].ch = tree[ff].ch;
return tree[nx].fa=ff;
} bool vis[]; void init()
{
hav = false;
scanf("%d %d", &n, &m);
scanf("%s", s + );
for (int i = ; i <= n; i++)
{
if (s[i] == '?')hav = true;
tree[i].fa = i;
tree[i].ch = s[i];
tree[i].pos = i;
vis[i] = false;
}
} int main(void)
{
scanf("%d", &T);
while (T--)
{
init();
bool can = true; for (int i = ; i <= m; i++)
{
int u, v;
scanf("%d %d", &u, &v);
int fx = find(u), fy = find(v);
if (fx != fy)
{
//printf("%d %d\n", fx, fy);
if (tree[fx].ch!='?'&&tree[fy].ch!='?')
{
if (tree[fx].ch != tree[fy].ch)can = false;
}
else if (tree[fx].ch == '?'&&tree[fy].ch != '?')
{
tree[fx].fa = fy;
tree[fx].ch = tree[fy].ch;
}
else if (tree[fx].ch != '?'&&tree[fy].ch == '?')
{
tree[fy].fa = fx;
tree[fy].ch = tree[fx].ch;
}
else
{
tree[fx].fa = fy;
tree[fx].ch = tree[fy].ch;
}
}
} int l = , r = n;
while (l <= r)
{
int fx = find(l), fy = find(r);
if (fx != fy)
{
//printf("%d %d\n", fx, fy);
if (tree[fx].ch != '?'&&tree[fy].ch != '?')
{
if (tree[fx].ch != tree[fy].ch)can = false;
}
else if (tree[fx].ch == '?'&&tree[fy].ch != '?')
{
tree[fx].fa = fy;
tree[fx].ch = tree[fy].ch;
}
else if (tree[fx].ch != '?'&&tree[fy].ch == '?')
{
tree[fy].fa = fx;
tree[fy].ch = tree[fx].ch;
}
else
{
tree[fx].fa = fy;
tree[fx].ch = tree[fy].ch;
}
}
l++, r--;
} if (!can){printf("0\n"); continue;} for (int i = ; i <= n; i++)
{
int fx = find(i);
s[i] = tree[fx].ch;
} //printf("%s\n", s + 1); LL ans = ;
bool tag = true; for (int i = ; i <= n; i++)
{
int fx = find(i);
if (!vis[fx])
{
if (tree[fx].ch == '?')
{
ans *= ;
ans %= mod;
}
vis[fx] = true;
}
} if (tag)
{
if (hav&&ans == )printf("1\n");
else if(!hav)printf("0\n");
else printf("%lld\n", ans);
}
else printf("0\n"); }
return ;
}
G.状压dp
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<functional>
#include<vector>
using namespace std;
const int maxv = ; int T, n, m;
string s;
map<string, int>mp;
int in[maxv], w[maxv];
int dp[ << ]; int main()
{
cin >> T;
while (T--)
{
cin >> n >> m;
memset(dp, -, sizeof(dp));
getchar();
for (int i = ; i <= n; i++)
{
in[i] = ;
w[i] = ;
}
mp.clear();
for (int i = ; i < n; i++)
{
getline(cin, s);
int pos = -;
for (int j = ; j < s.size(); j++)
{
if (s[j] <= ''&&s[j] >= '')
{
pos = j;
break;
}
}
string tmp = s.substr(, pos - );
int ww = ;
string shuzi = s.substr(pos, s.size() - pos);
for (int j = ; j < shuzi.size(); j++)
{
ww = ww * + shuzi[j] - '';
}
mp[tmp] = i;
w[i] = ww;
//cout << tmp << " " << ww << endl;
}
for (int i = ; i <= m; i++)
{
getline(cin, s);
int pos1 = -;
for (int i = ; i < s.size(); i++)
{
if (s[i] == '-')
{
pos1 = i;
break;
}
}
string t1 = s.substr(, pos1 - );
string t2 = s.substr(pos1 + , s.size() - pos1 - );
int id1 = mp[t1];
int id2 = mp[t2];
in[id2] |= ( << id1);
}
dp[] = ;
for (int j = ; j < ( << n) - ; j++)
{
if (dp[j] < )
{
continue;
}
int num = ;
for (int i = ; i < n; i++)
{
num += (j >> i & );
}
for (int i = ; i < n; i++)
{
int now = j | ( << i);
if (j != now && (in[i] & j) == in[i])
{
dp[now] = max(dp[now], dp[j] + (num + )*w[i]);
}
}
}
cout << dp[( << n) - ] << endl;
}
return ;
} /*
1
3 2
Implementation 3
Dynamic Programming 10
Greedy 7
Greedy --> Dynamic Programming
Implementation --> Dynamic Programming
*/
H.模板题,待施工