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 ]的更多相关文章

  1. CodeForces 840C - On the Bench | Codeforces Round #429 (Div. 1)

    思路来自FXXL中的某个链接 /* CodeForces 840C - On the Bench [ DP ] | Codeforces Round #429 (Div. 1) 题意: 给出一个数组, ...

  2. CodeForces 840B - Leha and another game about graph | Codeforces Round #429(Div 1)

    思路来自这里,重点大概是想到建树和无解情况,然后就变成树形DP了- - /* CodeForces 840B - Leha and another game about graph [ 增量构造,树上 ...

  3. CodeForces 840A - Leha and Function | Codeforces Round #429 (Div. 1)

    /* CodeForces 840A - Leha and Function [ 贪心 ] | Codeforces Round #429 (Div. 1) A越大,B越小,越好 */ #includ ...

  4. 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 ...

  5. 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 ...

  6. Educational Codeforces Round 43 (Rated for Div. 2)

    Educational Codeforces Round 43 (Rated for Div. 2) https://codeforces.com/contest/976 A #include< ...

  7. Educational Codeforces Round 35 (Rated for Div. 2)

    Educational Codeforces Round 35 (Rated for Div. 2) https://codeforces.com/contest/911 A 模拟 #include& ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Java基础---Java变量

    变量:程序运行期间,内容可以发生改变的量. 创建一个变量并且使用的格式: 数据类型 变量名称;         // 创建了一个变量 变量名称 = 数据值;           // 赋值,将右边的数 ...

  2. Linux "yin"才们的奇"yin"小技巧 --请用东北发音夸他们

    1. include/linux/bits.h GENMASK(h, l) /* * Create a contiguous bitmask starting at bit position @l a ...

  3. 剑指offer52:正则表达式匹配

    1 题目描述 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符‘.’表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个 ...

  4. Error: errCode: -404011 cloud function execution error | errMsg: clou……错误

    在开通了云开发之后,无论点击小程序获取openid按钮报,Error: errCode: -404011 cloud function execution error | errMsg: clou…… ...

  5. Nginx关联php安装及启动

    Nginx 1.10.2 php 5.6.30 [root@nginx local]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (C ...

  6. ftp-server(对象存储)

    1.背景 在腾讯云弄了一个对象存储,想通过ftp上传照片 说明连接: 腾讯云:https://cloud.tencent.com/document/product/436/7214 GitHub:ht ...

  7. hdu 1698 线段数的区间更新 以及延迟更新

    先说说区间更新和单点更新的区别 主要的区别是搜索的过程 前者需要确定一个区间 后者就是一个点就好了 贴上两者代码 void updata(int i)//单点更新 { int l=stu[i].l; ...

  8. linux 串口接收

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types. ...

  9. 【转载】salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句

    salesforce 零基础开发入门学习(二)变量基础知识,集合,表达式,流程控制语句 salesforce如果简单的说可以大概分成两个部分:Apex,VisualForce Page. 其中Apex ...

  10. TSec《mysql client attack chain》

    从这个议题学到挺多,攻击手法的串联. 1.mysql Client Attack 这个攻击手法去年就爆出来了,本质就是mysql协议问题,在5步篡改读取客户端内容,导致任意文件读取,如下图所示. 修改 ...