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. jQuery.callbacks 注释

    (function( jQuery ) { // String to Object flags format cache var flagsCache = {}; // Convert String- ...

  2. 九度OJ 1336:液晶屏裁剪 (GCD)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:983 解决:228 题目描述: 苏州某液晶厂一直生产a * b大小规格的液晶屏幕,由于该厂的加工工艺限制,液晶屏的边长都为整数.最近由于市场 ...

  3. TFS 中工作项的定制-修改工作流

    我们都会用到TFS中的工作项.一般来说,最主要的会用到任务.bug这些工作流来进行项目管理里.但我们发现,实际上,有些模板中的工作流并不能完全符合我们的需要,因此我们会进行工作流的定制操作.下面就会通 ...

  4. Java语言实现简单FTP软件------>源码放送(十三)

    Java语言实现简单FTP软件------>FTP协议分析(一) Java语言实现简单FTP软件------>FTP软件效果图预览之下载功能(二) Java语言实现简单FTP软件----- ...

  5. Nodejs课堂笔记-第四课 Dynamodb为何物

    本文由Vikings(http://www.cnblogs.com/vikings-blog/) 原创,转载请标明.谢谢! 我喜欢带着目标来学习新知识.因此学习nodejs过程中,不喜欢只看枯燥的语法 ...

  6. python基础8 -----迭代器和生成器

    迭代器和生成器 一.迭代器 1.迭代器协议指的是对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退) 2. ...

  7. php 图片下载

    php图片保存.下载 <?php //获取图片2进制内容 ,可以保存入数据库 $imgStr = file_get_contents('http://.../1.jpg'); //保存图片 $f ...

  8. iOS UITableViewCell UITableVIewController 纯代码开发

    iOS UITableViewCell UITableVIewController 纯代码开发 <原创> .纯代码 自定义UITableViewCell 直接上代码 ////// #imp ...

  9. pygame躲敌人的游戏

    #first.py# coding=utf- import pygame from pygame.locals import * from sys import exit from util impo ...

  10. Please enable network time synchronisation in system settings

    eth区块同步出现这样的WARN: WARN [06-17|13:02:42] System clock seems off by -51.509894715s, which can prevent ...