Codeforces-542div2
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的更多相关文章
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
- CodeForces - 148D Bag of mice
http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...
- CodeForces - 453A Little Pony and Expected Maximum
http://codeforces.com/problemset/problem/453/A 题目大意: 给定一个m面的筛子,求掷n次后,得到的最大的点数的期望 题解 设f[i]表示掷出 <= ...
随机推荐
- Informatic学习总结_day03_update组件学习
- nginx 配置文件[转]
#运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; ...
- WEBSHELL恶意代码批量提取清除工具
场景 使用D盾扫描到WEBSHELL后可以导出有路径的文本文件. 最后手动去把WEBSHELL复制到桌面然后以文件路径命名,挨个删除. D盾界面是这样的. 手动一个个找WEBSHELL并且改名效率太低 ...
- springboot系列十四、自定义实现starter
一.starter的作用 当我们实现了一个组建,希望尽可能降低它的介入成本,一般的组建写好了,只要添加spring扫描路径加载spring就能发挥作用.有个更简单的方式扫描路径都不用加,直接引入jar ...
- Python-GIL 进程池 线程池
5.GIL vs 互斥锁(*****) 1.什么是GIL(Global Interpreter Lock) GIL是全局解释器锁,是加到解释器身上的,保护的就是解释器级别的数据 (比如垃圾回收的数据) ...
- Highcharts 使用
官网:https://www.hcharts.cn/ api:https://api.hcharts.cn/highcharts 效果 html代码 <div id="containe ...
- 关于java中生产者消费者模式的理解
在说生产者消费者模式之前,我觉得有必要理解一下 Obj.wait(),与Obj.notify()方法.wait()方法是指在持有对象锁的线程调用此方法时,会释放对象锁,同时休眠本线程.notify() ...
- matplotlib画图
matplotlib画图 import numpy as np import matplotlib.pyplot as plt x1=[20,33,51,79,101,121,132,145,162, ...
- vue2的缓存问题(非原创)
keep-alive是vue内置的一个组件,可以使被它包含的组件处于保留状态,或避免被重新渲染. 用法: 运行结果描述: input输入框内,路由切换输入框内部的内容不会发生改变. 常见的用法:(下图 ...
- php中按指定标识及长度替换字符的方法代码
/** * 按指定标识及长度替换字符 * @param $str * @param int $start 开始的位数 * @param int $end 后面保留的位数 * @param string ...