Problem A:

题目大意:

给定两个数列\(a,b\),一次操作可以交换分别\(a,b\)数列中的任意一对数。求最少的交换次数使得任意一个数都在两个序列中出现相同的次数.

(\(1 \leq a_i,b_i \leq 5 , 1 \leq n \leq 100\))

题解:

直觉告诉我这么搞就行了

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 128;
int a[maxn],b[maxn];
int numa[8],numb[8];
int main(){
int n;read(n);
for(int i=1;i<=n;++i) read(a[i]),numa[a[i]]++;
for(int i=1;i<=n;++i) read(b[i]),numb[b[i]]++;
int ans = 0;
for(int i=1;i<=5;++i){
if((numa[i] + numb[i]) % 2 == 1) return puts("-1");
ans += abs(numa[i] - numb[i])/2;
}printf("%d\n",ans>>1);
getchar();getchar();
return 0;
}

Problem B:

题目大意:

给定两个正整数n,k,求n最少的删除数码的次数使其可以被\(10^k\)整除.

题解:

直接删除后k位就好了,我居然栽在这道题上...

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
int main(){
string s;int k;
cin >> s >> k;
reverse(s.begin(),s.end());
int len = s.size();
int cnt1 = 0,cnt2 = 0;
for(int i=0;i<len;++i){
if(s[i] == '0') ++ cnt1;
else ++ cnt2;
if(cnt1 == k) break;
}
if(cnt1 == k) printf("%d\n",cnt2);
else printf("%d\n",len-1);
getchar();getchar();
return 0;
}

Problem C

题目大意:

有n个物品一定要购买,给出两个数组a,b分别表示这个星期和下个星期时物品的价值,规定这个星期必须买至少k件物品,求最小的代价买到所有物品.

题解:

首先我们在这个星期买掉所有的物品

然后我们到了下一个星期,如果发现有什么物品更便宜了

那么我们可以让时光倒流回上个星期,并且选择不在上个星期购买这个物品

至于k的限制吗。。。去价值差大于零的值中的前n-k大即可.

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
priority_queue<int> q;
const int maxn = 200010;
int a[maxn],b[maxn];
int main(){
int n,k;read(n);read(k);
ll sum=0;
for(int i=1;i<=n;++i) read(a[i]),sum+=a[i];
for(int i=1;i<=n;++i){
read(b[i]);
q.push(a[i]-b[i]);
}
for(int i=k+1;i<=n;++i){
if(q.top()>0) sum-=q.top(),q.pop();
}
printf("%I64d\n",sum);
getchar();getchar();
return 0;
}

Problem D

题目大意:

给定两个字符串S,T,设第一个字符串的长度为n,再给定一个{n}的排列。然后按照给定的排列依次删除S中对应位置上的字符.问第几次删除的时候T不再是S的子序列.

题解:

首先我们可以发现答案具有明显的单调性

所以我们二分答案

然后直接扫一遍两个串判定即可

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 200010;
char s1[maxn],s2[maxn];
int a[maxn],n,m;
bool vis[maxn];
inline bool check(int mid){
memset(vis,0,sizeof vis);
for(int i=1;i<=mid;++i){
vis[a[i]] = true;
}
int p1 = 1,p2 = 1;
while(p2 <= m && p1 <= n){
if(vis[p1]) ++ p1;
else{
if(s2[p2] == s1[p1]) ++p1,++p2;
else ++ p1;
}
}
if(p2 == m+1) return true;
return false;
}
int main(){
scanf("%s%s",s1+1,s2+1);
n = strlen(s1+1);
m = strlen(s2+1);
for(int i=1;i<=n;++i){
read(a[i]);
}
int l = 0,r = n,ans = -1;
while(l <= r){
int mid = (l+r) >> 1;
if(check(mid)) ans = mid,l = mid+1;
else r = mid-1;
}printf("%d\n",ans);
getchar();getchar();
return 0;
}

Problem E

题目大意:

给定一个只有赋值和二元位运算的程序,并且其中存在未知的变量'?',要求为所有的变量'?'确定一个相同的值.使程序结束后所有变量的和达到最值。

分别输出达到最大值和最小值时的'?'的值.

题解:

这是一道考码力的题啊。。。

分别枚举'?'的每个二进制位,下去计算变量之和即可

#include <map>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 5010;
map<string,int> mp;
char vari[maxn][maxn],va[maxn][maxn];
int typ[maxn],tmpx[maxn],tmpy[maxn],ans1[maxn],ans2[maxn];
char tmp[maxn],tt[maxn];
int n,m,val[maxn];
int work(int x,int t){
val[0] = t;
for(int i=1;i<=n;++i){
if(typ[i] == 0) val[i] = va[i][x] - '0';
else if(typ[i] == 1) val[i] = val[tmpx[i]] ^ val[tmpy[i]];
else if(typ[i]==2) val[i] = val[tmpx[i]] | val[tmpy[i]];
else val[i] = val[tmpx[i]] & val[tmpy[i]];
}int sum = 0;
for(int i=1;i<=n;++i) sum += val[i];
return sum;
}
int main(){
read(n);read(m);mp["?"] = 0;
for(int i=1;i<=n;++i){
scanf("%s",vari[i]);
scanf("%s%s",tt,va[i]+1);
mp[vari[i]]=i;
if(va[i][1] == '0'||va[i][1] == '1'){
typ[i] = 0;
continue;
}
tmpx[i] = mp[va[i]+1];
scanf("%s",tmp);
if(tmp[0] == 'X') typ[i] = 1;
else if(tmp[0] == 'O') typ[i] = 2;
else typ[i] = 3;
scanf("%s",va[i]+1);
tmpy[i] = mp[va[i]+1];
}
for(int i=1;i<=m;++i){
int sum1 = work(i,1);
int sum0 = work(i,0);
if(sum1 < sum0) ans1[i] = 1;else ans1[i]=0;
if(sum1 > sum0) ans2[i] = 1;else ans2[i] = 0;
}
for(int i=1;i<=m;++i) printf("%d",ans1[i]);puts("");
for(int i=1;i<=m;++i) printf("%d",ans2[i]);puts("");
getchar();getchar();
return 0;
}

Problem F

题目大意:

给定你一颗若干个串插入得到的Trie树,并且不存在任意一个串是另一个串的前缀.现在允许你删除所有长度>=k字符串的下标为k的字符,然后再把这些串插入到Trie中.确定一个尽量小的k使得后来得到的Trie的节点数目最少.(首先保证节点数目最少)

题解:

好长的题面...

我们发现其实就是让我们删除Trie树中的一层,然后把剩余的部分该合并的合并

使得最终的Trie的size最小,并且多解的情况下输出最小的k

我们其实可以直接计算出每一层如果删去了,那么会合并+删除多少个节点

然后我们遍历每一层取一个最大值即可

至于删去每一层时的计算,我们可以直接递归处理.

(比赛的时候没做出来就是因为只考虑了两层...)

其中有一个很关键的优化,如果遇到了一个点只有一个儿子的情况则直接退出

这样就保证了单次计算复杂度最高是\(logn\)的.

所以总复杂度\(O(nlogn)\)就可以AC了

现在想想自己当时比赛的时候真是傻了

#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 300010;
int ch[maxn][27];
#define id(x) (x - 'a')
int dep[maxn],mxp,anss[maxn];
int get_num(const vector<int> &v){
int ret = v.size() - 1;
int n = v.size();
vector<int> vx;
for(int i='a';i<='z';++i){
vx.clear();
for(int j=0;j<n;++j){
if(ch[v[j]][id(i)]) vx.push_back(ch[v[j]][id(i)]);
}if(vx.size() <= 1) continue;
ret += get_num(vx);
}return ret;
}
void dfs(int u,int dep){
mxp = max(mxp,dep);
vector<int> v;
for(int i = 'a';i<= 'z';++i){
if(ch[u][id(i)]) v.push_back(ch[u][id(i)]);
}anss[dep] += get_num(v) + 1;
for(int i = 'a';i<= 'z';++i){
if(ch[u][id(i)]) dfs(ch[u][id(i)],dep+1);
}return;
}
int main(){
int n;read(n);
char c;
for(int i=1,u,v;i<n;++i){
read(u);read(v);while(c=getchar(),c<'!');
ch[u][id(c)] = v;
}dfs(1,1);
int ans = 0,ans_p = -1;
for(int i=1;i<=mxp;++i){
if(ans < anss[i]){
ans = anss[i];
ans_p = i;
}
}
printf("%d\n%d\n",n-ans,ans_p);
getchar();getchar();
return 0;
}

抽空去做做Div.1的D和E....

Codeforces Round #402 (Div. 2) 题解的更多相关文章

  1. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  2. Codeforces Round #402 (Div. 2)

    Codeforces Round #402 (Div. 2) A. 日常沙比提 #include<iostream> #include<cstdio> #include< ...

  3. Codeforces Round #402 (Div. 2) A+B+C+D

    Codeforces Round #402 (Div. 2) A. Pupils Redistribution 模拟大法好.两个数列分别含有n个数x(1<=x<=5) .现在要求交换一些数 ...

  4. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  5. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  6. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  7. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  8. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  9. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

随机推荐

  1. JS获取图片的缩略图

    js获取上传文件的缩略图 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  2. 【BZOJ2322】[BeiJing2011]梦想封印 高斯消元求线性基+DFS+set

    [BZOJ2322][BeiJing2011]梦想封印 Description 渐渐地,Magic Land上的人们对那座岛屿上的各种现象有了深入的了解. 为了分析一种奇特的称为梦想封印(Fantas ...

  3. nginx学习之静态内容篇(五)

    1.根目录和索引文件 server { root /www/data; location / { } location /images/ { } location ~ \.(mp3|mp4) { ro ...

  4. ORDER BY today_used ASC' % (MAX_USED_TIMES)

    python D:\pymine\clean\spider_map\get_bd_uid_rest_b.py python D:\pymine\clean\spider_map\get_bd_uid_ ...

  5. 监听并保存ssh账号密码

    有时候渗透搞下一台服务器的权限,想通过此服务器抓取其他服务器的ssh密码.就可以用以下方法,如果管理员通过这台服务器作为跳板登录其他服务器,密码就会记录下来. alias ssh='strace -o ...

  6. 还原sqlserver2008 r2 数据库步骤

    1: 有备份文件bak 文件(是在sqlserver2008 r2上备份的) 数据库下载地址(讯雷) ed2k://|file|cn_sql_server_2008_r2_express_with_m ...

  7. PHP网站在Linux服务器上面的安全配置

    本文详细总结了PHP网站在Linux服务器上面的安全配置,包含PHP安全.mysql数据库安全.web服务器安全.木马查杀和防范等,很好很强大很安全. PHP安全配置 1. 确保运行php的用户为一般 ...

  8. Hadoop如何修改HDFS文件存储块大小

    一. 临时修改可以在执行上传文件命令时,显示地指定存储的块大小.1. 查看当前 HDFS文件块大小我这里查看HDFS上的TEST目录下的jdk-7u25-linux-x64.gz  文件存储块大小.1 ...

  9. Python OOP(3) staticmethod和classmethod统计实例

    staticmethod 统计实例 #!python2 #-*- coding:utf-8 -*- class c1: amount_instance=0 def __init__(self): c1 ...

  10. StartUML如何画“不折弯”的直线

    将下图中line style改成 OBlique.快捷键Ctrl +B