Codeforces Round #451 (Div. 2)

A Rounding

题目链接:

http://codeforces.com/contest/898/problem/A

思路:

小于等于5向下,大于补上差值输出

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll n;
scanf("%I64d",&n);
int r=n%10;
if(r<=5) printf("%I64d\n",n-r);
else printf("%I64d\n",n+10-r);
return 0;
}

B Proper Nutrition

题目链接:

http://codeforces.com/contest/898/problem/B

思路:

暴力枚举

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ll a,b,n;
scanf("%I64d",&n);
scanf("%I64d %I64d",&a,&b);
bool flag=false;
ll index=n/a;
for(ll i=0;i<=index;++i) {
ll temp=n-i*a;
if(temp/b*b==temp) {
printf("YES\n");
printf("%I64d %I64d\n",i,temp/b);
flag=true;
break;
}
}
if(!flag) printf("NO\n");
return 0;
}

C Phone Numbers

题目链接:

http://codeforces.com/contest/898/problem/C

思路:

暴力大法。

就是string排序的时候重写一下cmp,另外sort之后不要unique,不然会wa5

代码:

#include <bits/stdc++.h>
using namespace std;
vector<string> vec[21];
map<int,string> mp;
set<string> se;
bool cmp(string a, string b) {
if(a.length()!=b.length()) return a.length()<b.length();
return a<b;
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,cnt,id;cin>>n;
string name;
int tot;
string number;
cnt=1;
for(int i=0;i<n;++i) {
cin>>name;
cin>>tot;
if(se.count(name)==0) {
mp[cnt]=name;
se.insert(name);
++cnt;
}
for(auto it=mp.begin();it!=mp.end();it++) {
if(it->second==name) {
id=it->first;
break;
}
}
for(int j=0;j<tot;++j) {
cin>>number;
vec[id].push_back(number);
}
}
cout<<se.size()<<endl;
for(int i=1;i<cnt;++i) {
sort(vec[i].begin(),vec[i].end(),cmp);
for(unsigned int j=0;j<vec[i].size();++j) {
string s;
s=vec[i][j];
reverse(s.begin(),s.end());
for(unsigned int z=j+1;z<vec[i].size();++z) {
string ss;
ss=vec[i][z];
reverse(ss.begin(),ss.end());
if(ss.substr(0,s.length())==s) {
vec[i].erase(vec[i].begin()+j);
--j;
break;
}
}
}
cout<<mp[i]<<" "<<vec[i].size()<<" ";
for(unsigned int j=0;j<vec[i].size();++j) {
if(j>0) cout<<" "<<vec[i][j];
else cout<<vec[i][j];
}
cout<<endl;
}
return 0;
}

D Alarm Clock

题目链接:

http://codeforces.com/contest/898/problem/D

题目大意:

不允许在m分钟内有k个闹钟响,每个闹钟响一分钟,求最少关掉的闹钟数目

思路:

首先其实按照从小到大的时间顺序查看,因为这相当于一个m大的滑块在整个数组上划一遍,所以使用队列就可以,其实就是贪心的思想。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int a[maxn];
int main() {
int n,m,k,num,res=0,tot=0;
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=n;++i) a[i]=0;
for(int i=1;i<=n;++i) scanf("%d",&num),a[num]=1;
for(int i=1;i<=1e6;++i) {
if(a[i]) ++tot;
if(i>m&&a[i-m]) --tot;
if(tot==k) {
--tot;
++res;
a[i]=0;
}
}
printf("%d\n",res);
return 0;
}

E Squares and not squares

题目链接:

http://codeforces.com/contest/898/problem/E

思路:

统计出给定序列的完全平方数和非完全平方数,看看是要补平方数还是补非完全平方数。记录下每个数距离最近的完全平方数的插值,同时记录下改为非完全平方数的最小差值。根据是前面所述的两种情况中的一种进行排序,选择出最小的数据插值求和即可。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 200005;
struct node {ll num,a,b;int d;}ans[maxn];
bool cmp1(node x, node y) {return x.a<y.a;}
bool cmp2(node x, node y) {return x.b<y.b;}
int main() {
int n;scanf("%d",&n);
int d1,d2,cnt;
ll sum=0;
d1=0;
for(int i=0;i<n;++i) {
scanf("%I64d",&ans[i].num);
ll number=(ll)sqrt(ans[i].num);
ll x=number*number;
if(x==ans[i].num) {
++d1;
ans[i].d=1;
} else ans[i].d=0;
ll y=(number+1)*(number+1);
ans[i].a=min(ans[i].num-x,y-ans[i].num);
if(max(ans[i].num-x,y-ans[i].num)>1) ans[i].b=1;
else ans[i].b=2;
}
d2=n-d1;
if(d2>d1) {
cnt=n/2-d1;
sort(ans,ans+n,cmp1);
for(int i=0;i<n;++i) {
if(ans[i].a!=0&&ans[i].d==0) {
sum+=ans[i].a;
--cnt;
if(cnt==0) break;
}
}
} else if(d1>d2) {
cnt=n/2-d2;
sort(ans,ans+n,cmp2);
for(int i=0;i<n;++i) {
if(ans[i].b!=0&&ans[i].d==1) {
sum+=ans[i].b;
--cnt;
if(cnt==0) break;
}
}
}
printf("%I64d\n",sum);
return 0;
}

Codeforces Round #451 (Div. 2) A B C D E的更多相关文章

  1. Codeforces Round #451 (Div. 2)-898A. Rounding 898B.Proper Nutrition 898C.Phone Numbers(大佬容器套容器) 898D.Alarm Clock(超时了,待补坑)(贪心的思想)

    A. Rounding time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  2. Codeforces Round #451 (Div. 2) A. Rounding【分类讨论/易错】

    A. Rounding time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  3. Codeforces Round #451 (Div. 2) [ D. Alarm Clock ] [ E. Squares and not squares ] [ F. Restoring the Expression ]

    PROBLEM D. Alarm Clock 题 OvO http://codeforces.com/contest/898/problem/D codeforces 898d 解 从前往后枚举,放进 ...

  4. Codeforces Round #451 (Div. 2)

    水题场.... 结果因为D题看错题意,B题手贱写残了...现场只出了A,C,E A:水题.. #include<bits/stdc++.h> #define fi first #defin ...

  5. Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】

    B. Proper Nutrition time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. 【Codeforces Round #451 (Div. 2) A】Rounding

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 模拟 [代码] /* 1.Shoud it use long long ? 2.Have you ever test several ...

  7. 【Codeforces Round #451 (Div. 2) B】Proper Nutrition

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 可以直接一层循环枚举. 也可以像我这样用一个数组来存y*b有哪些. 当然.感觉这样做写麻烦了.. [代码] /* 1.Shoud i ...

  8. 【Codeforces Round #451 (Div. 2) C】Phone Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 用map<string,vector > dic;模拟就好. 后缀.翻转一下就变成前缀了. 两重循环剔除这种情况不输出就 ...

  9. 【Codeforces Round #451 (Div. 2) D】Alarm Clock

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法+二分. 类似滑动窗口. 即左端点为l,右端点为r. 维护a[r]-a[l]+1总是小于等于m的就好. (大于m就右移左端点) ...

随机推荐

  1. python全局变量及局部变量

    变量作用域 全局变量(global):在函数外部定义,在整个全局范围都有效 局部变量(local) 在函数内部定义,局部变量在局部范围内使用 数字,字符串,元组,修改其变量值时需要加globle,列表 ...

  2. Python文件处理:创建、打开、追加、读、写

    在Python中,不需要导入外部库来读取和写入文件.Python为创建.写入和读取文件提供了内置的函数. 在本文中,我们将学习 如何创建文本文件 如何将数据附加到文件中 如何读取文件 如何逐行读取文件 ...

  3. PCA降维的原理、方法、以及python实现。

    PCA(主成分分析法) 1. PCA(最大化方差定义或者最小化投影误差定义)是一种无监督算法,也就是我们不需要标签也能对数据做降维,这就使得其应用范围更加广泛了.那么PCA的核心思想是什么呢? 例如D ...

  4. Elastic Stack 开源的大数据解决方案

    目的 本文主要介绍的内容有以下三点: 一. Elastic Stack是什么以及组成部分 二. Elastic Stack前景以及业务应用 三. Elasticsearch原理(索引方向) 四. El ...

  5. 【倒腾HTTPS】Nginx for Docker自签名SSL证书

    前言 合格的web程序员, 必须能自由在 IIS. Nginx. Nginx for Docker上配置Https服务, 博客最近将专题记录 Https  &   Hsts 如何申请适用于生产 ...

  6. python中程序的异常处理

    什么叫异常? 导致程序异常退出叫做异常 try...except...else 如果要抓取某种特定异常可以用except ERROR as e else:如果程序正常执行那么会执行else里面的代码 ...

  7. variable precision SWAR算法

    计算二进制形式中1的数量这种问题,在各种刷题网站上比较常见,以往都是选择最笨的遍历方法“蒙混”过关.在了解Redis的过程中接触到了variable precision SWAR算法(以下简称VP-S ...

  8. [springboot 开发单体web shop] 6. 商品分类和轮播广告展示

    商品分类&轮播广告 因最近又被困在了OSGI技术POC,更新进度有点慢,希望大家不要怪罪哦. 上节 我们实现了登录之后前端的展示,如: 接着,我们来实现左侧分类栏目的功能. ## 商品分类|P ...

  9. tornado的使用-数据库篇

    tornado的使用-数据库篇

  10. expect 自动填充密码

    它的脚本以#!/usr/bin/expect开头,执行时用expoct,而不是bash.我的一个给samba自动创建用户并且自动填写默认密码的脚本如下: vim smb_passwd.exp #!/u ...