1007.Even Tree Split
题意:
给出一个结点个数为n(n为偶数)的树,问有多少种切割的方案数能使切割后的所有连通块的结点个数均为偶数。
思路:
如果这棵子树的结点个数为偶数,那么就可以切一刀。
先预处理出结点个数为偶数的子树数目ans,那么可以最多可以砍ans-1刀,
最后是排列组合,答案为2^ans - 1刀。
因为容易超时,所以最好不用vector来建边。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10, M = N * 2,mod = 998244353;
int h[N], e[M], ne[M], idx, n;
ll sizes[N], ans;
ll qk(ll a,ll b){
ll ans=1;
a%=mod;
b%=mod;
while(b){
if(b&1) ans=ans*a%mod;
a=a*a%mod;
b>>=1;
b%=mod;
}
return ans;
}
void add(int x, int y){
e[idx] = y, ne[idx] = h[x], h[x] = idx ++;
}
void dfs(int x, int fa){//sizes:表示子树结点个数
for(int i = h[x]; ~i; i = ne[i])
{
int y = e[i];
if(fa == y) continue;
dfs(y, x);
sizes[x] += sizes[y];
}
if(!(sizes[x] & 1)) ++ ans;//如果为偶数,ans++
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(h, -1, sizeof h);
memset(e, 0, sizeof e);
memset(ne, 0, sizeof ne);
idx=0;
ans=0ll;
memset(sizes,0,sizeof(sizes));
scanf("%d", &n);
for(int i = 1; i < n; i ++)
{
int x, y;
scanf("%d%d", &x, &y);
add(x, y), add(y, x);//建边
}
for(int i = 1; i <= n; i ++) sizes[i] = 1ll;//初始为每个结点的子树结点个数为1
dfs(1, 0);//dfs
ans = (qk(2,ans-1)%mod - 1 +mod)%mod;//有ans个结点个数为偶数的子树,那么可以砍ans-1刀,最后答案为2^ans - 1。
printf("%lld\n",ans);
}
return 0;
}
1003.Wavy Tree
题意:
给出一个长度为n的数组,每次可以使用一个硬币让一个数+1或者-1,
问最少使用多少个硬币可以将这个数组变成摆动数组。
思路:
只要确定第一个数后面的数就都能确定了。
那么将第一个数分成两种情况:
(1)第一个数为波峰
(2)第一个数为波谷
取两者得到的结果的最小值。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6+10;
int n;
ll x,ans1,ans2,a[N],pre1,pre2;
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
ans1=0ll,ans2=0ll;
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
if(i==1) pre1=a[1],pre2=a[1];
else{
if(i%2==0){
//a[1]为波峰:
if(a[i]>=pre1) pre1-=1ll,ans1+=abs(pre1-a[i]);
else pre1=a[i];
//a[1]为波谷
if(a[i]<=pre2) pre2+=1ll,ans2+=abs(pre2-a[i]);
else pre2=a[i];
}
else{
if(a[i]<=pre1) pre1+=1ll,ans1+=abs(pre1-a[i]);
else pre1=a[i];
if(a[i]>=pre2) pre2-=1ll,ans2+=abs(pre2-a[i]);
else pre2=a[i];
}
}
}
printf("%lld\n",min(ans1,ans2));
}
return 0;
}
1009.Painting Game
题意:
有一排n个空格子,Alice和Bob轮流进行操作,每次选择一个空格将其涂成黑色,不能让两个黑色格子相邻。Alice想让黑格子尽量少,Bob想让黑格子尽量少。现在给出n以及先手,问最后的格子数目是多少。
思路:
先暴力打表,打出前20个数据,然后看数据找规律,看看有没有递推公式啥的。由于Alice想最小化,那么在手算的过程中尽量间隔两格走,Alice先手时,当n>7时Alice从第2个格子开始走。Bob想实现最大化,他是先手时尽量先占领第3个格子和倒数第3个格子,这样第一个格子和最后一个格子一定会被涂上黑色。
Alice: 1 1 1 2 2 3 3 4 4 4 5 5 6 6 7 7 7 8 8 9
Bob: 1 1 2 2 3 3 3 4 4 5 5 6 6 6 7 7 8 8 9 9
发现对于Alice:f[n]=f[n-7]+3;
对于Bob:g[n]=g[n-7]+3;
因此我们可以预处理出前7个数字的数组,运用取模和乘除运算得出答案。
注意:
如果对cin,cout进行优化,用了
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
就不要混用scanf,否则会输入错误。
详见Link
代码:
#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int a[10]={0,1,1,1,2,2,3,3};
int b[10]={0,1,1,2,2,3,3,3};
int main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--){
cin>>n>>s;
if(s=="Alice") printf("%d\n",a[n%7]+n/7*3);
else printf("%d\n",b[n%7]+n/7*3);
}
return 0;
}
1004.Average Replacement
题意:
每个人有一顶帽子,帽子上有一个权值。
每次这个人的帽子的权值会变成他和他的朋友的权值之和再除以他和他朋友的数量。
最后每个人的权值都会收敛到一个固定值。
问最后每个人的帽子的权值是多少。
思路:
因为每一个连通块里的数字会相互影响,因此最好一个连通块里的数字会趋近同一个值。
计算每个人的加权值。sum a[i]*(num[i]+1)是不变的,son = sum (num[i]+1)用这个连通块的这个累计值除以son就是这个连通块趋近的值。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
int n,m,fa[N];
ll a[N],s[N],son[N],num[N];
int find(int x){
if(x==fa[x]) return x;
return fa[x]=find(fa[x]);
}
void merge(int x,int y){
fa[find(x)]=find(y);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d", &n,&m);
for(int i=0;i<=n;i++) {
fa[i]=i;
}
memset(s,0,sizeof(s));
memset(son,0,sizeof(son));
memset(num,0,sizeof(num));
for(int i = 1; i <= n; i ++) {
scanf("%lld",&a[i]);
}
for(int i = 1; i <=m; i ++)
{
int x, y;
scanf("%d%d", &x, &y);
merge(x,y);
num[x]++;
num[y]++;
}
for(int i=1;i<=n;i++){
int x=find(i);
s[x]+=a[i]*(num[i]+1);
son[x]+=(num[i]+1);
}
for(int i=1;i<=n;i++){
int x=find(i);
printf("%.6lf\n",s[x]*1.0/son[x]);
}
}
return 0;
}