Codeforces Round #429 (Div. 2/Div. 1) [ A/_. Generous Kefa ] [ B/_. Godsend ] [ C/A. Leha and Function ] [ D/B. Leha and another game about graph ] [ E/C. On the Bench ] [ _/D. Destiny ]
PROBLEM A/_ - Generous Kefa
题
OvO http://codeforces.com/contest/841/problem/A
cf 841a
解
只要不存在某个字母,它的个数大于k,即可
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath> using namespace std; int n,k;
char str[333];
int mx;
int cnt[33]; void solve()
{
for(int i=0;i<26;i++)
if(cnt[i]>k)
{
printf("NO\n");
return ;
}
printf("YES\n");
} int main()
{
cin>>n>>k;
cin>>str;
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++)
cnt[str[i]-'a']++;
solve();
return 0;
}
PROBLEM B/_ - Godsend
题
OvO http://codeforces.com/contest/841/problem/B
cf 841b
解
如果一开始总数就是奇数,明显第一个玩家赢
如果一开始总数是偶数,而且不存在奇数,明显第二个玩家赢
如果一开始总数是偶数,且存在奇数,那么第一个玩家可以取,取完之后变成奇数,明显第二个玩家取不完,
若此后第二个玩家不可取,则第一个玩家胜利
若此后第二个玩家可以取,那么取完之后总数为奇数,第一个玩家可以取完,第一个玩家胜利。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath> using namespace std; typedef long long ll; const int M=1e6+44; int s[M];
int n; int main()
{
int i,j,flag=false;
ll sum=0;
cin>>n;
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
for(i=1;i<=n;i++)
{
sum+=s[i];
if(s[i]&1)
flag=true;
}
if(sum&1)
printf("First\n");
else
{
if(flag)
printf("First\n");
else
printf("Second\n");
}
return 0;
}
PROBLEM C/A - Leha and Function
题
OvO http://codeforces.com/contest/841/problem/C
cf 841c
cf 840a
解
做几组简单的样例,找个规律发现A从小到大排,B从大到小排,对应取,则为最优解。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath> using namespace std; typedef long long ll; const int M=2e5+44; struct node
{
int id,val;
} b[M],a[M]; int n;
int ans[M]; bool cmpd(node x,node y)
{
return x.val>y.val;
} bool cmpx(node x,node y)
{
return x.val<y.val;
} void solve()
{
int i,j;
sort(a+1,a+n+1,cmpx);
sort(b+1,b+n+1,cmpd);
for(i=1;i<=n;i++)
ans[b[i].id]=a[i].val;
for(i=1;i<=n;i++)
{
if(i-1) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
} int main()
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
a[i].id=i,b[i].id=i;
for(i=1;i<=n;i++)
scanf("%d",&a[i].val);
for(i=1;i<=n;i++)
scanf("%d",&b[i].val);
solve();
return 0;
}
PROBLEM D/B - Leha and another game about graph
题
OvO http://codeforces.com/contest/841/problem/D
cf 841d
cf 840b
解
如果能每次选2个d为1或者-1的点,将他们之间的某条路径翻转。
那么得到的结果肯定合法,否则的话,在翻转之后,肯定存在一个连通块,这个连通块中存在一个d为1的节点没有配对,这个时候肯定无解。
所以判断有无解的话,只要判断每个连通块中是否能够完全配对(如果没法配对肯定是不存在d为-1的节点,而且d为1的节点为奇数个)
那么这样的话,其实可以把这个图缩减成一个树林,
剩下就是对每个树做差分来判断如何取边(对每个匹配上的节点标记tag=1,dfs回溯的时候每次取当前节点子树的tag的异或和,如果为1,则当前节点向上的那条边必定是要连的)
( 思路来源 <-click here)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const int M=3e5+44; struct node{
int u,v,id;
int next;
}edge[2*M]; int n,m,num;
int d[M];
int head[M];
int ma[M];
int tag[M];
vector<int> ans,v1[M],vx[M]; void addedge(int u,int v,int id)
{
edge[num].u=u;
edge[num].v=v;
edge[num].id=id;
edge[num].next=head[u];
head[u]=num++;
} void init()
{
num=0;
memset (head,-1,sizeof(head));
for(int i=1;i<=n;i++)
ma[i]=i,v1[i].clear(),vx[i].clear();
ans.clear();
memset(tag,0,sizeof(tag));
} int findma(int x)
{
if(ma[x]==x)
return x;
ma[x]=findma(ma[x]);
return ma[x];
} void dfs(int rt,int ma,int id)
{
int i,j,v,tmp;
for(i=head[rt];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(v==ma) continue;
dfs(v,rt,edge[i].id);
tag[rt]^=tag[v];
}
if(tag[rt] && id!=-1)
ans.push_back(id);
} bool solve()
{
int i,j;
for(i=1;i<=n;i++)
if(d[i]==1)
v1[findma(i)].push_back(i);
else if(d[i]==-1)
vx[findma(i)].push_back(i);
for(i=1;i<=n;i++)
if(ma[i]==i)
{
if(v1[i].size()&1 && vx[i].size()==0)
return false;
for(j=0;j<v1[i].size();j++)
tag[v1[i][j]]=1;
if(v1[i].size()&1)
tag[vx[i][0]]=1;
dfs(i,-1,-1);
}
return true;
} int main()
{
int i,j,a,b,pa,pb;
scanf("%d%d",&n,&m);
init();
for(i=1;i<=n;i++)
scanf("%d",&d[i]);
for(i=1;i<=m;i++)
{
scanf("%d%d",&a,&b);
pa=findma(a); pb=findma(b);
if(pa!=pb)
{
addedge(a,b,i);
addedge(b,a,i);
ma[pa]=pb;
}
}
if(solve())
{
// cout<<"answer exist\n";
sort(ans.begin(),ans.end());
printf("%d\n",ans.size());
for(i=0;i<ans.size();i++)
{
if(i) printf(" ");
printf("%d",ans[i]);
}
printf("\n");
}
else
printf("-1\n");
return 0;
}
PROBLEM E/C - On the Bench
题
OvO http://codeforces.com/contest/841/problem/D
cf 841e
cf 841c
解
如果a和b相乘为一个平方数,那么a和b的各质因数的个数对1做与操作的结果必然相等。
所以可以对给定的n数字做个分组,
对于所有数,如果有2个数,它们质因数个数对1做与操作的结果相同,则放到一组(他们相乘结果必然是平方数)
那么题目就是求一个组合,并且要求同组中的数字不相邻。
可以用DP来做。
dp[i][j]代表已经处理了i组数,并且由j个相邻的点他们是同组的,这样的序列的个数。
所以可以用 i 枚举所有组数,对当前第 i 组,用 j 枚举dp数组的第二维,也就是相邻且相同组点对的个数,
然后对于每个j,用 p 枚举将下一个组分成多少个块,对于每个p,用 q 枚举把这 p 个块插入当前序列的多少个相邻且同组的点对中。
然后进行转移
( 思路来源 某大佬)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map> using namespace std; typedef long long ll; const int M=344;
const int mod=1e9+7;
const int N=314; struct Fac
{
int num,cnt; friend bool operator!=(Fac x,Fac y)
{
if(x.num!=y.num || x.cnt!=y.cnt)
return true;
return false;
}
} ; bool cmp(Fac x,Fac y)
{
return x.num<y.num;
} int n;
int scnt[M];
int s[M];
vector<Fac> fac[M];
int item[M],itemlen;
int dp[M][M];
int fact[M],C[M][M]; void deal(int id)
{
Fac facget;
int i,j,tmp,now;
now=s[id];
for(i=2;i*i<=now;i++)
if(now%i==0)
{
facget.num=i;
facget.cnt=0;
while(now%i==0)
{
now/=i;
facget.cnt++;
}
facget.cnt&=1;
if(facget.cnt)
fac[id].push_back(facget);
}
if(now!=1)
{
facget.num=now;
facget.cnt=1;
fac[id].push_back(facget);
}
sort(fac[id].begin(),fac[id].end(),cmp);
} void init()
{
int i,j,the,tmp,sz,k;
for(i=1;i<=n;i++)
fac[i].clear();
for(i=1;i<=n;i++)
deal(i);
memset(scnt,0,sizeof(scnt));
for(i=1;i<=n;i++)
{
the=i;
for(j=1;j<i;j++)
if(scnt[j]!=0 && fac[i].size()==fac[j].size())
{
bool flag=true;
for(k=0;k<fac[i].size();k++)
if(fac[i][k]!=fac[j][k])
{
flag=false;
break;
}
if(flag)
the=j;
}
scnt[the]++;
}
itemlen=0;
for(i=1;i<=n;i++)
if(scnt[i]!=0)
item[++itemlen]=scnt[i];
} void up(int &x,int y)
{
x=(0ll+x+y)%mod;
} void mul(int &x,int y)
{
x=(1ll*x*y)%mod;
} bool cmpless(int x,int y)
{
return x>y;
} void solve()
{
int i,j,p,q,tmp,end;
fact[1]=1;
for(i=2;i<=N;i++)
fact[i]=1ll*fact[i-1]*i%mod;
memset(C,0,sizeof(C));
for(i=0;i<=N;i++)
C[i][0]=C[i][i]=1;
for(i=1;i<=N;i++)
for(j=1;j<i;j++)
up(C[i][j],(0ll+C[i-1][j-1]+C[i-1][j])%mod);
// sort(item+1,item+itemlen+1,cmpless);
memset(dp,0,sizeof(dp));
dp[1][item[1]-1]=1; end=item[1]-1;
for(i=1;i<itemlen;i++)
{
for(j=0;j<=end;j++)
for(p=1;p<=item[i+1];p++)
for(q=max(0,(p+j-(end+i+1)));q<=min(p,j);q++)
{
// cout<<"--\n";
// cout<<i<<' '<<j<<' '<<p<<' '<<q<<endl;
tmp=dp[i][j];
// cout<<tmp<<endl;
mul(tmp,C[item[i+1]-1][p-1]);
// cout<<tmp<<endl;
mul(tmp,C[j][q]);
// cout<<tmp<<endl;
mul(tmp,C[end+i+1-j][p-q]);
// cout<<tmp<<endl;
up(dp[i+1][j+(item[i+1]-p)-q],tmp);
// cout<<i+1<<' '<<j+(item[i+1]-p)-q<<' '<<dp[i+1][j+(item[i+1]-p)-q]<<endl;
}
end+=item[i+1]-1;
}
int ans=dp[itemlen][0];
// cout<<ans<<endl;
for(i=1;i<=itemlen;i++)
mul(ans,fact[item[i]]);
printf("%d\n",ans);
} int main()
{
int i,j;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&s[i]);
init();
solve();
return 0;
} /* 50
873 838 288 87 889 364 720 410 565 651 577 356 740 99 549 592 994 385 777 435 486 118 887 440 749 533 356 790 413 681 267 496 475 317 88 660 374 186 61 437 729 860 880 538 277 301 667 180 60 393 */
PROBLEM _/D - Destiny
题
OvO http://codeforces.com/contest/840/problem/D
cf 840d
解
对这个序列建一个函数式线段树。
设p=(r-l+1)/k,在区间[l,r]查询第p大,第2p大……,中如果一个数在l到r中出现了p次,则必然在上述查询中出现。
对于每次查询,检查区间[l,r]中查询到的数出现次数是否大于k次即可。
(思路来自 OvO)
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; const int MAXN = 300010;
const int M = MAXN * 80;
int n,q,m,tot;
int a[MAXN], t[MAXN];
int T[M], lson[M], rson[M], c[M]; void Init_hsh()
{
for(int i = 1; i <= n;i++)
t[i] = a[i];
sort(t+1,t+1+n);
m = unique(t+1,t+1+n)-t-1;
}
int build(int l,int r)
{
int root = tot++;
c[root] = 0;
if(l != r)
{
int mid = (l+r)>>1;
lson[root] = build(l,mid);
rson[root] = build(mid+1,r);
}
return root;
}
int hsh(int x)
{
return lower_bound(t+1,t+1+m,x) - t;
}
int update(int root,int pos,int val)
{
int newroot = tot++, tmp = newroot;
c[newroot] = c[root] + val;
int l = 1, r = m;
while(l < r)
{
int mid = (l+r)>>1;
if(pos <= mid)
{
lson[newroot] = tot++; rson[newroot] = rson[root];
newroot = lson[newroot]; root = lson[root];
r = mid;
}
else
{
rson[newroot] = tot++; lson[newroot] = lson[root];
newroot = rson[newroot]; root = rson[root];
l = mid+1;
}
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int left_root,int right_root,int k)
{
int l = 1, r = m;
while( l < r)
{
int mid = (l+r)>>1;
if(c[lson[left_root]]-c[lson[right_root]] >= k )
{
r = mid;
left_root = lson[left_root];
right_root = lson[right_root];
}
else
{
l = mid + 1;
k -= c[lson[left_root]] - c[lson[right_root]];
left_root = rson[left_root];
right_root = rson[right_root];
}
}
return l;
}
int calcu(int left_root,int right_root,int k,int numk)
{
int ret=-1;
int l = 1, r = m;
while( l < r)
{
int mid = (l+r)>>1;
if(c[lson[left_root]]-c[lson[right_root]] >= k )
{
r = mid;
left_root = lson[left_root];
right_root = lson[right_root];
}
else
{
l = mid + 1;
k -= c[lson[left_root]] - c[lson[right_root]];
left_root = rson[left_root];
right_root = rson[right_root];
}
}
if((c[left_root]-c[right_root])>numk)
ret=l;
return ret;
} void solve(int q,int n)
{
int kk,i,j,l,r,li,ri,mid,k,leftmost,rightmost,now;
bool flag;
while(q--)
{
scanf("%d%d%d",&l,&r,&k);
kk=k;
k=(r-l+1)/k;
if(k==0)
{
printf("%d\n",t[query(T[l],T[r+1],1)]);
continue;
}
for(i=k;i<=r-l+1;i+=k)
{
now=calcu(T[l],T[r+1],i,k);
if(now!=-1)
{
now=t[now];
break;
}
}
printf("%d\n",now);
}
} int main()
{
int q;
int i,j;
scanf("%d%d",&n,&q);
tot=0;
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
Init_hsh();
T[n+1] = build(1,m);
for(int i = n;i ;i--)
{
int pos = hsh(a[i]);
T[i] = update(T[i+1],pos,1);
}
solve(q,n);
return 0;
}
Codeforces Round #429 (Div. 2/Div. 1) [ A/_. Generous Kefa ] [ B/_. Godsend ] [ C/A. Leha and Function ] [ D/B. Leha and another game about graph ] [ E/C. On the Bench ] [ _/D. Destiny ]的更多相关文章
- CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)
思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...
- CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)
思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...
- CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)
/* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 43 (Rated for Div. 2)
Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...
- Educational Codeforces Round 35 (Rated for Div. 2)
Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...
- Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes
Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...
随机推荐
- Oracle(11g)详细安装步骤
最详细的Oracle安装步骤就在这里,话不多说直接给大家上安装Oracle的详细教程 如果没有安装包,可以先点击下载下载地址:http://download.oracle.com/otn/nt/o ...
- insert 一条数据 然后拿出这条数据在数据库中生成的ID
[insert 一条数据 然后拿出这条数据在数据库中生成的ID] <insert id="insert" parameterType="management&quo ...
- 数据库相关概念讲解(java)
1.常用类或接口介绍 1.DataSource接口 通过javaAPI中javax.sql.DataSource接口注释了解. 1.DataSource功能 如下图: 翻译: DataSource对象 ...
- msql 数据类型
1.数据类型 #1. 数字: 整型:tinyinit int bigint 小数: float :在位数比较短的情况下不精准 double :在位数比较长的情况下不精准 0.0000012301231 ...
- spring cloud微服务实践三
上篇文章里我们实现了spring cloud中的服务提供者和使用者.接下来我们就来看看spring cloud中微服务的其他组件. 注:这一个系列的开发环境版本为 java1.8, spring bo ...
- Python开发【第三章】:函数介绍
一. 函数介绍 1.函数是什么? 在学习函数之前,一直遵循面向过程编程,即根据业务逻辑从上到下实现功能,其往往用一长段代码来实现指定功能,开发过程中最常见的操作就是粘贴复制,也就是将之前实现的代码块复 ...
- css 样式合集
td换行: style="word-wrap:break-word;word-break:break-all;" 超长省略号: table { table-layout: fixe ...
- (转)从0移植uboot (二) _uboot启动流程分析
ref:https://www.cnblogs.com/xiaojiang1025/p/6496704.html 经过了上一篇的配置,我们已经执行make就可以编译出一个uboot.bin,但这还不够 ...
- hdu 1698 线段数的区间更新 以及延迟更新
先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了 贴上两者代码 void updata(int i)//单点更新 { int l=stu[i].l; ...
- 总结一下C++与C#之间的区别
1,C#不支持多重继承 2.在标准的C#安全代码中不支持指针类型的操作,然而,你却能在微软所谓的“非安全代码”中操作指针类型对象. 3.C#中所有对象都只能通过关键词“new”来创建,C++的“类名_ ...