题目链接: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. Kubernetes容器集群管理环境 - 完整部署(上篇)

    Kubernetes(通常称为"K8S")是Google开源的容器集群管理系统.其设计目标是在主机集群之间提供一个能够自动化部署.可拓展.应用容器可运营的平台.Kubernetes ...

  2. String常量池和intern方法

    String s1 = "Hello"; String s2 = "Hello"; String s3 = "Hel" + "lo ...

  3. JDK1.8源码分析01之学习建议(可以延伸其他源码学习)

    序言:目前有个计划就是准备看一下源码,来提升自己的技术实力.同时现在好多面试官都喜欢问源码,问你是否读过JDK源码等等? 针对如何阅读源码,也请教了我的老师.下面就先来看看老师的回答,也许会有帮助呢. ...

  4. 关于Unity 中对UGUI制作任务系统的编程

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  5. 图像反转(一些基本的灰度变换函数)基本原理及Python实现

    1. 基本原理 获取像素值在[0, L]范围内的图像的反转图像,即为负片.适用于增强图像中白色或者灰色的区域,尤其当黑色在图片中占主地位时候 $$T(r) = L-r$$ 2. 运行结果 图源自ski ...

  6. 佳木斯集训Day8

    本来能AK的啊啊啊啊啊,唯一一天可以AK,却被Champion误导了(好吧实际上是我理解有问题) T1我写了俩小时,就是一道数列题,推公式的,可以二分解,我觉得二分麻烦,就直接想O(1)了 #incl ...

  7. java随笔之接口

    /* * 接口大致上可以分为:哑接口,抽象接口,接口类 * 哑接口:就是public,protected(注意protect有包权限,只有本包才开放接口)方法 * 抽象接口:就是哑接口变为抽象方法,在 ...

  8. Go中的命名规范

    1.命名规范 1.1 Go是一门区分大小写的语言. 命名规则涉及变量.常量.全局函数.结构.接口.方法等的命名. Go语言从语法层面进行了以下限定:任何需要对外暴露的名字必须以大写字母开头,不需要对外 ...

  9. requestAnimationFrame 兼容方案

    [toc] 编写涉及:css, html, js 在线演示codepen html代码 <div class="roll-box"> <div class=&qu ...

  10. 【RabbitMQ】如何进行消息可靠投递【上篇】

    说明 前几天,突然发生线上报警,钉钉连发了好几条消息,一看是RabbitMQ相关的消息,心头一紧,难道翻车了? [橙色报警] 应用[xxx]在[08-15 16:36:04]发生[错误日志异常],al ...