HDU 5590 ZYB’s Biology (水题)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 105;
char s1[maxn],s2[maxn];
int main(){
int T,N;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
scanf("%s%s", s1, s2);
int flag = 1;
for(int i = 0; i < N; i++){
if(!((s1[i] == 'A'&&s2[i] == 'U')||(s1[i] == 'C'&&s2[i] == 'G')||(s1[i] == 'G'&&s2[i] == 'C')||(s1[i] == 'T'&&s2[i] == 'A')))
{flag = 0; break; }
}
if(flag) printf("YES\n");
else printf("NO\n");
}
return 0;
}
HDU 5591 ZYB’s Game
一道博弈论的水题,很容易想,对于1~n的每个点来说,主要就是找关于猜到的那个数对称点的思想,这样就能保证对方每走一步,我都有相对应的策略,但是当n为奇数的时候,位于中值的时候,这时候对方可以利用此种策略让你输,因此就很容易得出结果,不明白的可以用手画一下就懂了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int T,N;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
printf("%d\n", N&1);
}
return 0;
}
HDU 5592 ZYB’s Premutation
很明显a[i] - a[i-1]就是该数前面有多少个逆序对的意思,那么我们从前往后找的话肯定
是不行,因为第一个是任何数逆序数都为0,因此无法继续得到结果,但是我们我们从后往前逆推
的话一定是可行的,对于第i个数,我们知道他的逆序对为a[i]-a[i-1],那么对于当前位置上的数
一定是剩余没有放完的位置中第i-(a[i]-a[i-1])大的数,因为后面放的数对于前面的数可以看做
是没有任何影响的,那么这样我们就可以对线段树进行更新了线段树中每个节点表示对于当前区间
还有多少个空位置没有放,因此这样就很简单了,直接利用线段树进行处理,得到第i个位置的数
所在的那个序列中的位置,然后得出结论。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50005;
#define mid (L+R)/2
#define lson o<<1, L, mid
#define rson o<<1|1, mid+1, R
int a[maxn],loc[maxn],sum[maxn<<2];
void build(int o,int L,int R){
sum[o] = R-L+1;
if(L == R) return ;
build(lson);
build(rson);
}
int update(int o,int L,int R,int p){
if(L == R){
sum[o] = 0;
return L;
}
int ret;
if(sum[o<<1] >= p) ret = update(lson, p);
else ret = update(rson, p-sum[o<<1]);
sum[o] = sum[o<<1] + sum[o<<1|1];
return ret;
}
int main(){
int T,N;
scanf("%d", &T);
while(T--){
scanf("%d", &N);
build(1, 1, N);
for(int i = 1; i <= N; i++) scanf("%d", &a[i]);
for(int i = N; i >= 1; i--){
int x = i-(a[i]-a[i-1]);
int location = update(1,1,N,x);
loc[i] = location;
}
for(int i = 1; i <= N; i++)
printf("%d%c", loc[i], i==N?'\n':' ');
}
return 0;
}
HDU 5593 ZYB’s Tree
树形dp的题目,这道题建树就不是那么直接给出边了,而是需要利用给出的方程建立边,因为要求出每个点到其距离小于等于K的结点的个数,很明显我们第一遍dp可以找出在子树上距离其为K的节点数,但是很明显在父亲节点上也是存在的,因此再跑一边dfs就可以求出来了。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 500005;
int dp[maxn][11],head[maxn],tot;
struct Edge{
int to,next;
}e[maxn];
void addedge(int from,int to){
e[tot].to = to;
e[tot].next = head[from];
head[from] = tot++;
}
void dfs1(int u,int K){ //由子推父亲
dp[u][0] = 1;
for(int i = head[u]; i != -1; i = e[i].next){
int v = e[i].to;
dfs1(v, K);
for(int j = 1; j <= K; j++)
dp[u][j] += dp[v][j-1];
}
}
void dfs2(int u,int K){ //由父亲推儿子
for(int i = head[u]; i != -1; i = e[i].next){
int v = e[i].to;
for(int j = K; j >= 1; j--) //注意这个地方一定是反向的,不然出错,至于为什么,自己可以好好想一下
if(j == 1) dp[v][j] += dp[u][j-1];
else dp[v][j] += dp[u][j-1] - dp[v][j-2];
dfs2(v, K);
}
}
void init(){
tot = 0;
memset(head, -1, sizeof(head));
memset(dp, 0, sizeof(dp));
}
int main(){
int T,N,K,A,B;
scanf("%d", &T);
while(T--){
init();
scanf("%d%d%d%d", &N, &K, &A, &B);
for(int to = 2; to <= N; to++){
int from = (1LL*A*to + B)%(to-1) + 1;
addedge(from, to);
}
dfs1(1, K);
dfs2(1, K);
int sum = 0;
for(int i = 1; i <= N; i++){
for(int j = 0; j <= K-1; j++) dp[i][K] += dp[i][j];
sum = sum^dp[i][K];
}
printf("%d\n", sum);
}
return 0;
}