前三题水

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5; int main() {
int n, a, b; std::cin >> n >> a >> b;
int bb = b;
if (b < 0) bb = -b;
while (bb--) {
if (b < 0) {
a--;
} else {
a++;
}
if (a == 0) {
a = n;
} else if (a == n + 1) {
a = 1;
}
}
std::cout << a << '\n'; return 0;
}

#include <bits/stdc++.h>

typedef long long ll;
const int M = 1e4 + 5;
struct Info {
std::string name;
int score;
bool operator < (const Info &rhs) const {
return score > rhs.score;
}
};
std::vector<Info> vec[M]; bool same_three(int a, int b, int c) {
return b == c;
} bool same(int id) {
return same_three (vec[id][0].score, vec[id][1].score, vec[id][2].score);
} int main() {
int n, m; std::cin >> n >> m;
std::string str; int num, score;
for (int i=0; i<n; ++i) {
std::cin >> str >> num >> score;
vec[num].push_back ((Info) {str, score});
}
for (int i=1; i<=m; ++i) {
if (vec[i].size () < 2) {
puts ("?");
continue;
} else {
std::sort (vec[i].begin (), vec[i].end ());
if (vec[i].size () > 2 && same (i)) {
puts ("?");
} else {
std::cout << vec[i][0].name << " " << vec[i][1].name << '\n';
}
}
} return 0;
}

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e5 + 5;
std::map<int, bool> vis; int main() {
int n, m; scanf ("%d%d", &n, &m);
for (int a, i=0; i<n; ++i) {
scanf ("%d", &a);
vis[a] = true;
}
std::vector<int> vec;
int now = 1;
while (m - now >= 0) {
if (vis[now]) {
now++;
} else {
m -= now;
vec.push_back (now);
now++;
}
}
printf ("%d\n", vec.size ());
for (int i=0; i<vec.size (); ++i) {
printf ("%d%c", vec[i], i == vec.size () - 1 ? '\n' : ' ');
} return 0;
}

几何(叉积) D - Bicycle Race

题意:一个人从最下面的位置逆时针沿着湖转一圈,当转角指向湖的方向认为是危险的,问有多少个危险的转角.

分析:分类讨论也就四种情况,但是注意出发点不一定是最左边的(最下面的);也可以用叉积来判断,大于0表示是顺时针满足危险的定义.

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e4 + 5;
struct Point {
int x, y;
};
Point point[N]; int main() {
int n; scanf ("%d", &n);
for (int i=0; i<=n; ++i) {
scanf ("%d%d", &point[i].x, &point[i].y);
}
int ans = 0;
for (int i=0; i<n-2; ++i) {
if (point[i].x == point[i+1].x) {
if (point[i+1].y > point[i].y) {
if (point[i+2].x < point[i+1].x) {
ans++;
}
} else {
if (point[i+2].x > point[i+1].x) {
ans++;
}
}
} else {
if (point[i].x < point[i+1].x) {
if (point[i+1].y < point[i+2].y) {
ans++;
}
} else {
if (point[i+1].y > point[i+2].y) {
ans++;
}
}
}
}
printf ("%d\n", ans); return 0;
}
#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e4 + 5;
struct Point {
int x, y;
Point operator - (const Point &rhs) const {
return (Point) {x - rhs.x, y - rhs.y};
}
};
Point point[N];
typedef Point Vector; int cross(Vector A, Vector B) {
return A.x * B.y - A.y * B.x;
} int main() {
int n; scanf ("%d", &n);
for (int i=0; i<=n; ++i) {
scanf ("%d%d", &point[i].x, &point[i].y);
}
int ans = 0;
for (int i=1; i<n; ++i) {
if (cross (point[i-1] - point[i], point[i+1] - point[i]) < 0) {
ans++;
}
}
printf ("%d\n", ans); return 0;
}

图论 E - New Reform

题意:n个点,m条边,每条边是单向的,问如何安排每条边的方向使得入度为0的点最少,最少是几个.

分析:进行DFS,如果访问到已访问过的点表示该连通块有环,也就是可以满足每个点的入度大于等于1.

#include <bits/stdc++.h>

const int N = 1e5 + 5;

std::vector<int> G[N];
bool vis[N];
int n, m, add; void DFS(int u, int fa) {
if (vis[u]) {
add = 0;
return ;
}
vis[u] = true;
for (int i=0; i<G[u].size (); ++i) {
int v = G[u][i];
if (v == fa) {
continue;
}
DFS (v, u);
}
} int main() {
scanf ("%d%d", &n, &m);
for (int i=0; i<m; ++i) {
int u, v; scanf ("%d%d", &u, &v);
G[u].push_back (v);
G[v].push_back (u);
}
int ans = 0;
for (int i=1; i<=n; ++i) {
if (vis[i]) {
continue;
}
add = 1;
DFS (i, 0);
ans += add;
}
printf ("%d\n", ans); return 0;
}

并查集 F - Polycarp and Hay

题意:n*m的矩阵有不同高度的干草,现要割成高度相同的连通块,且其中至少一个干草的高度没有变化,以及连通块的高度和等于k,求可行的方案.

分析:统计出每个可以高度不变化且被k整除,且连通块数量不小于k/a[i][j],那么BFS一次能的得到方案.按照高度从大到小排序,对于当前的干草累加上周围高度不小于它的连通块数量,DP思想或者说是求前缀,用并查集维护.

#include <bits/stdc++.h>

typedef long long ll;
const int N = 1e3 + 5;
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
ll a[N][N];
bool vis[N][N];
int n, m;
ll k, num, aim; int rt[N*N], rk[N*N];
int Find(int x) {
return rt[x] == -1 ? x : rt[x] = Find (rt[x]);
}
void Union(int x, int y) {
x = Find (x);
y = Find (y);
if (x == y) {
return ;
}
rt[x] = y;
rk[y] += rk[x];
} struct Info {
ll val;
int x, y;
bool operator < (const Info &rhs) const {
return val > rhs.val;
}
};
std::vector<Info> vec; bool judge(int x, int y) {
if (x < 1 || x > n || y < 1 || y > m) {
return false;;
}
return true;
} void BFS(int x, int y) {
std::queue<std::pair<int, int> > que;
que.push (std::make_pair (x, y));
vis[x][y] = true; num--; while (!que.empty ()) {
int nx = que.front ().first, ny = que.front ().second;
que.pop ();
for (int i=0; i<4; ++i) {
int tx = nx + dx[i];
int ty = ny + dy[i];
if (num == 0) {
continue;
}
if (!judge (tx, ty)) {
continue;
}
if (vis[tx][ty]) {
continue;
}
if (a[tx][ty] < aim) {
continue;
}
vis[tx][ty] = true; num--;
que.push (std::make_pair (tx, ty));
}
}
} void print() {
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
if (vis[i][j]) {
printf ("%I64d ", aim);
} else {
printf ("0 ");
}
}
puts ("");
}
} int main() {
scanf ("%d%d%I64d", &n, &m, &k);
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
scanf ("%I64d", &a[i][j]);
vec.push_back ((Info) {a[i][j], i, j});
}
}
std::sort (vec.begin (), vec.end ());
memset (rt, -1, sizeof (rt));
std::fill (rk, rk+N*N, 1); for (int i=0; i<vec.size (); ++i) {
Info &r = vec[i];
if (r.val == 0) {
break;
}
for (int j=0; j<4; ++j) {
int tx = r.x + dx[j];
int ty = r.y + dy[j];
if (!judge (tx, ty)) {
continue;
}
if (a[tx][ty] >= r.val) {
Union ((tx-1)*m+ty, (r.x-1)*m+r.y);
}
}
int fa = Find ((r.x-1)*m+r.y);
if (k % r.val != 0 || k / r.val > rk[fa]) {
continue;
}
puts ("YES");
num = k / r.val; aim = r.val;
BFS (r.x, r.y);
print ();
return 0;
}
puts ("NO"); return 0;
}

  

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

  1. Codeforces Round #346 (Div. 2)---E. New Reform--- 并查集(或连通图)

    Codeforces Round #346 (Div. 2)---E. New Reform E. New Reform time limit per test 1 second memory lim ...

  2. Codeforces Round #346 (Div. 2) A Round-House

    A. Round House 题目链接http://codeforces.com/contest/659/problem/A Description Vasya lives in a round bu ...

  3. Codeforces Round #346 (Div. 2) A. Round House 水题

    A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...

  4. Codeforces Round #346 (Div. 2) D Bicycle Race

    D. Bicycle Race 题目链接http://codeforces.com/contest/659/problem/D Description Maria participates in a ...

  5. Codeforces Round #346 (Div. 2) C Tanya and Toys

    C. Tanya and Toys 题目链接http://codeforces.com/contest/659/problem/C Description In Berland recently a ...

  6. Codeforces Round #346 (Div. 2) B Qualifying Contest

    B. Qualifying Contest 题目链接http://codeforces.com/contest/659/problem/B Description Very soon Berland ...

  7. Codeforces Round #346 (Div. 2) G. Fence Divercity dp

    G. Fence Divercity 题目连接: http://www.codeforces.com/contest/659/problem/G Description Long ago, Vasil ...

  8. Codeforces Round #346 (Div. 2) F. Polycarp and Hay 并查集 bfs

    F. Polycarp and Hay 题目连接: http://www.codeforces.com/contest/659/problem/F Description The farmer Pol ...

  9. Codeforces Round #346 (Div. 2) E. New Reform dfs

    E. New Reform 题目连接: http://www.codeforces.com/contest/659/problem/E Description Berland has n cities ...

  10. Codeforces Round #346 (Div. 2) D. Bicycle Race 叉积

    D. Bicycle Race 题目连接: http://www.codeforces.com/contest/659/problem/D Description Maria participates ...

随机推荐

  1. 9.12/ css3拓展、js基础语法、程序基本知识、数据类型、运算符表达方式、语句知识点

    css3拓展: <display:none>  将某个元素隐藏       <visibility:hidden>  也是将某个元素隐藏 <display:block&g ...

  2. yii过滤器的原理和使用

    过滤器官方页面:http://www.yiiframework.com/doc/guide/1.1/zh_cn/basics.controller 过滤器是一段代码,可被配置在控制器动作执行之前或之后 ...

  3. Hibernate创建hqll时报错

    Hibernate 问题,在执行Query session.createQuery(hql) 报错误 出错截图: 这条语句在java运行环境下,直接连数据库不出错,如果在hiberante,strut ...

  4. 零基础十分钟学会用git在coding.net上传(pull)和push

    ---恢复内容开始--- 对于入门者来说,特别是刚刚接触计算机的人来说,模仿是最快的学习方式了,先能够会使用(对于初学者来说,这种使用新事物的感觉很能爽的)至于原理,以后再说.下面先让初学者快速的学会 ...

  5. Mac 下 gradle 路径

    /Users/yourname/.gradle/wrapper/dists cmd:cd ~/.gradle/wrapper/dists/

  6. nyoj744(位运算)

    题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=744 思路:a^b可以得到a~b间任意两个数异或运算的长度的最大值,设为n,答案为:pow(2 ...

  7. ThinkPHP入门(二)

    smarty使用 smarty引入流程 1. 控制器IndexAction.class.php function index() $this -> display(); (父类Action的di ...

  8. React Native实例之房产搜索APP

    React Native 开发越来越火了,web app也是未来的潮流, 现在react native已经可以完成一些最基本的功能. 通过开发一些简单的应用, 可以更加熟练的掌握 RN 的知识. 在学 ...

  9. MVC缓存01,使用控制器缓存或数据层缓存

    对一些浏览频次多.数据量大的数据,使用缓存会比较好,而对一些浏览频次低,或内容因用户不同的,不太适合使用缓存.   在控制器层面,MVC为我们提供了OutputCacheAttribute特性:在数据 ...

  10. Android OkHttp完全解析 --zz

    参考文章 https://github.com/square/okhttp http://square.github.io/okhttp/ 泡网OkHttp使用教程 Android OkHttp完全解 ...