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. 封装ADO库之MFC应用

    Microsoft Activex Data Objects(ADO)支持用于建立基于客户端/服务器和web的应用程序开发的主要功能.其主要优点是易于使用.高速度.低内存支出和占用磁盘空间较少. 本次 ...

  2. Qt程序开机自动运行

    一.写入注册表需要管理员权限 1.开发中生成并运行程序需要写入注册表时,应该以管理员权限打开项目: 2.点击程序运行需要写入注册表,则应该以管理员权限打开此程序. 二.实现 void MoreSetW ...

  3. python面向对象基础-01

    面向对象(OOP)基本概念 前言 话说三国时期曹军于官渡大败袁绍,酒席之间,曹操诗兴大发,吟道:喝酒唱歌,人生真爽! 众将直呼:"丞相好诗",于是命印刷工匠刻板印刷以流传天下; 待 ...

  4. go 构造切片slice

    定义切片 make([]int, 5)  长度和容量均为5 make([]int, 0, 5) 长度为0 容量为0 切片 slice2[3:5] 对slice2进行切片返回 第3 4 两个元素 不包含 ...

  5. (转)从0移植uboot (一) _配置分析

    ref : https://www.cnblogs.com/xiaojiang1025/p/6106431.html 本人建议的uboot学习路线,先分析原有配置,根据现有的配置修改.增加有关的部分, ...

  6. Sublime Text 开发神器相关 插件安装 功能介绍

    无法安装更多见http://blog.csdn.net/freshlover/article/details/44261229/ Sublime Text 3 安装插件管理 Package Contr ...

  7. opencv中自适应阈值函数的实现(c++)

    根据<面向飞机蒙皮接缝的线结构光检测技术研究_张卡>论文中的原理,编写了自适应阈值函数 原理: //计算灰度最大最小值 void MaxGrayValue(Mat image,int &a ...

  8. Python练习_装饰器、生成器_day12

    装饰器 装饰器篇: 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码. def login(func): def inner( ...

  9. DRF 01

    目录 DRF 接口 概念 YApi接口文档 Postman接口测试 RESTful接口规范 URL设计 响应结果 响应状态码 数据状态码 数据状态信息 数据本身 五大请求方式 简单实现 DRF drf ...

  10. 基于【 Docker】二 || Docker的入门安装

    一.前提条件 本文介绍 Docker CE 的安装使用. 目前,CentOS 仅发行版本中的内核支持 Docker. Docker 运行在 CentOS 7 上,要求系统为64位.系统内核版本为 3. ...