今天唯一的成果就是把上次几个人一起开房打的那场cf补一下。

A. Combination Lock

此等水题看一眼样例加上那个配图我就明白题意了,可是手抽没有注释掉freopen,WA了一发。

 #include <bits/stdc++.h>
using namespace std; const int maxn = + ; char s1[maxn], s2[maxn]; int main()
{
int n; cin >> n;
scanf("%s", s1);
scanf("%s", s2);
int ans = ;
for(int i = ; i < n; i++)
{
int a = s1[i] - '';
int b = s2[i] - '';
if(a < b) swap(a, b);
ans += min(a - b, - a + b);
}
cout << ans << "\n"; return ;
}

代码君

B. School Marks

这道题本身不难,因为所给的n是奇数。但有可能会想错细节或漏掉情况。

n个数排好序,中间那个数一定是中位数,而且左右两边各n / 2个数。

所以我们看已知的k个数中比y小的数有多少个,如果大于n/2个,那么中位数一定小于y,无解。

如果k个数中不小于y的大于n / 2个,那么根据贪心,中位数已经在这些数里面了,所以剩下的n-k个数补成1就好了。

其次可以把中位数y插到这里面去,而且能算出中位数左边要补多少个1,右边要补多少个y。

注意在上面所有的情况中还要判断总数是否不超过x。

 #include <bits/stdc++.h>
using namespace std; const int maxn = + ;
int a[maxn]; int main()
{
//freopen("in.txt", "r", stdin); int n, k, p, x, y;
cin >> n >> k >> p >> x >> y;
for(int i = ; i < k; i++) scanf("%d", &a[i]);
sort(a, a + k);
int t = lower_bound(a, a + k, y) - a;
if(t > n / ) { puts("-1"); return ; } int s = ;
for(int i = ; i < k; i++) s += a[i];
if(k - t > n / )
{
int p = n - k;
if(s + p > x) { puts("-1"); return ; }
printf("");
for(int i = ; i < p; i++) printf("");
puts(""); return ;
} int l = n / - t;
int r = n / - k + t;
if(s + l + (r + ) * y > x) { puts("-1"); return ; }
bool print = false;
for(int i = ; i < l; i++) { if(print) printf(" "); print = true; printf(""); }
for(int i = ; i <= r; i++) { if(print) printf(" "); print = true; printf("%d", y); } return ;
}

代码君

C. Ice Cave (BFS)

总体来说这场CF难度偏易,虽然比赛的时候逗比地爆一了。但是下来的时候,手写一遍,没有编译错误交上去还过了,比赛要是这状态,啧啧

这道题一开始没有理解题意,其实就是地图上的'.'走过一遍就变成'X',而且'X'就不能再走了,终点除外。

问从能否起点走到终点,而且把终点变成'X'

把题意理解了,直接BFS就好啦,而且vis标记都不用,直接修改地图就好了。

 #include <bits/stdc++.h>
using namespace std; const int maxn = + ; char s[maxn][maxn];
int n, m; struct Node
{
int x, y;
Node(int x = , int y = ):x(x), y(y) {}
}st, ed; int dx[] = { , , -, };
int dy[] = { , , , - }; inline bool in(int x, int y) { return x > && x <= n && y > && y <= m; } bool BFS()
{
queue<Node> Q;
Q.push(st);
while(!Q.empty())
{
Node now = Q.front(); Q.pop();
for(int i = ; i < ; i++)
{
int x = now.x + dx[i];
int y = now.y + dy[i];
if(!in(x, y)) continue;
if(s[x][y] == 'X') { if(x == ed.x && y == ed.y) return true; continue; }
s[x][y] = 'X';
Q.push(Node(x, y));
}
}
return false;
} int main()
{
//freopen("in.txt", "r", stdin); cin >> n >> m;
for(int i = ; i <= n; i++) scanf("%s", s[i] + );
scanf("%d%d%d%d", &st.x, &st.y, &ed.x, &ed.y);
printf("%s\n", BFS() ? "YES" : "NO"); return ;
}

代码君

D. Bad Luck Island (概率 DP)

题解写得很清楚,递推或者DFS都行。

 #include <cstdio>
#include <iostream>
using namespace std; const int maxn = + ;
double d[maxn][maxn][maxn]; int main()
{
int a, b, c;
cin >> a >> b >> c;
d[a][b][c] = 1.0;
for(int i = a; i >= ; i--)
for(int j = b; j >= ; j--)
for(int k = c; k >= ; k--)
{
if(!i && !j) continue;
if(!i && !k) continue;
if(!j && !k) continue;
double cur = d[i][j][k];
double tot = i*j + i*k + j*k;
if(i) d[i-][j][k] += cur * (double)(i*k) / tot;
if(j) d[i][j-][k] += cur * (double)(i*j) / tot;
if(k) d[i][j][k-] += cur * (double)(j*k) / tot;
} double aa = , bb = , cc = ;
for(int i = ; i <= a; i++) aa += d[i][][];
for(int i = ; i <= b; i++) bb += d[][i][];
for(int i = ; i <= c; i++) cc += d[][][i]; printf("%.12f %.12f %.12f\n", aa, bb, cc); return ;
}

代码君

E. Infinite Inversions (树状数组 离散化)

可以把所求分成两个部分来计:

把这2n个数进行一次离散化,比如排序去重以后变成m个数。

那么经过m次操作,这m个数相较于初始状态是一个这m个数的排列。所以可以用树状数组计算一个这m个数之间的逆序数,注意这里不涉及位置没有变化的那些数。

这样我们便完成了第一部分的计数。

剩下的一部分是这m个数与其他位置未发生变化的逆序数。

预处理一下m个数中第i个数与第1个数之间有多少个数未发生位置改动sum[i]。

比如这m个数经过n次两两交换以后,第i个数的大小为rank[i],那么未发生位置改动的数与第i个数构成的逆序对有abs(sum[i] - sum[rank[i]])

举个栗子:

比如2 7互换,序列由

1 2 3 4 5 6 7 8 变成 1 7 3 4 5 6 2 8

2和7之间有4个数没有变(即3 4 5 6),对于2 7而言,7到2前面去了这是一个逆序对,也就是我们计数的第一部分。

对于2来说,2在7的位置,而且3 4 5 6都与2构成一个逆序对,因此贡献了4个逆序对;

对于7同样,7在2的位置,3 4 5 6在7的后面也贡献了4个逆序对。

所以最终答案为9

 #include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std; typedef long long LL;
const int maxn = + ; int a[maxn], b[maxn], x[maxn], r[maxn];
LL sum[maxn], s[maxn];
int n, m; inline int lowbit(int x) { return x&(-x); } void add(int x, LL d)
{ while(x <= m) { s[x] += d; x += lowbit(x); } } LL query(int x)
{
LL ans = ;
while(x) { ans += s[x]; x -= lowbit(x); }
return ans;
} int main()
{
//freopen("in.txt", "r", stdin); cin >> n;
for(int i = ; i <= n; i++)
{
scanf("%d%d", &a[i], &b[i]);
x[i*-] = a[i]; x[i*] = b[i];
}
sort(x + , x + + n*);
m = unique(x + , x + + n*) - x - ;
for(int i = ; i <= m; i++)
{
sum[i] = sum[i - ] + x[i] - x[i - ] - ;
r[i] = i;
}
for(int i = ; i <= n; i++)
{
int ta = lower_bound(x + , x + + m, a[i]) - x;
int tb = lower_bound(x + , x + + m, b[i]) - x;
swap(r[ta], r[tb]);
} LL inv = ;
for(int i = m; i > ; i--)
{
inv += query(r[i]);
add(r[i], );
inv += abs(sum[i] - sum[r[i]]);
} cout << inv << endl; return ;
}

代码君

CodeForces Round #301 Div.2的更多相关文章

  1. DFS/BFS Codeforces Round #301 (Div. 2) C. Ice Cave

    题目传送门 /* 题意:告诉起点终点,踩一次, '.'变成'X',再踩一次,冰块破碎,问是否能使终点冰破碎 DFS:如题解所说,分三种情况:1. 如果两点重合,只要往外走一步再走回来就行了:2. 若两 ...

  2. 贪心 Codeforces Round #301 (Div. 2) B. School Marks

    题目传送门 /* 贪心:首先要注意,y是中位数的要求:先把其他的都设置为1,那么最多有(n-1)/2个比y小的,cnt记录比y小的个数 num1是输出的1的个数,numy是除此之外的数都为y,此时的n ...

  3. 贪心 Codeforces Round #301 (Div. 2) A. Combination Lock

    题目传送门 /* 贪心水题:累加到目标数字的距离,两头找取最小值 */ #include <cstdio> #include <iostream> #include <a ...

  4. Codeforces Round #301 (Div. 2)(A,【模拟】B,【贪心构造】C,【DFS】)

    A. Combination Lock time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  5. Codeforces Round #301 (Div. 2) D. Bad Luck Island 概率DP

    D. Bad Luck Island Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/pr ...

  6. Codeforces Round #301 (Div. 2) C. Ice Cave BFS

    C. Ice Cave Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/problem/C ...

  7. Codeforces Round #301 (Div. 2) B. School Marks 构造/贪心

    B. School Marks Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/probl ...

  8. Codeforces Round #301 (Div. 2) A. Combination Lock 暴力

    A. Combination Lock Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/540/p ...

  9. 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)

    题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...

  10. 「日常训练」Ice Cave(Codeforces Round 301 Div.2 C)

    题意与分析(CodeForces 540C) 这题坑惨了我....我和一道经典的bfs题混淆了,这题比那题简单. 那题大概是这样的,一个冰塔,第一次踩某块会碎,第二次踩碎的会掉落.然后求可行解. 但是 ...

随机推荐

  1. C#&java重学笔记(面向对象)

    C#部分 1.C#有一个internal关键字,指字段可以同一个程序集中访问,出了程序集不行.还有一个protected internal(没有先后之分)修饰词,指只能在同一个程序集中的子类访问 2. ...

  2. linux使用crontab实现PHP执行定时任务及codeiginter参数传递相关

    http://www.phpddt.com/php/linux-crontab.html crontab: yum install crontabs //安装 说明: /sbin/service cr ...

  3. android开发环境搭建(for 驱动开发人员)

    前言 一.android驱动的开发流程 1: 写LINUX驱动 2: 写LINUX应用测试程序 3: 写JNI接口,用来包装第二步写的应用 (要用NDK来编译) 生成一个.SO文件,相当于CE下的DL ...

  4. 深入浅出ES6(九):学习Babel和Broccoli,马上就用ES6

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 现在,我们将向你分步展示如何做到的这一切.上面提及的工具被称为转译器,你可以将它 ...

  5. App接口设计思路

    http://www.techweb.com.cn/network/system/2016-01-11/2256859.shtml http://www.woshipm.com/pmd/172952. ...

  6. Visual Studio 项目中添加include, lib, dll库文件(*.h,*.lib,*.dll)

    应用程序使用外部库时需要进行加载,两种库的加载本质上都是一样:提供功能和功能的定义.vs2005 c++ 项目设置外部库方法如下: 1. 添加编译所需要(依赖)的 lib 文件     在“项目-&g ...

  7. C#保留小数位数

    1.System.Globalization.NumberFormatInfo provider = new System.Globalization.NumberFormatInfo(); prov ...

  8. 【转】SQL Server T-SQL写文本文件

    原文:http://www.nigelrivett.net/SQLTsql/WriteTextFile.html The are several methods of creating text fi ...

  9. mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换等

    mp3 音频 音乐 tag ID3 ID3V1 ID3V2 标签 读取信息 获得图片 jpeg bmp 图片转换(上) MP3文件格式(二)---ID3v2 图:ID3V1标签结构 图:ID3V2标签 ...

  10. java开发--反射技术

    学习目标: 1.什么是反射:即反射的定义, 2.反射有什么作用,能解决什么问题, 3.反射的知识点是什么, 4.反射的利弊 5.反射的例子 1.什么是反射:反射的定义: a) 能够分析类能力的程序被称 ...