sort函数比较cmp写法
hihocoder1566http://hihocoder.com/problemset/problem/1566
一直WA因为cmp的写法写错了,未能正确实现排序功能。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<iterator>
#include<queue>
#include<cctype>
#include<map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define IO ios::sync_with_stdio(false);cin.tie(0);
#define INF 0x3f3f3f3f
#define MAXN 100010
const int MOD=1e9+;
typedef long long ll;
using namespace std;
int t;
typedef struct {
char s1[], s2[];
int sum;
}Node;
Node node[];
map<char, int> mp;
int cal(const char s[])
{
int len = strlen(s), ans=, last=mp[s[len-]];
for(int i = len-; i >= ; i--){
if(last-mp[s[i]] > ){
last -= mp[s[i]];
}
else{
ans += last;
last = mp[s[i]];
}
}
ans += last;
return ans;
}
bool cmp(const Node a, const Node b)
{
if(strcmp(a.s1, b.s1) != ) return strcmp(a.s1, b.s1)<;
else{
return a.sum < b.sum;
}
}
int main()
{
IO;
mp['I']=;mp['V']=;mp['X']=;mp['L']=;mp['C']=;mp['D']=;mp['M']=;
cin >> t;
for(int i = ; i < t; i++){
cin >> node[i].s1 >> node[i].s2;
node[i].sum = cal(node[i].s2);
//cout << cal(node[i].s2) << endl;
}
sort(node, node+t, cmp);
for(int i = ; i < t; i++){
cout << node[i].s1 << " " << node[i].s2 << endl;
}
return ;
}
sort函数比较cmp写法的更多相关文章
- 干货:详解C++ sort函数的cmp参数!
学算法的第一天你在学冒泡.桶排 在你还没搞明白快排和归并的时候 你已经学到了数据结构最后的堆排序和希尔排序 可以说排序是很多竞赛生的噩梦-- 于是它诞生了 void std::sort() Sort ...
- C++中的sort函数和⾃定义cmp函数
写在最前面,本文摘录于柳神笔记: sort 函数在头⽂件 #include ⾥⾯,主要是对⼀个数组进⾏排序( int arr[] 数组或 者 vector 数组都⾏), vector 是容器,要⽤ v ...
- C中的qsort函数和C++中的sort函数的理解与使用
一.qsort()函数 原型:_CRTIMP void __cdecl qsort (void*, size_t, size_t,int (*)(const void*, const void*)); ...
- sort函数(cmp)、map用法---------------Tju_Oj_2312Help Me with the Game
这道题里主要学习了sort函数.sort的cmp函数写法.C++的map用法(其实和数组一样) Your task is to read a picture of a chessboard posit ...
- HDU 1425 C++使用sort函数
sort Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submissi ...
- STL sort 函数实现详解
作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...
- qsort函数、sort函数【转】
http://blog.163.com/yuhua_kui/blog/static/9679964420142195442766/ 先说明一下:qsort和sort,只能对连续内存的数据进行排序,像链 ...
- python 中的sort 和java中的Collections.sort()函数的使用
x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...
- sort函数用法
原文链接:http://blog.csdn.net/csust_acm/article/details/7326418 sort函数的用法 做ACM题的时候,排序是一种经常要用到的操作.如果每次都自己 ...
随机推荐
- weex用阿里矢量图
首先这段代码来自 zwwill在github上的 weex网易严选项目 他是在utils下封装了一个方法 let utilFunc = { initIconFont () { let domModul ...
- Json传递数据两种方式(json大全)
1.Json传递数据两种方式(json大全)----------------------------字符串 var list1 = ["number","name&quo ...
- Selenium自动化测试框架的搭建
说 起自动化测试,我想大家都会有个疑问,要不要做自动化测试? 自动化测试给我们带来的收益是否会超出在建设时所投入的成本,这个嘛别说是我,即便是高手也很难回答,自动化测试的初衷是美好的,而测 ...
- html5的audio实现高仿微信语音播放效果(实际项目)
HTML部分: <div class="tab-pane fade dialog-record" id="dialogRecord"> <vo ...
- 2018牛客网暑假ACM多校训练赛(第六场)I Team Rocket 线段树
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round6-I.html 题目传送门 - https://www.no ...
- Lock为线程上锁,防止数据混乱
用法: 先实例化 lock = threading.Lock() 1. lock.acquire() 上锁 需上锁代码 lock.release() 解锁 2. with lock: 上下两种方式都 ...
- os2
1. os.getcwd() 显示当前路径 2. a = os.name 显示当前操作系统 3. a = listdir(path) 显示该路径的所有内容,类似与ls 4. os.chdir(&quo ...
- Dataset:利用Python将已有mnist数据集通过移动像素上下左右的方法来扩大数据集为初始数据集的5倍—Jason niu
from __future__ import print_function import cPickle import gzip import os.path import random import ...
- 【python】web开发
No1: hello.py def application(environ,start_response): start_response('200 OK',[('Content-Type','tex ...
- [ 严重 ] my系统核心数据库sql注入
某网注入 注入点 : xxx.maoyan.com/xxxager.php username存在注入 POST: adminLogin=XX&username=-1&userpwd=X ...