https://www.cnblogs.com/31415926535x/p/10468017.html

codeforces-1130A~G

和队友做了一套题,,

A. Be Positive

题意

题意是给你一串整数,,要找到一个除数使得每一个数被除后正数的个数大于等于 \(\lceil \frac{n}{2} \rceil\),,,

分析

统计出所有正数,负数的个数,,正数多那个除数就是1,负数多就是-1

代码

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 2e5 + 5;
const ll mod = 1e9 + 7;
int a[maxn];
int main()
{
// freopen("233.in" , "r" , stdin);
// freopen("233.out" , "w" , stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n; cin >> n;
for(int i = 1; i <= n; ++i)cin >> a[i];
sort(a + 1, a + 1 + n);
int nump = 0;
int numn = 0;
for(int i = 1; i <= n; ++i)
if(a[i] > 0)
++nump;
else if(a[i] < 0)
++numn;
if(nump >= (n + 1) / 2)
cout << 1 << endl;
else if(numn >= (n + 1) / 2)
cout << -1 << endl;
else
cout << 0 << endl;
return 0;
}

B. Two Cakes

题意

题意是由两组1~n的数组成的序列,,每一个人选择一组,,费用是两个树之间的距离,,然后问你总距离最小是多少,,

分析

我一开始想着先贪心处理一个人的选择出最少的,,再加上剩下的那个人的,,然后就wa了,,因为这样并不保证这一次选的和下一次选的距离和是最小的,,解决方法是两个一起处理,,考虑每一种选择的情况,,这样取最小的就行了,,,

代码

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 1e4 + 5;
const ll mod = 1e9 + 7;
int a[maxn][2];
bool flag[maxn];
int main()
{
// freopen("233.in" , "r" , stdin);
// freopen("233.out" , "w" , stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n;cin >> n;
int t;
memset(flag, false, sizeof flag);
for(int i = 1; i <= 2 * n; ++i)
{
cin >> t;
if(!flag[t])
{
a[t][0] = i;
flag[t] = true;
}
else
a[t][1] = i;
}
ll ans = a[1][0] + a[1][1] - 2;
for(int i = 1; i <= n - 1; ++i)
{
int p = abs(a[i + 1][0] - a[i][0]) + abs(a[i + 1][1] - a[i][1]);
int q = abs(a[i + 1][0] - a[i][1]) + abs(a[i + 1][1] - a[i][0]);
ans += min(p, q);
}
cout << ans << endl;
return 0;
}

C. Connect

题意

给你一个地图,,其中陆地是0,水则是1,,然后给你一个起点一个终点,,你可以在任意两块陆地上建 一条 隧道使这两片陆地相通,,然后问你起点到终点需要的隧道的最小长度,,,

分析

因为只能建一条隧道,,所以如果起点所在的陆地与终点所在的陆地不相通的话,,那么这条隧道一定在这两片陆地之间,,数据量不大,,直接枚举这两片陆地上的点,,取最小的距离就行了,,,

判断一个点是否在起点或终点所在的陆地可以现用并查集把地图 “染色”,,,这样就可以枚举了,,,

代码

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 2e5 + 5;
const ll mod = 1e9 + 7;
int fa[maxn];
int _find(int x)
{
if(fa[x] == x)return x;
return fa[x] = _find(fa[x]);
}
void _union(int x, int y)
{
int f1 = _find(x);
int f2 = _find(y);
if(f1 != f2)fa[f1] = f2;
else fa[f2] = f1;
}
int mp[60][60];
int solve(int i, int j, int n)
{
int x1 = i / n;
int y1 = i - x1 * n;
int x2 = j / n;
int y2 = j - x2 * n;
if(y1 == 0)
{
y1 = n;
--x1;
}
if(y2 == 0)
{
y2 = n;
--x2;
}
// cout << x1 << y1 << x2 << y2 << endl;
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
int main()
{
// freopen("233.in" , "r" , stdin);
// freopen("233.out" , "w" , stdout);
// ios_base::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
int n;scanf("%d", &n);
int x1, x2, y1, y2;
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
for(int i = 1; i <= n; ++i)
{
getchar();
for(int j = 1; j <= n; ++j)
mp[i][j] = (int)(getchar() - '0'); } for(int i = n + 1; i <= n + 1 + n * n; ++i)fa[i] = i;
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= n; ++j)
{
if(mp[i - 1][j] == mp[i][j] && i - 1 >= 1)
_union(i * n + j, (i - 1) * n + j);
if(mp[i + 1][j] == mp[i][j] && i + 1 <= n)
_union(i * n + j, (i + 1) * n + j);
if(mp[i][j + 1] == mp[i][j] && j + 1 <= n)
_union(i * n + j, i * n + j + 1);
if(mp[i][j - 1] == mp[i][j] && j - 1 >= 1)
_union(i * n + j, i * n + j - 1);
}
}
// for(int i = 1; i <=n; ++i)
// {
// for(int j = 1; j <= n; ++j)
// cout << _find(i * n + j) << " ";
// cout << endl;
// }
int s = _find(x1 * n + y1);
int t = _find(x2 * n + y2);
// cout << s << t << endl;
int ans = inf;
for(int i = n + 1; i <= n + 1 + n * n; ++i)
{
for(int j = 1 + n; j <= n + 1 + n * n; ++j)
{
if(_find(i) == s && _find(j) == t)
{
ans = min(ans, solve(i, j, n));
}
}
}
cout << ans << endl;
return 0;
}

D1. Toy Train

题意

由一个环形的铁路,,上面有n个车站,,每个车站有一些糖果,,这些糖果要运到 \(b_i\) 那个车站,,,火车只能在一个车站拉上一个糖果,,但是可以放下任意块糖果,,,问你从这n个车站出发送完所有的糖果所需的最少的时间,,

分析

每次只能上一个糖果,,最后下的糖果就是糖果数量最多的车站的,,找一个从这个车站出发花费最多的另一个车站,,这样把那个车站所有的糖果送完时其他车站的糖果顺带也就送完了,,,

枚举每一个车站i,,对于车站i枚举所有的其他的车站,,求出所有的时间里的最大值就是这个车站所用的时间了,,,

参考

代码

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 1e4 + 5;
const ll mod = 1e9 + 7;
struct node
{
int num;
int mi;
}node[maxm];
int n, m;
int getdis(int i, int j)
{
//get the dis of i -> j
if(i <= j)return j - i;
else return n - i + j;
}
int solve(int loc)
{
//find the furthest and the most candies node
int fur = loc;
int num = node[loc].num;
int ans = 0;
int dis;
for(int i = loc; i <= n; ++i)
{
if(node[i].mi == inf)continue;
dis = getdis(loc, i) + (node[i].num - 1) * n + node[i].mi;
ans = max(ans, dis);
}
for(int i = 1; i <= loc - 1; ++i)
{
if(node[i].mi == inf)continue;
dis = getdis(loc, i) + (node[i].num - 1) * n + node[i].mi;
ans = max(ans, dis);
}
// for(int i = loc; i <= n; ++i)
// {
// if(node[i].num >= num)
// {
// fur = i;
// num = node[i].num;
// }
// }
// for(int i = 1; i <= loc - 1; ++i)
// {
// if(node[i].num >= num)
// {
// fur = i;
// num = node[i].num;
// }
// }
// cout << fur << " ";
// int ans = n * (node[fur].num - 1);
// ans += getdis(loc, fur);
// ans += getdis(fur, node[fur].mi);
return ans;
}
int main()
{
// freopen("233.in" , "r" , stdin);
// freopen("233.out" , "w" , stdout);
// ios_base::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
cin >> n >> m;
int a, b;
for(int i = 1; i <= n; ++i)node[i].mi = inf;
for(int i = 1; i <= n; ++i)node[i].num = 0;
for(int i = 1; i <= m; ++i)
{
cin >> a >> b;
++node[a].num;
if(getdis(a, b) <= node[a].mi)
node[a].mi = getdis(a, b);
}
for(int i = 1; i <= n; ++i)
cout << solve(i) << " ";
cout << endl;
// for(int i = 1; i <= n; ++i)
// {
// cout << i << " ";
// cout << solve(i) << endl;
// }
return 0;
}

E. Wrong Answer

题意

一个数列求出最大的 区间和乘以区间长度,,

他给的算法当前面一段区间和出现负数就舍弃了,,没有考虑长度对最后答案的影响,,,

题目要我们构造一个数列,,使得这个数列的正确答案比它的做法算出的结果大k

分析

可以构造一个前面1998个都是0,,后面一个数是-p,一个时p + q,,,

这样正确的答案就是 \(2000q\),,,他算出的答案就是 \(p + q\),,,

要大k,,就是 \(2000q - (p+q)=k\),,也就是 \(q= \frac{p+k}{1999}\) ,,,为了保证p,q都是整数,,,那么就设 \(p=1999-k\%1999\),,这样算出的q就是整数,,,

//cf
#include <bits/stdc++.h>
//#include <iostream>
//#include <cstdio>
//#include <cstdlib>
//#include <string.h>
//#include <algorithm>
#define aaa cout<<233<<endl;
#define endl '\n'
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
const int inf = 0x3f3f3f3f;//1061109567
const ll linf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-6;
const double pi = 3.14159265358979;
const int maxn = 1e6 + 5;
const int maxm = 1e4 + 5;
const ll mod = 1e9 + 7; int main()
{
// freopen("233.in" , "r" , stdin);
// freopen("233.out" , "w" , stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int k; cin >> k;
cout << 2000 << endl;
for(int i = 1; i <= 2000 - 2; ++i)cout << 0 << " ";
int p = 1999 - k % 1999;
cout << -p << " " << ((k + p) / 1999 + p) << endl;
return 0;
}

(end)

Codeforces-542div2的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

  10. CodeForces - 453A Little Pony and Expected Maximum

    http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...

随机推荐

  1. mysql 原理~ index的详解

    一 简介:今天咱们来介绍下index的一些东西 二 数据的基本存储结构 1 磁盘空间被划分为许多大小相同的块(Block) 在内存中读出是页(Page).   2 一个表的这些数据块以链表的方式串联在 ...

  2. js给<img>的src赋值

    用js原生方法:document.getElementById("imageId").src = "xxxx.jpg";用Jquery方法:$("#i ...

  3. python 内置函数总结(大部分)

    python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...

  4. ftruncate(改变文件大小)

    ftruncate(改变文件大小) 定义函数 int ftruncate(int fd,off_t length); 函数说明 ftruncate()会将参数fd指定的文件大小改为参数length指定 ...

  5. Linux命令:pigz多线程压缩工具【转】

    学习Linux系统时都会学习这么几个压缩工具:gzip.bzip2.zip.xz,以及相关的解压工具.关于这几个工具的使用和相互之间的压缩比以及压缩时间对比可以看:Linux中归档压缩工具学习 那么P ...

  6. java多线程系列六、线程池

    一. 线程池简介 1. 线程池的概念: 线程池就是首先创建一些线程,它们的集合称为线程池. 2. 使用线程池的好处 a) 降低资源的消耗.使用线程池不用频繁的创建线程和销毁线程 b) 提高响应速度,任 ...

  7. 转载:Java高并发,如何解决,什么方式解决

    原文:https://www.cnblogs.com/lr393993507/p/5909804.html 对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了.而并 ...

  8. vue系列之核心思想

    1.数据驱动 只要改变数据,Vuejs会通过Directives指令对DOM进行封装,当数据发生变化,会通知相应的DOM进行变化 Vuejs会对DOM进行监听,通过DOMListeners监听视图的变 ...

  9. 测试开发之Django——No7.Django模板中的过滤器

    1.add 将参数添加到值. 例如: {{ value|add:"2" }} 如果value是4,那么输出将是6. 此过滤器将首先尝试将两个值强制转换为整数.如果失败,它将尝试将值 ...

  10. java多线程快速入门(十)

    synchonizd解决安全性问题 package com.cppdy; class MyThread6 implements Runnable{ private Integer ticketCoun ...