前三题水

#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. 模拟赛1029d2

    [问题描述]祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到轨道上并加入原有序列中.一旦有三个或更多同色的珠子变 ...

  2. Stanford大学机器学习公开课(二):监督学习应用与梯度下降

    本课内容: 1.线性回归 2.梯度下降 3.正规方程组   监督学习:告诉算法每个样本的正确答案,学习后的算法对新的输入也能输入正确的答案   1.线性回归 问题引入:假设有一房屋销售的数据如下: 引 ...

  3. 隐藏<input type="file"> 实现点击div或图片打开文件选择路径

    HTML: <input type="file" style="display:none" id="addfile-btn"> ...

  4. js 删除确定

    "<td><a href='shanchu.php?c={$v[0]}' onclick=\"return confirm('确定删除么?')\"> ...

  5. 360极速浏览器安装.crx扩展(postman)

    用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网络的监视工具比如著名的Firebug等网页调试工具.今天给大家介绍的这款网页调试工具不仅可以 ...

  6. python检测文件的MD5值

    python检测文件的MD5值MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),经MD2.MD3和MD4发展而来.MD5算法的使用不需要支付任何版权 ...

  7. 学习ASP.NET缓存机制

    缓存是大型BS架构网站的性能优化通用手段,之前知道有这个概念,并且也知道很重要,但是一直没静下心来了解.这次借着学习PetShop源码的机会熟悉一下ASP.NET基本的缓存机制(生产环境中的真实缓存有 ...

  8. sqlplus使用(二)

    详见SQL*Plus® User's Guide and Reference Release 11.2   5 Using Scripts in SQL*Plus   1.定义环境变量 _EDITOR ...

  9. POJ 2299 Ultra-QuickSort 逆序数 树状数组 归并排序 线段树

    题目链接:http://poj.org/problem?id=2299 求逆序数的经典题,求逆序数可用树状数组,归并排序,线段树求解,本文给出树状数组,归并排序,线段树的解法. 归并排序: #incl ...

  10. Uncaught ReferenceError: console is not defined

    今天写javascript代码遇到了这个极其神奇的问题,居然报错说内置的console不存在,而且后来我换成了alert也不行.照例说这些都是js代码内置的东西不应该出现这种错误.不过百度之发现貌似没 ...