题目链接:http://codeforces.com/contest/818

A. Diplomas and Certificates

题解:水题

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
int main() {
ll n , k;
cin >> n >> k;
ll gg = n / 2;
if(gg < (k + 1)) cout << 0 << ' ' << 0 << ' ' << n << endl;
else {
cout << gg / (k + 1) << ' ' << gg / (k + 1) * k << ' ' << n - gg / (k + 1) - gg / (k + 1) * k << endl;
}
return 0;
}

B. Permutation Game(构造)

题解:也是一道水题模拟构造一下就行了

if l[i + 1] > l[i]   a[l[i]] = l[i + 1] - l[i];
else    a[l[i]] = l[i + 1] - l[i] + n

最后没有被赋值的点敷一下值就行。

#include <iostream>
using namespace std;
int l[200] , a[200] , vis[200];
int main() {
int n , m;
cin >> n >> m;
int flag = 0;
for(int i = 1 ; i <= m ; i++) cin >> l[i];
for(int i = 1 ; i <= n ; i++) a[i] = 0;
for(int i = 1 ; i < m ; i++) {
if(l[i + 1] > l[i]) {
if(a[l[i]]) {
if(a[l[i]] != l[i + 1] - l[i]) {
flag = 1;
break;
}
}
a[l[i]] = l[i + 1] - l[i];
}
else {
if(a[l[i]]) {
if(a[l[i]] != l[i + 1] - l[i] + n) {
flag = 1;
break;
}
}
a[l[i]] = l[i + 1] - l[i] + n;
}
}
for(int i = 1 ; i <= n ; i++) {
vis[a[i]]++;
if(!a[i]) continue;
if(vis[a[i]] > 1) {
flag = 1;
break;
}
}
int cnt = 1;
for(int i = 1 ; i <= n ; i++) {
if(a[i] == 0) {
while(vis[cnt] != 0) cnt++;
a[i] = cnt;
vis[a[i]] = 1;
}
}
for(int i = 1 ; i <= n ; i++) if(!a[i]) {flag = 1; break;}
if(flag) cout << -1 << endl;
else {
for(int i = 1 ; i <= n ; i++) cout << a[i] << ' ';
cout << endl;
}
return 0;
}

C. Sofa Thief

题解:稍微有点麻烦的题目,主要还是求一下前缀,由于一个沙发占两格所以处理起来可能有点麻烦具体看一下代码最终还是要求前缀来解决这个问题。

#include <iostream>
#include <cstring>
using namespace std;
const int M = 1e5 + 10;
struct TnT {
int x1 , y1 , x2 , y2 , id;
}T[M];
int l[M] , r[M] , t[M] , b[M];
int sum[M];
int main() {
int d;
cin >> d;
int n , m;
cin >> n >> m;
for(int i = 1 ; i <= d ; i++) {
cin >> T[i].x1 >> T[i].y1 >> T[i].x2 >> T[i].y2 , T[i].id = i;
}
int cntl , cntr , cntt , cntb;
cin >> cntl >> cntr >> cntt >> cntb;
memset(sum , 0 , sizeof(sum));
for(int i = 1 ; i <= d ; i++) {
sum[min(T[i].x1 , T[i].x2)]++;
}
for(int i = 1 ; i <= n ; i++) {
sum[i] += sum[i - 1];
}
for(int i = 1 ; i <= d ; i++) {
if(T[i].x1 != T[i].x2) l[i] = sum[min(T[i].x1 , T[i].x2)] - 1;
else l[i] = sum[T[i].x1 - 1];
}
memset(sum , 0 , sizeof(sum));
for(int i = 1 ; i <= d ; i++) {
sum[max(T[i].x1 , T[i].x2)]++;
}
for(int i = n ; i >= 1 ; i--) {
sum[i] += sum[i + 1];
}
for(int i = 1 ; i <= d ; i++) {
if(T[i].x1 != T[i].x2) r[i] = sum[max(T[i].x1 , T[i].x2)] - 1;
else r[i] = sum[T[i].x1 + 1];
}
memset(sum , 0 , sizeof(sum));
for(int i = 1 ; i <= d ; i++) {
sum[min(T[i].y1 , T[i].y2)]++;
}
for(int i = 1 ; i <= m ; i++) {
sum[i] += sum[i - 1];
}
for(int i = 1 ; i <= d ; i++) {
if(T[i].y1 != T[i].y2) t[i] = sum[min(T[i].y1 , T[i].y2)] - 1;
else t[i] = sum[T[i].y1 - 1];
}
memset(sum , 0 , sizeof(sum));
for(int i = 1 ; i <= d ; i++) {
sum[max(T[i].y1 , T[i].y2)]++;
}
for(int i = m ; i >= 1 ; i--) {
sum[i] += sum[i + 1];
}
for(int i = 1 ; i <= d ; i++) {
if(T[i].y1 != T[i].y2) b[i] = sum[max(T[i].y1 , T[i].y2)] - 1;
else b[i] = sum[T[i].y1 + 1];
}
int flag = 0;
for(int i = 1 ; i <= d ; i++) {
//cout << l[i] << ' ' << r[i] << ' ' << t[i] << ' ' << b[i] << endl;
if(cntl == l[i] && cntr == r[i] && cntt == t[i] && cntb == b[i]) {
flag = 1;
cout << i << endl;
break;
}
}
if(!flag) cout << -1 << endl;
return 0;
}

D. Multicolored Cars (优先队列)

题解:可用优先队列辅助一下具体看一下代码挺好理解的。一旦出现出现次数最小的颜色小于A直接pop出去。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int M = 1e6 + 10;
int cnt[M] , vis[M];
struct TnT {
int num;
TnT() {}
TnT(int num):num(num) {}
bool operator <(const TnT &a) const {
return cnt[num] > cnt[a.num];
}
};
priority_queue<TnT> q;
int main() {
int n , A;
scanf("%d%d" , &n , &A);
int count = 0 , flag = 1;
memset(vis , 0 , sizeof(vis));
for(int i = 1 ; i <= n ; i++) {
int c;
scanf("%d" , &c);
if(c == A) count++;
else {
cnt[c]++;
if(cnt[c] > count && !vis[c]) q.push(c) , vis[c] = 1;
else vis[c] = 1;
}
while(!q.empty()) {
TnT gg = q.top();
if(cnt[gg.num] >= count) break;
q.pop();
}
if(q.empty()) flag = 0;
}
if(!flag) printf("-1\n");
else {
if(q.empty()) printf("-1\n");
else printf("%d\n" , q.top().num);
}
return 0;
}

E. Card Game Again(思维+二分)

题解:现将k因式分解然后拿因式的和求前缀显然是递增的然后遍历一遍向前二分

#include <iostream>
#include <cstring>
using namespace std;
const int M = 1e5 + 10;
typedef long long ll;
ll gcd(ll a , ll b) {
return (b <= 0) ? a : gcd(b , a % b);
}
struct dig {
ll num;
int sum;
};
struct TnT {
int Size;
dig a[64];
TnT():Size(0) {
memset(a , 0 , sizeof(a));
}
TnT operator +=(const TnT &b) {
for(int i = 0 ; i < 64 ; i++) {
a[i].sum += b.a[i].sum;
}
return (*this);
}
TnT operator -(const TnT &b) {
for(int i = 0 ; i < 64 ; i++) {
a[i].sum -= b.a[i].sum;
}
return (*this);
}
}s , pre[M];
void getTnT(ll x , TnT &gg) {
gg.a[gg.Size].num = 1 , gg.a[gg.Size].sum++;
gg.Size++;
ll i;
for(i = 2 ; i * i <= x ; i++) {
if(x % i == 0) {
gg.a[gg.Size].num = i;
while(!(x % i)) x /= i , gg.a[gg.Size].sum++;
gg.Size++;
}
}
if(x >= i) gg.a[gg.Size].num = x , gg.a[gg.Size].sum++ , gg.Size++;
}
void getTnT_arr(ll x , TnT &gg) {
int len = s.Size;
gg.a[0].num = 1 , gg.a[0].sum++;
for(int i = 1 ; i < len ; i++) {
if(x % s.a[i].num == 0) {
gg.a[i].num = s.a[i].num;
while(!(x % s.a[i].num)) x /= s.a[i].num , gg.a[i].sum++;
}
else gg.a[i].num = s.a[i].num , gg.a[i].sum = 0;
}
gg.Size = len;
}
ll arr[M];
int cau(int pos , int ed) {
TnT gl = pre[ed];
TnT gg = gl - pre[pos - 1];
int flag = 0;
for(int i = 0 ; i < s.Size ; i++) {
if(gg.a[i].sum < s.a[i].sum) {
flag = 1;
break;
}
}
return flag;
}
int binsearch(int l , int r , int ed) {
int mid = (l + r) >> 1;
int ans = 0;
while(l <= r) {
mid = (l + r) >> 1;
if(cau(mid , ed)) r = mid - 1;
else {
ans = mid;
l = mid + 1;
}
}
return ans;
}
int main() {
int n;
ll k;
scanf("%d%lld" , &n , &k);
getTnT(k , s);
pre[0].Size = s.Size;
for(int i = 1 ; i <= n ; i++) {
scanf("%lld" , &arr[i]);
arr[i] = gcd(arr[i] , k);
getTnT_arr(arr[i] , pre[i]);
pre[i] += pre[i - 1];
}
ll ans = 0;
for(int i = 1 ; i <= n ; i++) {
int pos = binsearch(1 , i , i);
ans += pos;
}
printf("%lld\n" , ans);
return 0;
}

F. Level Generation(三分)

题解:主要还是推一下公式一共有n个点,有k个点是任意联通的那么显然有n-k个点是单连通的那么n-k个单连通的点贡献的边数是(n-k)条边。k个点最多能连接的边的个数是min(n-k(由于要求桥的个数要超过总边数的一半),k*(k-1)/2(k个点最多能连接的边的个数)),这个自行理解一下。然后我们可以发现边的贡献是

n-k+min(n-k,k*(k-1)/2)。像这种有单峰或单谷的函数可以利用三分来求最值

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n;
bool cau(ll k) {
if(k * (k - 1) / 2 > n - k) return true;
else return false;
}
ll ternsearch(ll l , ll r) {
ll mid = (l + r) >> 1;
ll sum = 0;
if(cau(mid)) sum = 2 * n - 2 * mid;
else sum = n - mid + mid * (mid - 1) / 2;
while(l <= r) {
mid = (l + r) >> 1;
ll midl = (l + mid) >> 1;
ll midr = (r + mid) >> 1;
ll now = 0 , sum1 = 0 , sum2 = 0;
if(cau(mid)) now = 2 * n - 2 * mid;
else now = n - mid + mid * (mid - 1) / 2;
sum = max(sum , now);
if(cau(midl)) sum1 = 2 * n - 2 * midl;
else sum1 = n - midl + midl * (midl - 1) / 2;
sum = max(sum1 , sum);
if(cau(midr)) now = 2 * n - 2 * midr;
else now = n - midr + midr * (midr - 1) / 2;
sum = max(sum2 , sum);
if(cau(midl) && cau(midr)) r = mid - 1;
else if(!cau(midl) && !cau(midr)) l = mid + 1;
else {
if(sum1 < sum2) r = midr - 1;
else l = midl + 1;
}
}
return sum;
}
int main() {
int q;
scanf("%d" , &q);
while(q--) {
scanf("%lld" , &n);
printf("%lld\n" , ternsearch(1 , n));
}
return 0;
}

codeforces Educational Codeforces Round 24 (A~F)的更多相关文章

  1. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) F. Isomorphic Strings 题目连接: http://cod ...

  2. Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes

    Codeforces Educational Codeforces Round 44 (Rated for Div. 2) E. Pencils and Boxes 题目连接: http://code ...

  3. codeforces Educational Codeforces Round 5 A. Comparing Two Long Integers

    题目链接:http://codeforces.com/problemset/problem/616/A 题目意思:顾名思义,就是比较两个长度不超过 1e6 的字符串的大小 模拟即可.提供两个版本,数组 ...

  4. Codeforces Educational Codeforces Round 15 E - Analysis of Pathes in Functional Graph

    E. Analysis of Pathes in Functional Graph time limit per test 2 seconds memory limit per test 512 me ...

  5. Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分,贪心

    D. Gadgets for dollars and pounds 题目连接: http://www.codeforces.com/contest/609/problem/C Description ...

  6. Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge 树上倍增

    E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...

  7. codeforces Educational Codeforces Round 16-E(DP)

    题目链接:http://codeforces.com/contest/710/problem/E 题意:开始文本为空,可以选择话费时间x输入或删除一个字符,也可以选择复制并粘贴一串字符(即长度变为两倍 ...

  8. Codeforces Educational Codeforces Round 15 D. Road to Post Office

    D. Road to Post Office time limit per test 1 second memory limit per test 256 megabytes input standa ...

  9. Codeforces Educational Codeforces Round 15 C. Cellular Network

    C. Cellular Network time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. Codis与RedisCluster的原理详解

    背景介绍 我们先来看一下为什么要做集群,如果我们要部署一个单节点Redis,很明显会遇到单点故障的问题. 首先能想到解决单点故障的方法,就是做主从,但是当有海量存储需求时,单一的主从结构就会出问题,说 ...

  2. 最基础的 ant build 脚本

    最基础的 ant build 脚本,根据项目,自行进行修改 <?xml version="1.0" encoding="UTF-8" ?> < ...

  3. [译]使用golang每分钟处理百万请求

    [译]使用golang每分钟处理百万请求 在Malwarebytes,我们正在经历惊人的增长,自从我在1年前加入硅谷的这家公司以来,我的主要职责是为多个系统做架构和开发,为这家安全公司的快速发展以及百 ...

  4. java并发编程(十八)----(线程池)java线程池框架Fork-Join

    还记得我们在初始介绍线程池的时候提到了Executor框架的体系,到现在为止我们只有一个没有介绍,与ThreadPoolExecutor一样继承与AbstractExecutorService的For ...

  5. LeetCode——264. Ugly Number II

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  6. 再次学习Git版本控制工具

    Git 究竟是怎样的一个系统呢?为什么在SVN作为版本控制工具已经非常流行的时候,还有Git这样一个版本控制工具呢?Git和SVN的区别在哪儿呢?Git优势又在哪呢?下面PHP程序员雷雪松带你一起详细 ...

  7. JMeter的JavaRequest探究

    1.背景 最近笔者的一位老朋友咨询了一个问题:在自定义的Java请求中如何编写多个请求?老朋友反应他们发送请求只能基于这种Java请求形式(代码调需用三方封装的jar包).这个问题恰巧不久前在笔者所在 ...

  8. luoguP3588_[POI2015]PUS

    题意 有一个\(n\)个数的序列,已知其中的\(k\)个数,然后有\(m\)个信息,每个信息给出区间\([l,r]\),和\(k\)个数,表示区间\([l,r]\)中这\(k\)个数大于剩下的\(r- ...

  9. spark sql/hive小文件问题

    针对hive on mapreduce 1:我们可以通过一些配置项来使Hive在执行结束后对结果文件进行合并: 参数详细内容可参考官网:https://cwiki.apache.org/conflue ...

  10. Okhttp3源码解析(2)-Request分析

    ### 前言 前面我们讲了 [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析]( ...