AtCoder Beginner Contest 127 D,E,F
题意:先给出n个数字,然后可以有m次操作,每次操作以数字对(x,y)表示最多能选x个数字把它变成y,问经历m次操作后n个数字和最大为多少?
解法:一个明显正确的做法是:用y尽量大的操作去改变数组,直到不能改变(产生负收益)为止。
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+;
int n,m,a[N];
struct dat{
int x,y;
bool operator < (const dat &rhs) const {
return y>rhs.y;
}
}b[N]; int main()
{
cin>>n>>m;
for (int i=;i<=n;i++) scanf("%d",&a[i]);
sort(a+,a+n+);
for (int i=;i<=m;i++) scanf("%d%d",&b[i].x,&b[i].y);
sort(b+,b+m+);
int nowa=,nowb=;
while (nowa<=n && nowb<=m) {
if (b[nowb].y<=a[nowa]) break;
for (int i=;i<=b[nowb].x;i++) {
if (nowa>n) break;
if (b[nowb].y<=a[nowa]) break;
a[nowa]=b[nowb].y; nowa++;
}
nowb++;
}
long long ans=;
for (int i=;i<=n;i++) ans+=(long long)a[i];
cout<<ans<<endl;
return ;
}
题意:给出n*m的表格,每次选k个,然后计算这k个格子任两个欧几里得距离和。算出以上所有不同方案答案距离总和。
解法:这题一看题目明显往算贡献方面想。选k个格子方案数是C(n,m),然后k个格子任两个欧几里得距离有C(k,2)对,那么每一对的距离是多少呢?我们注意到每一对的距离都可能不尽相同,但是题目要求计算的是任两对的距离,那么我们就可以考虑用总数*平均得到总和,这里有一个结论:任两个格子的平均欧几里得距离是(n+m)/3。那么答案就是:C(n*m,k)*C(k,2)*(n+m)/3。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int P=1e9+;
const int N=2e5+;
int n,m,k;
LL fac[N],inv[N]; LL power(LL x,LL p) {
LL ret=;
for (;p;p>>=) {
if (p&) ret=ret*x%P;
x=x*x%P;
}
return ret;
} void prework() {
fac[]=; inv[]=power(fac[],P-);
for (int i=;i<=;i++) {
fac[i]=fac[i-]*i%P;
inv[i]=power(fac[i],P-);
}
} LL C(LL n,LL m) {
return fac[n]*inv[m]%P*inv[n-m]%P;
} int main()
{
prework();
cin>>n>>m>>k;
LL ans=C(n*m,k)%P*C(k,)%P*(n+m)%P*power(,P-)%P;
cout<<ans<<endl;
return ;
}
题意:每个给出操作或查询,操作是给出(a,b)然后f(x)=f(x)+|x-a|+b,查询是查询所有f(x)的最小值坐标x以及最小值f(x)。
解法:大但猜想最小值坐标是不是a的中位数,手动尝试几个(a,b)操作后发现确实是这样。那么这题就变成景经典的动态中位数问题了,用大根堆小根堆解法可以解决。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
priority_queue<int,vector<int>,greater<int> > q1;
priority_queue<int> q2;
LL sum,sum1,sum2; int main()
{
int n,m; cin>>m;
while (m--) {
int opt; scanf("%d",&opt);
if (opt==) {
int x,y; scanf("%d%d",&x,&y);
n++; sum+=y;
q1.push(x); sum1+=x;
while (!q1.empty() && !q2.empty() && q1.top()<q2.top()) {
sum1-=q1.top(); sum1+=q2.top();
sum2-=q2.top(); sum2+=q1.top();
int t=q1.top(); q1.pop();
q1.push(q2.top());
q2.pop(); q2.push(t);
}
if (q1.size()-q2.size()>=)
sum1-=q1.top(),sum2+=q1.top(),q2.push(q1.top()),q1.pop();
} else {
int mid;
if (n%) mid=q1.top(); else mid=q2.top();
LL ans=((LL)mid*(n/)-sum2)+(sum1-(LL)mid*(n-n/))+sum;
printf("%d %lld\n",mid,ans);
}
}
return ;
}
AtCoder Beginner Contest 127 D,E,F的更多相关文章
- AtCoder Beginner Contest 127 解题报告
传送门 非常遗憾.当天晚上错过这一场.不过感觉也会掉分的吧.后面两题偏结论题,打了的话应该想不出来. A - Ferris Wheel #include <bits/stdc++.h> u ...
- 【AtCoder Beginner Contest 181】A~F题解
越学越菜系列 于2020.11.2,我绿了(错乱) A - Heavy Rotation 签到题,奇数Black,偶数White. code: #include<bits/stdc++.h> ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 238 A - F 题解
AtCoder Beginner Contest 238 \(A - F\) 题解 A - Exponential or Quadratic 题意 判断 \(2^n > n^2\)是否成立? S ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 154 题解
人生第一场 AtCoder,纪念一下 话说年后的 AtCoder 比赛怎么这么少啊(大雾 AtCoder Beginner Contest 154 题解 A - Remaining Balls We ...
- AtCoder Beginner Contest 153 题解
目录 AtCoder Beginner Contest 153 题解 A - Serval vs Monster 题意 做法 程序 B - Common Raccoon vs Monster 题意 做 ...
- AtCoder Beginner Contest 177 题解
AtCoder Beginner Contest 177 题解 目录 AtCoder Beginner Contest 177 题解 A - Don't be late B - Substring C ...
- AtCoder Beginner Contest 161
比赛链接:https://atcoder.jp/contests/abc161/tasks AtCoder Beginner Contest 161 第一次打AtCoder的比赛,因为是日本的网站终于 ...
随机推荐
- mysql百万级别重排主键id(网上的删除重建id在大数据量下会出错)
网上教程: 先删除旧的主键 再新建主键 :数据量少时没问题,不会出现主键自增空缺间隔的情况(如:1,2,3,5):但是大数据量时会出现如上所述问题(可能是内部mysql多进程或多线程同时操作引起问题) ...
- Codeforces 360D Levko and Sets (数论好题)
题意:有一个长度为n的数组a和一个长度为m的数组b,一个素数p.有n个集合,初始都只有一个1.现在,对(i从1到n)第i个集合执行以下操作: 对所有集合中的元素c,把c * (a[i] ^ b[j]) ...
- opengl 库glew
OpenGL OpenGL是个专业的3D程序接口,是一个功能强大,调用方便的底层3D图形库.OpenGL的前身是SGI公司为其图形工作站开发的IRIS GL.IRIS GL是一个工业标准的3D图形软件 ...
- Python3.5-20190516-廖老师-自我笔记-匿名函数-装饰器
当函数很简单的时候采用匿名函数很方便.
- 【leetcode】1003. Check If Word Is Valid After Substitutions
题目如下: We are given that the string "abc" is valid. From any valid string V, we may split V ...
- 【leetcode】994. Rotting Oranges
题目如下: In a given grid, each cell can have one of three values: the value 0 representing an empty cel ...
- apue 第6章 系统数据文件和信息
在给出用户登录名或数值用户ID后,这两个函数就能查看相关项. #include <sys/types.h> #include <pwd.h> struct passwd *ge ...
- SPOJ - VLATTICE (莫比乌斯反演)
Consider a N*N*N lattice. One corner is at (0,0,0) and the opposite one is at (N,N,N). How many latt ...
- 学习java web中的listener
web.xml里的顺序为:context-param->listener->filter->servlet 监听器是需要新建一个类,然后按监听的对象继承:ServletContext ...
- Python基础教程(006)--Python的特点
前言 了解Python背景,明白Python在目前社会中的标准库是有成千上万的Python爱好者共同维护的. 知识点 Python是完全面相对象的语言 函数,模块,数字,字符串都是对象,在Python ...