Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)
https://codeforces.com/contest/1065
题意
给你a,b,让你找尽量多的自然数,使得他们的和<=a,<=b,用在a和b的自然数不能重复
思路
- 假如只有一个数a让你去找,问题就很简单了,就是找到一个x,使得x尽量大,且x*(x+1)/2<=a,意味着答案就是1~x
- 我的思路是从1开始累加,加到一个不能再加的数时,向后退一个数,加上n-sum(1~a[i-1])(ps:a[i+1]为不能再加的数)
错误贪心思想:假如加到不能再加,同样是只能再加一个数了,不如把小的数留给后面
然后就一直wa
正确思路
贪心策略有问题,因为留给第一个数的不一定是[1,2...5,6,100]这样两段数,有可能根据a,b的大小,第一个数也有可能是三段,四段。。。
设c=a+b,那么统一起来考虑,那么组成a和b的数一定为[1~x],其中(1+x)*x/2<=c,且x最大,
那么让x1<=a,且x1尽量大,因此找出x后,从大到小扫,确保能把a尽可能填满,那么剩下数的和一定<=b
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
ll a,b,sum,i;
vector<ll>A,B;
int main(){
cin>>a>>b;
sum=a+b;
for(i=1;;i++){
if(i*(i+1)/2>sum)break;
}i--;
for(;i>0;i--){
if(a>=i){
A.pb(i);a-=i;
}else B.pb(i);
}
cout<<A.size()<<endl;
for(i=0;i<A.size();i++)printf("%lld ",A[i]);cout<<endl;
cout<<B.size()<<endl;
for(i=0;i<B.size();i++)printf("%lld ",B[i]);cout<<endl;
}
Codeforces Round #517 (Div. 2) C. Cram Time(思维+贪心)的更多相关文章
- Codeforces Round #643 (Div. 2) B. Young Explorers (思维,贪心)
题意:给你一组人\(a\),现在要将这些人进行分组,对于\(i\),只有某一组的人数\(\ge a_{i}\)时,\(i\)才可以加入这个组,问最多能够有多少组,(不必将所有人都选用). 题解:我们将 ...
- Codeforces Round #668 (Div. 2) B. Array Cancellation (思维,贪心)
题意:有一个长度为\(n\)并且所有元素和为\(0\)的序列,你可以使\(a_{i}-1\)并且\(a_{j}+1\),如果\(i<j\),那么这步操作就是免费的,否则需要花费一次操作,问最少操 ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2)
Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) #include <bits/stdc++ ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path
http://codeforces.com/contest/1072/problem/D bfs 走1步的最佳状态 -> 走2步的最佳状态 -> …… #include <bits/ ...
- Codeforces Round #517 (Div. 2, based on Technocup 2019 Elimination Round 2) D. Minimum path(字典序)
https://codeforces.com/contest/1072/problem/D 题意 给你一个n*n充满小写字母的矩阵,你可以更改任意k个格子的字符,然后输出字典序最小的从[1,1]到[n ...
- Codeforces Round #517 (Div. 2)(1~n的分配)
题:https://codeforces.com/contest/1072/problem/C 思路:首先找到最大的x,使得x*(x+1)/2 <= a+b 那么一定存在一种分割使得 a1 &l ...
- Codeforces Round #517 Div. 2/Div. 1
\(n\)天没更博了,因为被膜你赛的毒瘤题虐哭了... 既然打了这次CF还是纪念一下. 看看NOIP之前,接下来几场的时间都不好.这应该是最后一场CF了,差\(4\)分上紫也是一个遗憾吧. A 给一个 ...
- Codeforces Round #517 (Div. 2)
A #include<queue> #include<cstdio> #include<cstring> #include<algorithm> #de ...
随机推荐
- 问题2:css图片、文字居中
1. 文本或图片水平对齐:父元素中添加以下样式 text-align : center;2. 单行文字垂直对齐:父元素中添加以下样式 line-height : 父元素高度; 3.图片 ...
- [leetcode]252. Meeting Rooms会议室有冲突吗
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- Java_9 面向对象
1.面向对象思想 面向过程的思想:自己做什么 面向对象的思想:自己找谁做,即不断地创造对象.使用对象.指挥对象做事情.万物皆对象. 面对对象的特征:封装.继承.多态. 2.java中基本单元类 成员变 ...
- ubuntu下安装php
一.下载PHP7的最新版源码 php7.0.9 下载地址 http://php.net/get/php-7.0.9.tar.gz/from/a/mirror 二.解压 tar -zxf /tmp/ph ...
- Linux后台运行进程
方式一: /bin/bash /var/www/html/crontab.sh & 缺点:终端关闭的话,进程也会关闭. 方式二: nohup /bin/bash /var/www/html/c ...
- SSH Secure Shell链接Ubuntu报错Server responded "Algorithm negotiation failed"
vim /etc/ssh/sshd_config Ciphers aes128-cbc,aes192-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr,3 ...
- js 横屏 竖屏 相关代码 与知识点
<!DOCTYPE html> <html> <head> <title></title> </head> <body&g ...
- Vue v-if 和 v-show
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- .net如何处理高并发socket,建立高性能健壮的socket服务
看到一个问题,说如何保持5000-10000+的健壮的socket服务. 初学者肯定是会把每个连接都开一个线程来监听.这很浪费资源 通常只会(动态地)占用几个线程就能保持3000个长连接会话了.“为每 ...
- Python 官方文件
7.2. 文件读写 函数 open() 返回 文件对象,通常的用法需要两个参数:open(filename, mode). >>> f = open('workfile', 'w') ...