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调用函数的方式
--方法1.PLSQL代码块 SQL> set serveroutput onSQL> declare 2 v_sal emp_pl.sal%type; 3 begin 4 v_sal : ...
- C++:盾神与条状项链
实现代码如下: #include<cstdlib> #include <string> #include <iostream> using namespace st ...
- 剑指offer53:表示数值的字符串,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是
1 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3 ...
- 安全篇-AES/RSA加密机制
在服务器与终端设备进行HTTP通讯时,常常会被网络抓包.反编译(Android APK反编译工具)等技术得到HTTP通讯接口地址和参数.为了确保信息的安全,我们采用AES+RSA组合的方式进行接口参数 ...
- 数据库设计_ERMaster安装使用_PowerDesigner数据设计工具
数据库设计 1. 说在前面 项目开发的流程包括哪些环节 需求调研[需求调研报告]-- 公司决策层 (1) 根据市场公司需求分析公司是否需要开发软件来辅助日常工作 (2) 公司高层市场考察,市场分析,决 ...
- 剑指Offer(4)——替换空格
题目: 请实现一个函数,把字符串中的每个空格替换成"%20".例如输入“We are happy.”,则输出“We%20are%20happy.”. 思路: 如果按照顺序从前往后依 ...
- python中集合set,字典dict和列表list的区别以及用法
python中set代表集合,list代表列表,dict代表字典 set和dict的区别在于,dict是存储key-value,每一个key都是唯一的,set相对于dict存储的是key,且key是唯 ...
- ES6--JavaScript扩展知识点(let、const、解构)
一,ES2015(ES6)新增了两个声明变量的关键字:let.const let:只在代码块内{}有效,不可重复声明,不会提前初始化 1.只在代码块内有效 { let a = 1; var b = 2 ...
- js如何获取数值
获取jsfunction返回的值1,首先,用初始化代码创建一个新的HTML5文件,如下所示.2,创建一个新的H1标记来接收JS中函数休闲鹿的返回值.3,创建一个新的脚本标记并在标记中创建一个新函数.函 ...
- 前端编译原理 简述-jison
最近几年的项目技术难点都和编译原理,抽象语法树,代码编辑器 有关系.现在时间有点空,先从基础了解起来,让有些交互和提示能够更智能些. 编译原理-Parser 编译原理 其实就是 让计算机懂的 “ ...