CCF 相反数
2015-09-11 14:38
204 查看
自己写的实现,未采用用空间换时间的方案,采用了排序的方式,效率不高,仅供参考
#include<iostream> #include<vector> #include<map> #include "math.h" using namespace std; int main() { int N; cin>>N; int i = 0; map<int,vector<int> > groups; for(;i<N;i++) { int tempt; cin>>tempt; groups[abs(tempt)].push_back(tempt); } map<int,vector<int> >::iterator iter = groups.begin(); int count = 0; for(;iter != groups.end();iter++) { //std::cout<<"first"<<iter->first<<"size"<<(iter->second).size()<<std::endl; if((iter->second).size()<=1) continue; else { int len = (iter->second).size(); int value = iter->first; int sum = value*len; vector<int>::iterator iter1 = (iter->second).begin(); int sumreal = 0; for(;iter1!= (iter->second).end();++iter1) sumreal += *iter1; //std::cout<<sum<<" df "<<sumreal<<std::endl; count += (sum - sumreal)/(2*(iter->first)); } } std::cout<<count; }
相关文章推荐
- ceph ubuntu14.10 手动安装 修正版
- 第七章 复用类(下)
- 提升APK文件安全的方法
- tomcat配置文件server.xml详解
- 用协议实现代理模式
- 问题:C#属性;结果:c# 属性
- Appium学习笔记1_获取到APK安装包的Package以及Activity属性值
- ftp部署
- Generate Parentheses C++
- Intel Edison C++ 开发之I2C-使用MRAA库进行C/C++开发
- 问题-ListView 使用的时候,已经遇到的问题;
- sizeof
- javascript JSON
- 2015 免费申请邓白氏码的步骤
- 配置torch,luarocks 在linux下
- 不常见的数据类型---结构体,指针,全局数据
- ExtJs—Grid多选框中勾选与单击事件的冲突
- 【Developer Log】开发日志
- 函数参数初探
- 一个C++程序中有多个cin输入的情况