刚好在考完当天有一场div3,就开了个小号打了,打的途中被辅导员喊去帮忙,搞了二十分钟-_-||,最后就出了四题,题解如下:
题目链接:http://codeforces.com/contest/1003

题目:

思路:求众数出现的次数

代码如下:

 #include <bits/stdc++.h>
using namespace std; typedef long long ll;
#define debug(x) cout <<"[" <<x <<"]" <<endl const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + ; int n;
int a[], vis[]; int main() {
cin >>n;
int mx = -;
for(int i = ; i < n; i++) {
cin >>a[i];
vis[a[i]]++;
if(mx < vis[a[i]]) {
mx = vis[a[i]];
}
}
cout <<mx <<endl;
return ;
}

题目:

思路:构造一个01串,其中0的个数为a,1的个数为b,中间刚好有k次01的交替,题目保证有解。模拟即可。

代码实现如下:

 #include <bits/stdc++.h>
using namespace std; int a, b, x, flag;
int vis[]; int main() {
cin >> a >> b >> x;
int flag = x & ;
if(a >= b) {
vis[] = ;
a--;
for(int i = ; i < x; i++) {
vis[i] = !vis[i-];
if(i & ) b--;
else a--;
}
for(int i = ; i < x; i++) {
cout <<vis[i];
}
if(flag) {
for(int i = ; i < a; i++) {
cout <<"";
}
for(int i = ; i < b; i++) {
cout <<"";
}
} else {
for(int i = ; i < b; i++) {
cout <<"";
}
for(int i = ; i < a; i++) {
cout <<"";
}
}
cout <<endl;
} else {
vis[] = ;
b--;
for(int i = ; i < x; i++) {
vis[i] = !vis[i-];
if(i & ) a--;
else b--;
}
for(int i = ; i < x; i++) {
cout <<vis[i];
}
if(flag) {
for(int i = ; i < b; i++) {
cout <<"";
}
for(int i = ; i < a; i++) {
cout <<"";
}
} else {
for(int i = ; i < a; i++) {
cout <<"";
}
for(int i = ; i < b; i++) {
cout <<"";
}
}
cout <<endl;
}
return ;
}

题目:

思路:求长度大于等于k的连续字串的平均值的最大值,因为本题数据小,可以用n^2水过,数据大了需要用二分,这里就只贴比赛时写的n^2的代码了~先用一个前缀和来维护分子,分母用一个循环处理。

代码如下:

 #include <bits/stdc++.h>
using namespace std; const int maxn = * ;
const double eps = 1e-;
int n, k;
double ans;
int a[maxn], sum[maxn]; int main() {
scanf("%d%d", &n, &k);
sum[] = ;
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
sum[i] = sum[i-] + a[i];
}
ans = -;
for(int i = ; i <= n; i++) {
for(int j = k; j <= n; j++) {
double t = 1.0 * (sum[i + j - ] - sum[i-]) / j;
if(t - ans >= eps) {
ans = t;
}
}
}
printf("%.7f\n", ans);
return ;
}

题目:

题意:给你n个数,ai保证是2的幂次,q次查询,每次查询给你一个数x,问使用最少的a来组成这个数,如果无法组成就输出-1.

思路:先将ai转换成2^t,用个数组来储存;将x转换成二进制,然后从最大为开始贪心,当题目给的某一次方(t次方)不够时,就用2个t-1次方来构成一个t次方,最后判断0次方是否足够即可。用样例来解释:题目给的2的1次方有2个,2次方有2个,3次方有1个。第一个x的二进制为1000,需要一个2的3次方,题目给的刚好有一个3次方,因此答案为1;第二个x的二进制为101,需要2的2次方1个,0次方1个,2次方足够,但是没有0次方,所以答案为-1;第三个x的二进制为1110,需要1个3次方,1个2次方,1个1次方,均足够,故为3。

代码实现如下:

 #include <bits/stdc++.h>
using namespace std; const int maxn = 2e5 + ;
int n, q, t;
int a[maxn], b[maxn], num[], p[];
long long vis[maxn]; void prepare() {
vis[] = ;
for(int i = ; i < ; i++) {
vis[i] = vis[i-] * ;
}
} int main() {
prepare();
cin >>n >>q;
for(int i = ; i < n; i++) {
cin >>a[i];
t = lower_bound(vis, vis + , a[i]) - vis;
num[t]++;
}
for(int i = ; i < q; i++) {
cin >>b[i];
int k = , ans = ;
while(b[i]) {
p[k++] = b[i] % ;
b[i] = b[i] / ;
}
for(int j = k - ; j >= ; j--) {
if(num[j] < p[j]) {
ans += num[j];
p[j-] += * (p[j] - num[j]);
} else {
ans += p[j];
}
}
if(num[] < p[]) cout <<"-1" <<endl;
else cout <<ans + p[] <<endl;
}
return ;
}

赛后补题,一定要假装出是自己比赛时A的~

题目:

题意:生成一棵树,树的直径(本题的直径是指两节点间距离的最大值)为d,度数不超过k。

思路:首先生成一条链,此处节点分别为1~d+1,然后以这条链上的节点为根生成树,用dfs处理,此处生成的树的深度为min(i-1,d-i+1)(因为两节点间的距离不能超过d嘛~),注意处理NO的情况。

代码实现如下:

 #include <bits/stdc++.h>
using namespace std; const int maxn = 4e5 + ;
int n, k, d, cnt;
vector<int> G[maxn]; void dfs(int u, int deep, int mx) {
if(deep == mx) return;
for(int i = ; i < k - - (deep == ) && cnt < n; i++) {
int v = ++cnt;
G[u].push_back(v);
dfs(v, deep + , mx);
}
} int main() {
cin >>n >>d >>k;
if(k==){
if(n!= || d!=)printf("NO\n");
else printf("YES\n1 2\n");
return ;
}
if(d >= n) {
cout <<"NO" <<endl;
return ;
}
for(int i = ; i <= d; i++) {
G[i].push_back(i+);
}
cnt = d + ;
for(int i = ; i <= d + ; i++) {
dfs(i, , min(i - , d - i + ));
}
if(cnt < n) {
cout <<"NO" <<endl;
return ;
}
cout <<"YES" <<endl;
for(int i = ; i <= n; i++) {
for(int j = ; j < G[i].size(); j++) {
cout <<i <<" " <<G[i][j] <<endl;
}
}
return ;
}

Codeforces Round #494 (Div. 3)的更多相关文章

  1. Codeforces Round #494 (Div 3) (A~E)

    目录 Codeforces 1003 A.Polycarp's Pockets B.Binary String Constructing C.Intense Heat D.Coins and Quer ...

  2. Codeforces Round #494 (Div. 3) D. Coins and Queries(贪心

    题目链接 题目大意:给你n个物品,第iii个物品价值aia_iai​,询问q次,问你能不能凑出价值为qiq_iqi​的物品. 小贪心吧.从大到小找,能拿就拿就行了. #include<bits/ ...

  3. Codeforces Round #494 (Div. 3) D. Coins and Queries (贪心,数学)

    题意:给你一组全是\(2^d\ (d\ge0)\)的数,询问q次,每次询问一个数,问这个数是否能够由原数组中的数相加得到,如果能,输出最少用多少个数,否则输出\(-1\). 题解:首先贪心得出结论:如 ...

  4. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  5. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  6. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  7. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  8. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  9. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

随机推荐

  1. 自定义类属性设置及setter、getter方法的内部实现

    属性是可以说是面向对象语言中封装的一个体现,在自定义类中设置属性就相当于定义了一个私有变量.设置器(setter方法)以及访问器(getter方法),其中无论是变量的定义,方法的声明和实现都是系统自动 ...

  2. thrift多平台安装

    thrift支持多语言的RPC,一直都想深入学习了解thrift,最近有空,就上网查了些资料,学习了一下,对它的使用有了一些了解.本篇是写thrift的安装,使用方法会另起一篇来写. 本文使用thri ...

  3. php缓存技术——memcache常用函数详解

    php缓存技术——memcache常用函数详解 2016-04-07 aileen PHP编程 Memcache函数库是在PECL(PHP Extension Community Library)中, ...

  4. TCP/IP三次握手与四次握手

    原文地址 http://blog.csdn.net/whuslei/article/details/6667471 http://blog.csdn.net/wo2niliye/article/det ...

  5. [计算机网络] TCP的拥塞控制

    引言 计算机网络中的带宽.交换结点中的缓存和处理机等,都是网络的资源.在某段时间,若对网络中某一资源的需求超过了该资源所能提供的可用部分,网络的性能就会变坏.这种情况就叫做拥塞. 拥塞控制就是防止过多 ...

  6. linux 装redmine

    看第一篇 https://www.cnblogs.com/iluzhiyong/p/redmine.html 看第二篇 http://blog.51yip.com/cloud/1874.html 基本 ...

  7. 从一个ListBox中的元素点击导入另一个ListBox元素中

    先看效果图:

  8. 【.Net】C# 反编译工具之dnSpy

    下载地址:https://github.com/0xd4d/dnSpy/releases无需安装,和 ILSPY同门,感觉比ILSPY还强大 直接把dll拖拽到程序集资源管理器里面就可以啦

  9. MATLAB中imfilter函数

    功能:对任意类型数组或多维图像进行滤波. 用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,...) 或写作g = imfilter(f, w ...

  10. JavaScript词法分析解析

    函数在调用之前,会进行词法分析或者叫语法分析: 1. 函数在形成调用的那一瞬间,会有一个活动对象叫 active object ,简称AO,会分析如下几条: 形式参数 函数内局部变量声明 函数声明表达 ...