The 13th Chinese Northeast Collegiate Programming Contest(B C E F H J)
B. Balanced Diet
思路:把每一块选C个产生的价值记录下来,然后从小到大枚举C。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + ;
typedef long long ll;
vector<int> G[maxn];
int l[maxn], a[maxn], b[maxn];
ll dp[maxn];
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
int n, m;
cin >> n >> m;
for(int i = ;i <= m;i++){
cin >> l[i];
G[i].clear();
}
fill(dp, dp + + n, );
for(int i = ;i <= n;i++){
cin >> a[i] >> b[i];
G[b[i]].push_back(a[i]);
}
int cnt = ;
for(int i = ;i <= m;i++){
sort(G[i].begin(), G[i].end(),cmp);
if(G[i].size() < l[i]) continue;
ll k = ;
for(int j = ;j < G[i].size();j++){
if(j < l[i])
k += G[i][j];
else
dp[j + ] += G[i][j];
}
dp[l[i]] += k;
}
for(int i = ;i <= n;i++)dp[i] += dp[i-];
ll fz = ,fm = ;
for(int i = ;i <= n;i++){
long double pre = fz *1.0 / fm;
long double after = 1.0 * dp[i]/ i * 1.0;
if(after > pre) fz = dp[i] , fm = i;
}
ll d = __gcd(fz, fm);
fz /= d;
fm /= d;
cout << fz << "/" << fm << endl;
}
return ;
}
C. Line-line Intersection
思路:用map分别记录所有直线的斜率和于X轴的交点XC,如果斜率与交点都相同则直线重合,每条直线都与他斜率不同且不重合的直线有交点。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<pair<ll,ll>,ll> k ;
map<pair<pair<ll,ll>,ll>,ll> c;
int main()
{
std::ios::sync_with_stdio(false);
int t;
int n;
cin >> t;
while(t--)
{
cin >>n;
k.clear();
c.clear();
ll ans = ;
ll x1, x2, y1, y2, XC;
for(int i = ;i < n;i++)
{
cin >> x1 >> y1 >> x2 >> y2;
XC = x1 * y2 - x2 * y1;
ll x = x2 - x1;
ll y = y2 - y1;
ll d = __gcd(x, y);
x /= d;
y /= d;
XC /= d;
ans += i + c[{{x, y}, XC}] - k[{x, y}];
c[{{x, y}, XC}]++;
k[{x, y}]++;
}
cout << ans << endl;
}
return ;
}
E. Minimum Spanning Tree
思路:首先我们可以从每条边权(即每个点的出边)的贡献入手,首先一个点的出边必须连通,否则构不成最小生成树。
那么对于特定的一个点,首先将其所有出边全部的权值加一遍,然后将其最小的一个边权乘以(这一点的度degree-2)即保证了最优解当degree==1 时会把多加的ans减去,因此遍历所有点进行上述操作即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
const int INF = 0x3f3f3f3f;
struct node{
int to, cost;
bool operator <(const node &b)
{
return cost < b.cost;
}
};
vector<node> G[maxn];
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
int n;cin >> n;
for(int i = ;i <= n;i++) G[i].clear();
for(int i = ;i < n - ;i++)
{
int a, b, c;
cin >> a >> b >> c;
G[a].push_back(node{b, c});
G[b].push_back(node{a, c});
}
ll ans = ;
for(int i = ;i <= n;i++)
{
sort(G[i].begin(), G[i].end());
int minn = INF;
for(int j = ;j < G[i].size();j++){
ans += G[i][j].cost;
minn = min(minn,G[i][j].cost);
}
ans += (ll)minn*(G[i].size() - 2LL);
}
cout << ans << endl;
}
return ;
}
G. Radar Scanner
思路:题意可转化为求所有矩形移动到一个格点的最短距离,将x和y上的移动距离分开考虑,对于X轴,选一个位置,使它到所有格点的距离最小,这个X就是所有格点的中位数(不难证明),Y同理,因此可以求出那个格点(X,Y),最后一个个求一下移动到(X,Y)的距离即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + ;
ll x[maxn], y[maxn];
ll x1[maxn], x2[maxn], y1[maxn], y2[maxn];
int main()
{
std::ios::sync_with_stdio(false);
int n;
int t;
cin >> t;
while(t--)
{
cin >> n;
int cnt = ;
for(int i = ;i < n;i++)
{
cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
x[cnt] = x1[i];
y[cnt++] = y1[i];
x[cnt] = x2[i];
y[cnt++] = y2[i];
}
sort(x, x + cnt);
sort(y, y + cnt);
ll ans = ;
ll x0 = x[cnt / ], y0 = y[cnt / ];
for(int i = ;i < n;i++)
{
if(x1[i] > x0) ans += x1[i] - x0 ;
else if(x0 > x2[i])ans += x0 - x2[i];
if(y1[i] > y0) ans += y1[i] - y0 ;
else if(y0 > y2[i])ans += y0 - y2[i];
}
cout << ans << endl;
}
return ;
}
H. Skyscraper
思路:区间修改可以用差分实现,将整个数组转换成差分数组之后,就会发现所求的答案 [ l , r ] 就是a[l] + (b[l+1] ~ b[r])这段区间中非负的值的总和。a[l] = (b[1] + ... + b[l])
#include<bits/stdc++.h>
using namespace std;
#define ls rt << 1
#define rs rt << 1 | 1
#define lr2 (l + r) >> 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int maxn = 1e5 + ;
ll a[maxn],b[maxn];//b是差分数组
ll val[maxn << ], sum[maxn << ];
void pushup(int rt)
{
sum[rt] = sum[ls] + sum[rs];
val[rt] = val[ls] + val[rs];
}
void build(int l, int r, int rt)
{
if(l == r){
val[rt] = b[l];
sum[rt] = b[l] > ? b[l] : ;
return;
}
int mid = lr2;
build(lson);
build(rson);
pushup(rt);
}
void update(int k, int v, int l, int r, int rt){
if(l == r){
val[rt] += v;
sum[rt] = val[rt] > ? val[rt] : ;
return;
}
int mid = lr2;
if(k <= mid) update(k, v, lson);
else update(k, v, rson);
pushup(rt);
}
ll queryval(int a, int b, int l, int r, int rt)
{
if(a > b)return ;
if(a <= l && b >= r){
return val[rt];
}
int mid = lr2;
ll ans = ;
if(a <= mid) ans += queryval(a, b, lson);
if(b > mid) ans += queryval(a, b, rson);
return ans;
}
ll querysum(int a, int b, int l, int r, int rt)
{
if( a <= l && b >= r){
return sum[rt];
}
ll ans = ;
int mid = lr2;
if(a <= mid) ans += querysum(a, b, lson);
if(b > mid) ans += querysum(a, b, rson);
return ans;
}
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin >> t;
while(t--)
{
int n, m;
cin >> n >> m;
for(int i = ;i <= n;i++)
{
cin >> a[i];
b[i] = a[i] - a[i - ];
}
build( ,n, );
int l, r, k;
while(m--)
{
int c;
cin >> c;
if(c == ){
cin >> l >> r >> k;
update(l, k, , n, );
if(r < n)
update(r + , -k, , n, );
}
else{
cin >> l >> r;
cout << querysum(l + , r, , n, ) + queryval(, l, , n, ) << endl;
}
}
}
return ;
}
J. Time Limit
思路:x =max(3*a1, ai+1),若x是奇数再加1
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
int a;
cin >> a;
int ans = * a;
for(int i = ;i < n;i++) cin >> a,ans = max(ans, a + );
if(ans & ) ans += ;
cout << ans << endl;
}
return ;
}
The 13th Chinese Northeast Collegiate Programming Contest(B C E F H J)的更多相关文章
- The 13th Chinese Northeast Collegiate Programming Contest
题解: solution Code: A. Apple Business #include<cstdio> #include<algorithm> #include<ve ...
- 【The 13th Chinese Northeast Collegiate Programming Contest E题】
题目大意:给定一棵 N 个点的树,边有边权,定义"线树"为一个图,其中图的顶点是原树中的边,原树中两条有公共端点的边对应在线图中存在一条边,边权为树中两条边的边权和,求线图的最小生 ...
- 【The 13th Chinese Northeast Collegiate Programming Contest H 题】
题目大意:NOIP2018d1t1 支持 M 次区间查询答案和区间修改操作. 题解: 首先考虑不带区间修改的情况.从左到右进行考虑,发现对于第 i 个数来说,对答案的贡献仅仅取决于第 i-1 个数的大 ...
- ZOJ 3946.Highway Project(The 13th Zhejiang Provincial Collegiate Programming Contest.K) SPFA
ZOJ Problem Set - 3946 Highway Project Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the ...
- The 13th Zhejiang Provincial Collegiate Programming Contest - D
The Lucky Week Time Limit: 2 Seconds Memory Limit: 65536 KB Edward, the headmaster of the Marja ...
- The 13th Zhejiang Provincial Collegiate Programming Contest - I
People Counting Time Limit: 2 Seconds Memory Limit: 65536 KB In a BG (dinner gathering) for ZJU ...
- The 13th Zhejiang Provincial Collegiate Programming Contest - C
Defuse the Bomb Time Limit: 2 Seconds Memory Limit: 65536 KB The bomb is about to explode! Plea ...
- The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple - F 贪心+二分
Heap Partition Time Limit: 2 Seconds Memory Limit: 65536 KB Special Judge A sequence S = { ...
- The 2018 ACM-ICPC Chinese Collegiate Programming Contest Moving On
Firdaws and Fatinah are living in a country with nn cities, numbered from 11 to nn.Each city has a r ...
随机推荐
- 基于Zookeeper实现客户端动态监听服务器上下线
一.在具体实现之前,先来了解一下Zookeeper的监听器的原理: 图中Main()线程作为客户端,当在主线程中创建Zookeeper客户端时,会默认创建两个子线程:Listener和connect, ...
- 【学习总结】尚硅谷2019java数据结构和算法
相关链接 github:javaDSA 目录 第一章 内容介绍和授课方式 第二章 数据结构和算法概述 第三章 稀疏数组和队列 第四章 链表 第五章 栈 第六章 递归 第七章 排序算法 第八章 查找算法 ...
- swiper插件在ie浏览器无反应,解决办法
在写pc端页面时,用swiper插件发现在ie中用不了,百度下说是swiper从3以后向手机端发展,所以在pc端无响应.后来了解到,swiper3是专门针对移动端写的.如果想兼容IE8的话,应该引入s ...
- JavaScript 数组sort方法使用
直接上例子 1.无参调用返回按unicode字符编码排序 var arr = [1,2,0,21,15,6,34,9,45]; var arrSort = arr.sort(); console.lo ...
- vue.js(6)--v-model
v-model实现数据的双向绑定(简易计算器实例) 简易计算器实例 <!DOCTYPE html> <html lang="en"> <head> ...
- CSS-05 html和body标签
html和body标签 一直对这两个标签有迷惑,查了一些网上资料整理了一下. 1.html和body标签的背景 1.当给body一个背景色时候,背景图是充满整个窗口的,这里看上去是body标签下的背景 ...
- pip命令一般使用
pip类似RedHat里面的yum,安装Python包非常方便.本节详细介绍pip的安装.以及使用方法. 1.pip下载安装 1.1 pip下载 1 # wget "https://py ...
- java this关键字的用法
- ivew 绑定时间控件
<FormItem label="开始时间" style="width: 100%" prop="startDate"> < ...
- vue之路由传参三种基本方式
现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据. 父组件中: <li v-for="article in articles" @click= ...