[codeforces#592Div2]C-G题
感觉这场难度迷茫,个人觉得难度排序为$A<B<D=E=G<C<F$
C题:
比赛结束1500+pp,结果出分900+fst,我就是fst的睿智Orz。
题意为给出$n,p,w,d$,求满足下列式子的任意$x,y,z$
$x*w+y*d=p\&\& x+y+z=n\&\&x\geq 0\&\&y\geq 0\&\&z\geq 0$
如果不看$z$,式子的前半段就是扩展欧几里得,所以先求出式子$x*w+y*d=p$的一种解$(x,y)$,然后再判断解的合法性,并将解转化成合法的。
由于求解时会爆longlong,所以用的java。
import java.math.BigInteger;
import java.util.*;
public class Main {
public static BigInteger x,y;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
BigInteger n,p,w,d,gcd,xx,yy;
n = in.nextBigInteger();
p = in.nextBigInteger();
w = in.nextBigInteger();
d = in.nextBigInteger();
gcd = exgcd(w,d);
if(p.mod(gcd)!=BigInteger.ZERO) {
System.out.printf("-1\n");
return ;
}
x=x.multiply(p.divide(gcd));
y=y.multiply(p.divide(gcd));
BigInteger lcm = w.divide(gcd).multiply(d);
xx = lcm.divide(w);
yy = lcm.divide(d);
if (x.compareTo(BigInteger.ZERO)==-1 &&y.compareTo(BigInteger.ZERO)==-1) {
System.out.printf("-1\n");
return ;
}
if (x.compareTo(BigInteger.ZERO)==-1) {
x=x.add (y.divide(yy).multiply(xx));
y=y.mod(yy);
}
else if (y.compareTo(BigInteger.ZERO)==-1) {
y=y.add (x.divide(xx).multiply(yy));
x=x.mod(xx);
}
if (x.add(y).compareTo(n)!=1 && x.compareTo(BigInteger.ZERO)!=-1 && y .compareTo(BigInteger.ZERO)!=-1) {
System.out.print(x);
System.out.print(" ");
System.out.print(y);
System.out.print(" ");
System.out.print(n.subtract(x.add(y)));
return ;
}
x=x.add (y.divide(yy).multiply(xx));
y=y.mod(yy);
if (x.add(y).compareTo(n)!=1 && x.compareTo(BigInteger.ZERO)!=-1 && y.compareTo(BigInteger.ZERO)!=-1&&x.multiply(w).add(y.multiply(d)).compareTo(p)==0) {
System.out.print(x);
System.out.print(" ");
System.out.print(y);
System.out.print(" ");
System.out.print(n.subtract(x.add(y)));
return ;
}
else {
System.out.printf("-1\n");
return ;
}
}
public static BigInteger exgcd(BigInteger a,BigInteger b) {
if (b == BigInteger.ZERO) {
x = BigInteger.ONE;
y = BigInteger.ZERO;
return a;
}
BigInteger g = exgcd(b, a.mod(b));
BigInteger t = x;
x = y;
y =t.subtract((a.divide(b)).multiply(y));
return g;
}
}
D题:
题意是给一棵树每个点染色,一共三种颜色,求染色花费最少并且相邻的三个点颜色不能重复。
因为相邻的三个点颜色不能一样,所以树上每个点的度都要$\leq 2$,即只有链才能染色。而链上染色只有$6$种方法,所以枚举$6$次即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
const ll inf = 1e18;
ll a[maxn][], ans[maxn];
vector<int>p[maxn];
int cnt[][] = { {,,},{,,},{,,},{,,},{,,},{,,} };
ll dfs(int x, int fa, int w, int t) {
ll ans = a[x][cnt[w][t]];
for (int i = ; i < p[x].size(); i++) {
int y = p[x][i];
if (y == fa)continue;
ans += dfs(y, x, w, (t + ) % );
}
return ans;
}
void dfs1(int x, int fa, int w, int t) {
ans[x] = cnt[w][t];
for (int i = ; i < p[x].size(); i++) {
int y = p[x][i];
if (y == fa)continue;
dfs1(y, x, w, (t + ) % );
}
}
int main() {
int n;
scanf("%d", &n);
for (int j = ; j <= ; j++)
for (int i = ; i <= n; i++)
scanf("%lld", &a[i][j]);
int f = , root = ;
for (int i = , x, y; i < n; i++) {
scanf("%d%d", &x, &y);
p[x].push_back(y);
p[y].push_back(x);
if (p[x].size() > || p[y].size() > )
f = ;
}
for (int i = ; i <= n; i++)
if (p[i].size() == )
root = i;
if (f == )
printf("-1\n");
else {
ll Min = inf, ansi = ;
for (int i = ; i < ; i++) {
ll t = dfs(root, , i, );
if (Min > t)
Min = t, ansi = i;
}
printf("%lld\n", Min);
dfs1(root, , ansi, );
for (int i = ; i <= n; i++)
printf("%lld%c", ans[i], i == n ? '\n' : ' ');
}
}
E题:
题意说有$n$个数,有一种操作可以让一个数$+1$或$-1$,问最多$k$次操作后$min(max(a_{i})-min(a_{i}))$的值。
排序后记录一下每种数的个数,然后从首尾扫,判断哪种数的个数少,然后就乱搞。感觉比之前的要简单...
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
ll a[maxn];
pair<ll, ll>b[maxn];
int main() {
ll n, k, cnt = , num = ;
scanf("%lld%lld", &n, &k);
for (int i = ; i <= n; i++)
scanf("%lld", &a[i]);
sort(a + , a + + n);
for (int i = ; i <= n + ; i++) {
if (a[i - ] && a[i] != a[i - ])
b[++cnt] = pair<ll, ll>(a[i - ], num), num = ;
num++;
}
ll l = , r = cnt;
while (k && l != r) {
if (b[l].second <= b[r].second) {
int cg = min(b[l + ].first - b[l].first, k / b[l].second);
if (cg == )break;
b[l].first += cg;
k -= b[l].second * cg;
if (b[l].first == b[l + ].first)b[l + ].second += b[l].second, l++;
}
else {
int cg = min(b[r].first - b[r-].first, k / b[r].second);
if (cg == )break;
b[r].first -= cg;
k -= b[r].second * cg;
if (b[r].first == b[r-].first)b[r - ].second += b[r].second, r--;
}
}
printf("%lld\n", b[r].first - b[l].first);
}
F题:
题意是说一个有n个黑白点的环,操作k次,每次操作时对于每个点,点$i$的颜色变为$i,i-1,i+1$三个点颜色的众数,即为点$i-1$为黑,点$i+1$为黑,点$i$为白,则点$i$为黑。
大致画几个图就会发现,如果有两个即以上的相邻点同色,则块内的点的颜色永远都不会被改变。
所以我们只要找到每个点到达最终状态需要多少次就可以得到,将所有在块内的点初始为0,其余的状态扫正反扫两遍可以得到。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
char s[];
int vis[];
int main() {
memset(vis, 0x3f, sizeof(vis));
int n, k;
scanf("%d%d", &n, &k);
scanf("%s", s);
for (int i = ; i < n; i++)
if (s[i] == s[(i + ) % n] || s[i] == s[(i - + n) % n])
vis[i] = ;
for (int i = * n - ; i >= ; i--)
vis[i % n] = min(vis[i % n], vis[(i + ) % n] + );
for (int i = ; i < * n; i++)
vis[i % n] = min(vis[i % n], vis[(i - + n) % n] + );
for (int i = ; i < n; i++)
if (min(vis[i], k) % == )
printf("%c", 'W' + 'B' - s[i]);
else
printf("%c", s[i]);
return ;
}
G题:
题意是说有两个队列$q,p$,每个队列有$n$个人,编号为$1-n$,求最大的$ans=\sum_{i=1}^{n}max(p_{i},q_{i})\&\&ans\leq k$
先固定一个序列$q$,为$1,2,3\cdot \cdot \cdot n$,然后去构造序列$p$。
序列$p$初始也为$1,2,3\cdot \cdot \cdot n$,如果交换$p_{1},p_{n}$后的答案小于等于$k$,则交换,然后判断能否交换$p_{2},p_{n-1}$。
如果交换$p_{1},p_{n}$后的答案大于$k$,则直接找到位置$x$,使得交换$p_{1},p_{x}$后答案等于k。依次类推。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e6 + ;
int ans[maxn];
int main() {
ll n, k;
scanf("%lld%lld", &n, &k);
ll sum = (n + ) * n >> ;
if (k < sum) {
printf("-1\n");
return ;
}
for (int i = ; i <= n; i++)
ans[i] = i;
int l = , r = n;
while (l < r) {
if (sum + r - l <= k) {
swap(ans[l], ans[r]);
sum += r - l; l++; r--;
}
else {
int p = k - sum + l;
swap(ans[l], ans[p]);
sum += p - l;
break;
}
}
printf("%lld\n", sum);
for (int i = ; i <= n; i++)
printf("%d%c", i, i == n ? '\n' : ' ');
for (int i = ; i <= n; i++)
printf("%d%c", ans[i], i == n ? '\n' : ' '); }
[codeforces#592Div2]C-G题的更多相关文章
- Codeforces #550 (Div3) - G.Two Merged Sequences(dp / 贪心)
Problem Codeforces #550 (Div3) - G.Two Merged Sequences Time Limit: 2000 mSec Problem Description T ...
- Asia Yokohama Regional Contest 2018 G题 What Goes Up Must Come Down
链接 G题 https://codeforces.com/gym/102082 使其成为单峰序列需要交换多少次相邻的数. 树状数组维护逆序对. 对于每个序列中的数,要么在单峰的左侧,要么在单峰的右侧, ...
- 2017Summmer_上海金马五校 F题,G题,I题,K题,J题
以下题目均自己搜 F题 A序列 一开始真的没懂题目什么意思,还以为是要连续的子串,结果发现时序列,简直智障,知道题意之后,好久没搞LIS,有点忘了,复习一波以后,直接双向LIS,处理处两个数组L和R ...
- codeforces 407 div1 B题(Weird journey)
codeforces 407 div1 B题(Weird journey) 传送门 题意: 给出一张图,n个点m条路径,一条好的路径定义为只有2条路径经过1次,m-2条路径经过2次,图中存在自环.问满 ...
- codeforces 407 div1 A题(Functions again)
codeforces 407 div1 A题(Functions again) Something happened in Uzhlyandia again... There are riots on ...
- Codeforces 1214 F G H 补题记录
翻开以前打的 #583,水平不够场上只过了五题.最近来补一下题,来记录我sb的调试过程. 估计我这个水平现场也过不了,因为前面的题已经zz调了好久-- F:就是给你环上一些点,两两配对求距离最小值. ...
- Codeforces Round #744 (Div. 3) G题题解
淦,最后一道题没写出来,...还是我太菜了,不过这个题确实比较有趣. G. Minimal Coverage 简化题意:就是你处在坐标轴的0点上,给你一个序列\(a_i\),每次你可以选择向左走\(a ...
- Codeforces Gym 101194G Pandaria (2016 ACM-ICPC EC-Final G题, 并查集 + 线段树合并)
题目链接 2016 ACM-ICPC EC-Final Problem G 题意 给定一个无向图.每个点有一种颜色. 现在给定$q$个询问,每次询问$x$和$w$,求所有能通过边权值不超过$w$的 ...
- 【CodeForces 602B】G - 一般水的题2-Approximating a Constant Range
Description When Xellos was doing a practice course in university, he once had to measure the intens ...
随机推荐
- [大数据] hadoop全分布式安装
一.准备工作 在伪分布式的搭建基础上修改配置,搭建全分布式hadoop环境,伪分布式安装参照 hadoop伪分布式安装. 首先准备4台虚拟机,信息如下: 192.168.1.11 namenode1 ...
- C# 1.0(2002)
序言 C# 1可以看做2001年Java语言的升级版. 主要功能 类 结构 接口 事件 属性 委托 表达式 语句 特性 值类型和引用类型 装箱和拆箱 资料
- ORACLE/SQL用函数进行每年,每月,每周,每日的数据汇总
15/03/21 用函数进行每年,每月,每周,每日的数据汇总 假设一个销售明细表 sale_detail 含有 国家(country),销售时间(sale_time),销售额(sale_money) ...
- Web大文件上传断点续传解决方案
最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...
- TTTTTTTTTTTTTTTTTTTTT POJ 3690 0与* 二维哈希 模板 +multiset
Constellations Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 5923 Accepted: 1164 De ...
- Spark译文(三)
Structured Streaming Programming Guide(结构化流编程指南) Overview(概貌) ·Structured Streaming是一种基于Spark SQL引擎的 ...
- Android Handler 内存泄漏问题
1. 问题先看以下代码: 第一种写法: public class MainActivity extends AppCompatActivity { ... ... ... private class ...
- python学习---50行代码实现图片转字符画1
转自:https://blog.csdn.net/mm1030533738/article/details/78447714 项目链接: https://www.shiyanlou.com/cours ...
- PLSQL查看表创建语句
在我们想要查看的表上右键选择view:
- 获取当前线程状态--Thread类
String msgToPrint = Thread.currentThread().getStackTrace()[3] .getMethodName(); 就是调用时的方法名. 其中使用的Thre ...