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.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
随机推荐
- linux下tar用法
以下是linux下tar的用法,转一下,以便方便自己看(这里没把rar,zip类的转过来,一般rar,zip在linux下基本没人用,基本上是zip,unzip,rar,unrar,这些命令,并且ra ...
- X-factor Chains(POJ3421 素数)
X-factor Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6212 Accepted: 1928 D ...
- python-Django环境搭建
一例中python版本使用3.5版,通常来说linux自带的python都在2.6左右,所以3.5环境要自己编译安装python 第一部分:安装python3.5 001.解决依赖问题 yum -y ...
- 工作中小知识点汇总(sql)
1.with(nolock) 在查询表的时候加上它可以增加33%查询效率 例子:SELECT TOP 5 b.UserName,f.issuedDate,r.tktedTime,r.refundSig ...
- PHP标记
1. xml风格 <?php echo 'ok'; ?> 推荐风格 2. 简短风格 <? echo 'ok'; ?> 限制条件:在配置文件中启用short_open_tag选项 ...
- 在Git中一定要关注的crlf自动转换
GitHub 第一坑:换行符自动转换 如果你已经做出了错误的选择,也不需要重新安装,可以直接使用命令行来修改设置.很简单,直接打开这货自带的命令行工具 Git Bash,输入以下命令,再敲回车即可: ...
- DM365视频处理流程/DM368 NAND Flash启动揭秘
出自http://blog.csdn.net/maopig/article/details/7029930 DM365的视频处理涉及到三个相关处理器,分别是视频采集芯片.ARM处理器和视频图像协处理器 ...
- HttpClient连接池抛出大量ConnectionPoolTimeoutException: Timeout waiting for connection异常排查
转自: http://blog.csdn.net/shootyou/article/details/6615051 今天解决了一个HttpClient的异常,汗啊,一个HttpClient使用稍有不慎 ...
- Clone Graph 解答
Question Clone an undirected graph. Each node in the graph contains a label and a list of its neighb ...
- Machine Learning - Lecture 16
Reinforcement Learning (R.L.) ① MDPs (Markov Decision Processes) ② Value Functions ③ Value Iteration ...