DFS A - Joysticks

嫌麻烦直接DFS暴搜吧,有坑点是当前电量<=1就不能再掉电,直接结束。

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
int ans = 0; void DFS(int a, int b, int step) {
if (a <= 0 || b <= 0) {
ans = std::max (ans, step); return ;
}
if (a < b && b - 2 >= 0) {
DFS (a + 1, b - 2, step + 1);
} else if (a >= b && a - 2 >= 0) {
DFS (a - 2, b + 1, step + 1);
}
} int main() {
int a, b; std::cin >> a >> b;
ans = 0;
DFS (a, b, 0);
std::cout << ans << '\n'; return 0;
}

构造 + 贪心 B - Beautiful Paintings

每次取出不重复的递增序列,直到集合为空

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
std::vector<int> vec; int main() {
int n; std::cin >> n;
for (int x, i=0; i<n; ++i) {
std::cin >> x; vec.push_back (x);
}
std::sort (vec.begin (), vec.end ());
int ans = 0;
while (vec.size () > 0) {
std::vector<int> tmp;
int pre = 0, num = 0;
for (auto x: vec) {
if (x > pre) {
num++; pre = x;
} else {
tmp.push_back (x);
}
}
ans += num - 1; vec = tmp;
}
std::cout << ans << '\n'; return 0;
}

数学 + 容斥 C - Watchmen

化简公式得到找到pair (i, j) xi == xj || yi == yj。找两次再容斥一下。map做更好。

#include <bits/stdc++.h>

typedef long long ll;
const int N = 2e5 + 5;
const int INF = 1e9 + 7;
std::pair<int, int> point[N]; ll calc(int n) {
return 1ll * n * (n - 1) / 2;
} bool cmpx(std::pair<int, int> a, std::pair<int, int> b) {
return a.first < b.first;
} bool cmpy(std::pair<int, int> a, std::pair<int, int> b) {
return a.second < b.second;
}
int main() {
int n; scanf ("%d", &n);
for (int x, y, i=0; i<n; ++i) {
scanf ("%d%d", &x, &y);
point[i] = std::make_pair (x, y);
}
std::sort (point, point+n, cmpx);
ll ans = 0;
int x = INF, y = INF, num = 1;
for (int i=0; i<n; ++i) {
if (x == point[i].first) {
num++;
} else {
x = point[i].first;
if (num > 1) {
ans += calc (num);
}
num = 1;
}
}
if (num > 1) {
ans += calc (num);
}
std::sort (point, point+n, cmpy);
x = INF; y = INF; num = 1;
for (int i=0; i<n; ++i) {
if (y == point[i].second) {
num++;
} else {
y = point[i].second;
if (num > 1) {
ans += calc (num);
}
num = 1;
}
}
if (num > 1) {
ans += calc (num);
}
std::sort (point, point+n);
x = INF; y = INF; num = 1;
for (int i=0; i<n; ++i) {
if (x == point[i].first && y == point[i].second) {
num++;
} else {
x = point[i].first, y = point[i].second;
if (num > 1) {
ans -= calc (num);
}
num = 1;
}
}
if (num > 1) {
ans -= calc (num);
}
printf ("%I64d\n", ans); return 0;
}

two points D - Image Preview

题意:浏览图片,浏览,滑动以及反转需要时间,问在T时间内最多浏览多少图片。

分析:定义两个指针from,to,可行的方案是n+1->from,from->n+1,n+1->to或者n+1->to,to->n+1,n+1->from,还有from->to。前两个重复滑动的可以选择距离小的,第三个只要定义to=n+1就对了。其实可以用二分做的。

#include <bits/stdc++.h>

typedef long long ll;
const int N = 5e5 + 5;
char str[2*N];
ll tim[2*N];
int n, a, b, T; ll get_time(int from, int to) {
ll ret = tim[from] - tim[to-1];
int move = from - to + std::min (from-(n+1), (n+1)-to);
return ret + 1ll * a * move;
} int main() {
scanf ("%d%d%d%d", &n, &a, &b, &T);
scanf ("%s", str + 1);
for (int i=1; i<=n; ++i) {
tim[i] = (str[i] == 'w' ? (b+1) : 1);
}
for (int i=n+1; i<=2*n; ++i) {
tim[i] = tim[i-n];
}
for (int i=1; i<=2*n; ++i) {
tim[i] += tim[i-1];
}
int ans = 0;
int from = n + 1;
for (int to=2; to<=n+1; ++to) {
while (from < to+n-1 && get_time (from+1, to) <= T) from++;
if (get_time (from, to) <= T) ans = std::max (ans, from - to + 1);
}
printf ("%d\n", ans); return 0;
}

图论 + 并查集 E - Table Compression

题意:给一个矩阵,求新的矩阵,使得原矩阵的同行,同列的大小关系不变,且使得新矩阵的最大值最小。即离散化

分析:找好顺序,一个一个从小到大填充就行了。因为有相同数字的存在,使用并查集使得将相同的数字赋值离散化后相同的数字。因为有行和列共同影响一个数字,所以建立一个大数到小数的图,类似树形DP,选择当前行或列的最大值+1为离散化后的数字

#include <bits/stdc++.h>

const int N = 1e6 + 5;
std::pair<int, std::pair<int, int> > A[N];
int val[N], ans[N];
int mr[N], mc[N];
int rt[N];
std::vector<int> edge[N];
std::vector<int> num[N]; int Find(int x) {
return rt[x] == x ? x : rt[x] = Find (rt[x]);
} int main() {
int n, m; scanf ("%d%d", &n, &m);
int tot = n * m;
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
scanf ("%d", &val[i*m+j]);
A[i*m+j].first = val[i*m+j];
A[i*m+j].second = std::make_pair (i, j);
rt[i*m+j] = i * m + j;
}
}
std::fill (mr, mr+n, -1);
std::fill (mc, mc+m, -1);
std::sort (A, A+tot);
for (int i=0; i<tot; ++i) {
int r = A[i].second.first, c = A[i].second.second;
int pos = r * m + c;
if (mr[r] != -1) {
if (val[mr[r]] == A[i].first) {
rt[pos] = Find (mr[r]);
} else {
edge[pos].push_back (mr[r]);
}
}
if (mc[c] != -1) {
if (val[mc[c]] == A[i].first) {
rt[Find (pos)] = Find (mc[c]);
} else {
edge[pos].push_back (mc[c]);
}
}
mr[r] = mc[c] = pos;
}
for (int i=0; i<tot; ++i) {
num[Find (i)].push_back (i);
}
for (int i=0; i<tot; ++i) {
int r = A[i].second.first, c = A[i].second.second;
int id = r * m + c;
if (Find (id) == id) {
int _max = 0;
for (auto u: num[id]) {
for (auto v: edge[u]) {
_max = std::max (_max, ans[v]);
}
}
for (auto u: num[id]) {
ans[u] = _max + 1;
}
}
}
for (int i=0; i<n; ++i) {
for (int j=0; j<m; ++j) {
printf ("%d ", ans[i*m+j]);
}
puts ("");
} return 0;
}

  

Codeforces Round #345 (Div. 2)的更多相关文章

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

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

  2. Codeforces Round #345 (Div. 2)【A.模拟,B,暴力,C,STL,容斥原理】

    A. Joysticks time limit per test:1 second memory limit per test:256 megabytes input:standard input o ...

  3. Codeforces Round #345 (Div. 1) C. Table Compression dp+并查集

    题目链接: http://codeforces.com/problemset/problem/650/C C. Table Compression time limit per test4 secon ...

  4. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...

  5. Codeforces Round #345 (Div. 1) D. Zip-line 上升子序列 离线 离散化 线段树

    D. Zip-line 题目连接: http://www.codeforces.com/contest/650/problem/D Description Vasya has decided to b ...

  6. Codeforces Round #345 (Div. 2) E. Table Compression 并查集

    E. Table Compression 题目连接: http://www.codeforces.com/contest/651/problem/E Description Little Petya ...

  7. Codeforces Round #345 (Div. 2) D. Image Preview 暴力 二分

    D. Image Preview 题目连接: http://www.codeforces.com/contest/651/problem/D Description Vasya's telephone ...

  8. Codeforces Round #345 (Div. 1) A - Watchmen 容斥

    C. Watchmen 题目连接: http://www.codeforces.com/contest/651/problem/C Description Watchmen are in a dang ...

  9. Codeforces Round #345 (Div. 2) B. Beautiful Paintings 暴力

    B. Beautiful Paintings 题目连接: http://www.codeforces.com/contest/651/problem/B Description There are n ...

  10. Codeforces Round #345 (Div. 2) A. Joysticks dp

    A. Joysticks 题目连接: http://www.codeforces.com/contest/651/problem/A Description Friends are going to ...

随机推荐

  1. iOS开发MAC下配置Svn和Git

    如果你对iOS开发中的版本控制还不了解那么你可以先看看这篇(大致看一遍就ok) http://www.cnblogs.com/iCocos/p/4767692.html   关于版本控制使用起来并不难 ...

  2. objective-c可变数组

     1 #pragma mark ---------------可变数组-----------------  2 //        可以在数组里面进行增删改的操作  3 //  4 //        ...

  3. 解决svn迁移过程中出现:SVN Error: is not the same repository as的问题

    一.背景 由于公司业务的需要,新购买了一批机器,那么面临着的就是svn等一系列东西进行迁移的问题,在svn迁移以后,本地的svn代码在切换时出现了SVN Error: 旧服务器地址 is not th ...

  4. October 13th 2016 Week 42nd Thursday

    If the world seems cold to you, kindle fires to warm it. 若世界以寒相待,请点燃火堆以温暖相报. Kindle fires to warm th ...

  5. cmd for备忘

    =============参数 /d (参数只能显示当前目录下的目录名字)=================== rem 显示d:\work\目录下与*.txt相匹配的文件(如果报错就%i-> ...

  6. 项目配置laungchImage

  7. vector< vector<int> >类似于二维数组

    vector< vector<int> > intVV; vector<int> intV; int i,j; ;i<;++i){ intV.clear(); ...

  8. java 学习笔记——网络(Socket)

    阅读方法:将网页放大到200%. 如果你用过用过word应该知道按住ctrl键使用鼠标滚轮缩放.

  9. Tomcat在Linux上的安装与配置

    以下使用的Linux版本为: Redhat Enterprise Linux 6.5 x86_64,Tomcat版本为tomcat-7.0.54. 1.下载JDK与Tomcat.    jdk下载地址 ...

  10. HTML5学习之WebSocket通讯(六)

    WebSocket是下一代客户端-服务器的异步通信方法. WebSocket最伟大之处在于服务器和客户端可以在任意时刻相互推送信息 WebSocket允许跨域通信 Ajax技术需要客户端发起请求,We ...