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. (十四)JDBC入门

    目录 什么是JDBC 操作JDBC的步骤 DriverManager对象 数据库URL Connection对象 Statement对象 ResultSet对象 常用数据类型转换表 释放资源 SQL注 ...

  2. echarts配置项说明//持续添加

    <template> <div>      <!-- <h2>本月抄表完成率</h2> --> <!-- <div id=&qu ...

  3. MarkdownPad2安装与破解-转载

    MarkdownPad安装包下载链接链接:https://pan.baidu.com/s/1o7c4W7C2d8zCPh5z7y4IvQ提取码:e4bf 下载解压之后,找要MarkdownPad2.e ...

  4. Warning: popen() has been disabled for security reasons in OS/Guess.php on line 241

    今天使用pecl install swoole命令编译安装swoole的时候提示:Warning: popen() has been disabled for security reasons in ...

  5. linux之find的使用

    基本语法 find [查找目录] [选项] [查找规则] [查找完后的操作] 即:find pathname -option -condition [-print -exec -ok …] 选项参数 ...

  6. 验证码识别的免费 OCR

    在做接口自动化以及爬虫的过程中,验证码一般是个很烦的存在,其实大厂们已经做好了一些 OCR 供使用,这里介绍一下百度 OCR 的使用方法. 注册并生成应用 1.注册一个百度智能云账号:http://a ...

  7. C#基础--AbStract与Interface

         Interface: 接口方法不能用public abstract等修饰.接口内不能有字段变量,构造函数. 接口内可以定义属性,如string color{get;set;}这种. 实现接口 ...

  8. cmd查找端口占用情况

    查找端口占用情况:netstat -ano|findstr 4848 查看使用指定端口的应用程序:tasklist|findstr xxxx,xxxx指的是pid 结束指定进程:taskkill /p ...

  9. 搭建nginx静态资源站

    搭建静态资源站包括以下几部分: root指令与alias指令的区别 使用gzip压缩资源 如何访问指定目录下的全部资源文件 如何限制访问流量 如何自定义log日志 root指令与alias指令的区别 ...

  10. ElasticSearch做实时OLAP框架~实时搜索、统计和OLAP需求,甚至可以作为NOSQL来使用(转)

    使用ElasticSearch作为大数据平台的实时OLAP框架 – lxw的大数据田地 http://lxw1234.com/archives/2015/12/588.htm 一直想找一个用于大数据平 ...