P1338 末日的传说,P1372 P1414 又是毕业季——贪心
2024-10-06 09:35:17
一个1到n序列,合理排序逆序对数要求是m,而且字典序要求最小;
这个题,因为数字只能用一次,所以我们可以知道什么位置放什么数逆序对的个数会增加或减少多少;
先求出最多能产生的数量,每次先输出最小的数,用总数减去减少的逆序对数;
如果不够的时候就要用大数排在前面;
vector,记得输出一个删一个;
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll n,m;
ll now;
vector<ll> v;
int main()
{
scanf("%lld%lld",&n,&m);
for(ll i=;i<=n;i++) v.push_back(i);
ll s=n*(n-)/;
for(int i=n-;i>=;i--)
{
s-=i;
if(m>s)
{
now=m-s;
m=s;
}
else now=;
printf("%lld ",v[now]);
v.erase(v.begin()+now);
}
return ;
}
又是毕业季我什么也不想说
1到n这些数中,找到k个,使他们的GCD最大,输出GCD;
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,k;
int main()
{
scanf("%d%d",&n,&k);
printf("%d",n/k);
return ;
}
又是毕业季II
这回数字不连续而且k的值是变的;
把所有的数的公约数的数量记一下就好了
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=1e6+;
int n,x;
int c[maxn];
int maxx;
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
maxx=max(maxx,x);
int m=sqrt(x);
for(int j=;j<=m;j++)
{
if(x%j==)
{
c[j]++;
if(x/j!=j) c[x/j]++;
}
}
}
x=maxx;
for(int i=;i<=n;i++)
{
while(c[x]<i) x--;
printf("%d\n",x);
}
return ;
}
最新文章
- Spring基本框架
- JQuery制作简单的网页导航特效
- 普华永道高级JAVA面试记录
- U3D4.X版本无法安装MONODEV编辑器
- 有用的dede表单代码
- python3爬虫初探(四)之文件保存
- jetty-distribution-7.6.x 部署
- unity 基础之PhysicsManager
- 【转载】NativeSQL实例
- Django 邮件推送 解决附件中文名字乱码
- PHP去除Notice警告提示
- poj 2533 Longest Ordered Subsequence(线性dp)
- 将dom4j格式化为标准的xml字符串
- 美团点评DBProxy读写分离使用说明
- 在linux中导入sql文件的方法分享(使用命令行转移mysql数据库)
- [Leetcode] Sort, Hash -- 274. H-Index
- Redis的持久化机制包括RBD和AOF两种,对于这两种持久化方式各有优势
- String 类
- 建立自己的git服务器
- R实用小技巧