CodeForces 214B Hometask
本题求n个数组成的大数,要求是2,3,5的倍数。
因为是2 和5 的倍数,所以个位为 0;所以若n个数中没有0,直接输出-1;
难点就是要求为3 的倍数。
因为若某个数为3的倍数,则其各位数的和必然是3的倍数。
当n个数的和为3的倍数时从大到小输出便可;
当n个数的和不为3的倍数时,若n个数中有模3余数与和模3余数相同时,去掉其中最小的,否则去掉两个模3余数不为0且与和模3余数不同的数中最小的。若没有这些数,输出-1;
注意当结果为0时,不能输出前导0;
ps:开始写了一个很复杂的代码,后来看过大牛们60ms的算法后又敲了一遍。。。结果还有92ms。给大牛跪了 orz ~~o(>_<)o ~~
附:(第二遍稍简洁代码)
#include <iostream>
#include <cstring>
using namespace std; int main (){
int n;
int sign[];
int num[];
int sum;
while (cin>>n){
sum=;
sign[]=sign[]=sign[]=;
memset (num,,sizeof num);
for (int i=;i<n;i++){
int x;
cin>>x;
num[x]++;
sum+=x;
sign[x%]=min (sign[x%],x);
}
if (num[]==){ //n个数中没有0;
cout<<-<<endl;
continue ;
}
if (sum%){
if (sign[sum%]==){
int temp=;
for (int i=;i<;i++){//cout<<i<<" "<<num[i]<<endl;
if (i%!=&&num[i]){
num[i]--;
temp--;
n--; //为末尾去前导零准备;
}
if (i%!=&&num[i]){
num[i]--;
temp--;
n--;
}
if (temp==)
break ;
}
if (temp){
cout<<-<<endl;
continue ;
}
}
else num[sign[sum%]]--,n--;
}
if (num[]==n){ //若结果为0,不能输出前导零;
cout<<<<endl;
continue ;
}
for (int i=;i>=;i--)
while (num[i]--)
cout<<i;
cout<<endl;
}
return ;
}
附:(第一遍复杂代码)
#include <iostream>
#include <cstring>
#include <algorithm> #define maxn 100000+10 using namespace std; int sum,ans;
int n;
int sign[];
int a[maxn]; int main (){
while (cin>>n){
sum=;
sign[]=sign[]=sign[]=;
for (int i=;i<n;i++){
cin>>a[i];
sum+=a[i];
int temp;
temp=a[i]%;
if (temp){
sign[temp]=min (sign[temp],a[i]);
}
}
sort (a,a+n);
if (a[]!=){
cout<<"-1"<<endl;
continue ;
}
if (sum%==){
int flag=;
for (int i=n-;i>=;i--){
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
continue ;
}
else if (sum%==){
if (sign[]==){
int temp=;
for (int i=;i<n;i++){
if (a[i]%==){
a[i]=;
temp--;
}
if (temp==)
break ;
}
if (temp){
cout<<"-1"<<endl;
continue ;
}
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==)
continue ;
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
}
else {
int temp=;
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==sign[]&&temp){
temp=;
continue ;
}
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
}
}
else if (sum%==){
if (sign[]==){
int temp=;
for (int i=;i<n;i++){
if (a[i]%==){
a[i]=;
temp--;
}
if (temp==)
break ;
}
if (temp){
cout<<"-1"<<endl;
continue ;
}
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==)
continue ;
if (flag==&&a[i]==)
break ;
cout<<a[i];
flag=;
}
if (flag==)
cout<<"";
cout<<endl;
}
else {
int temp=;
int flag=;
for (int i=n-;i>=;i--){
if (a[i]==sign[]&&temp){
temp=;
continue ;
}
if (flag==&&a[i]==)
break ;
flag=;
cout<<a[i];
}
if (flag==)
cout<<"";
cout<<endl;
}
}
}
return ;
}
CodeForces 214B Hometask的更多相关文章
- CF 214B Hometask(想法题)
题目链接: 传送门 Hometask Time Limit: 2 seconds Memory Limit: 256 megabytes Description Furik loves mat ...
- CodeForces 154A Hometask dp
题目链接: http://codeforces.com/problemset/problem/154/A 题意: 给你一个字符串,和若干模板串(长度为2),至少删除多少个字母,使得字符串的字串里面没有 ...
- Codeforces Round #131 (Div. 2) B. Hometask dp
题目链接: http://codeforces.com/problemset/problem/214/B Hometask time limit per test:2 secondsmemory li ...
- CodeForces - 404A(模拟题)
Valera and X Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Submit ...
- Codeforces Round #237 (Div. 2) A
链接:http://codeforces.com/contest/404/problem/A A. Valera and X time limit per test 1 second memory l ...
- Codeforces Round #260 (Div. 2)AB
http://codeforces.com/contest/456/problem/A A. Laptops time limit per test 1 second memory limit per ...
- 2010-2011 ACM-ICPC, NEERC, Moscow Subregional Contest Problem H. Hometask 水题
Problem H. Hometask 题目连接: http://codeforces.com/gym/100714 Description Kolya is still trying to pass ...
- xtu summer individual 2 C - Hometask
Hometask Time Limit: 2000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Origin ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- 50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段
50个必备的实用jQuery代码段+ 可以直接拿来用的15个jQuery代码片段 本文会给你们展示50个jquery代码片段,这些代码能够给你的javascript项目提供帮助.其中的一些代码段是从j ...
- Ultra-QuickSort(树状数组+离散化)
Ultra-QuickSort POJ 2299 Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50495 Accep ...
- win7 Oracle 11g安装及安装中遇到的问题
根据自己的系统从oracle官方下载安装包,官方地址:http://www.oracle.com/index.html win7的oracle 11g 安装包(2个): http://223.20.2 ...
- mysql索引分类及注意事项
MYSQL索引主要分为四类:主键索引,普通索引(聚合,非聚合),唯一索引,全文索引 全文索引,主要是针对对文件,文本的检索, 比如文章, 全文索引针对MyISAM有用. 索引的原理:利用二叉树(哈希表 ...
- php版权重轮询调度算法
2013-09-25 <?php class WeightedRoundRobin { private static $_weightArray = array(); private stati ...
- 手机三种SIM卡 你所不知道的剪卡“秘密”
SIM卡物理尺寸的发展是逐渐轻薄化,尺寸逐渐缩小的一个过程,最早手机中的卡都是2FF,2003年国际标准提出3FF,当前很多终端都使用这种形态的卡,4FF在2011年的国际标准会议中提出,2012年纳 ...
- logstash 处理nginx 错误日志
2016/08/30 14:52:02 [error] 11325#0: *346 open() "/var/www/zjzc-web-frontEnd/%27%22%2f%3E%3C%2f ...
- Merge Two Sorted Lists 解答
Question Merge two sorted linked lists and return it as a new list. The new list should be made by s ...
- CreateThread简单那多线程编程
CreateThread简单那多线程编程 作者:vpoet mail:vpoet_sir@163.com 在进行多任务处理的时候我们往往会用到多线程技术,多线程理论上是多个线程同事处理不同的工作,但是 ...
- 2.8 Classes of Restricted Estimators
根据所加限制的不同,可以将模型分为以下几类 RSS+Roughness penalty $PRSS(f;\lambda)=RSS(f)+\lambda J(f)$ 其中$J(f)$为对函数$f$的pe ...