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. supersr--addSubview和 insertSubView 区别

    A addSubview B  是将B直接覆盖在A的最上层  例子: [self.view addSubview:scrollView]; A insertSubView B AtIndex:2 是将 ...

  2. [Android Pro] android 4.4 Android原生权限管理:AppOps

    reference : http://m.blog.csdn.net/blog/langzxz/45308199 reference : http://blog.csdn.net/hyhyl1990/ ...

  3. NYOJ题目872开会

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsoAAAKwCAIAAAAOTc6wAAAgAElEQVR4nO3dO3LcSpOG4dkEfS6Edi

  4. cascade 介绍与用法 ( oracle)

    级联删除,比如你删除某个表的时候后面加这个关键字,会在删除这个表的同时删除和该表有关系的其他对象 1.级联删除表中的信息,当表A中的字段引用了表B中的字段时,一旦删除B中该字段的信息,表A的信息也自动 ...

  5. 与你相遇好幸运,Sail.js新建模型控制器

    sails generate api user  创建了user的controller和models sails generate api user index 创建了user的controller和 ...

  6. Vs 控件错位 右侧资源管理器文件夹点击也不管用,显示异常

    问题:显卡驱动异常. 缘由:驱动精灵万能显卡安装系统 解决方案:根据笔记本型号去官网下载适配显卡驱动.

  7. WindowManagerPolicy的后缀 解释

    转自:http://blog.csdn.net/hunanwy/article/details/8563090 Ti,called from the input thread. Input threa ...

  8. 傻瓜式十分钟免费开启 HTTPS,是时候为你的站点加上小绿锁了

    http://gold.xitu.io/entry/57df65690bd1d00057f9455b?from=singlemessage&isappinstalled=0 原文链接:http ...

  9. Android - 控件android:ems属性

    Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...

  10. Delphi中的函数指针判断是否为空

    delphi函数指针 只有@@p才代表了函数指针本身的地址   assigned(p) 判断是否为空 或者用 @p=nil 来判断函数指针是不是为空 Delphi中的函数指针实际上就是指针,只是在使用 ...