Categories
不学无术

1028. List Sorting (25) | STL中自定义排序

时间限制

200 ms
内存限制
32000 kB
代码长度限制
16000 B
Excel can sort records according to any column. Now you are supposed to imitate this function.
Input
Each input file contains one test case. For each case, the first line contains two integers N (<=100000) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, each contains a record of a student. A student’s record consists of his or her distinct ID (a 6-digit number), name (a string with no more than 8 characters without space), and grade (an integer between 0 and 100, inclusive).
Output
For each test case, output the sorting result in N lines. That is, if C = 1 then the records must be sorted in increasing order according to ID’s; if C = 2 then the records must be sorted in non-decreasing order according to names; and if C = 3 then the records must be sorted in non-decreasing order according to grades. If there are several students who have the same name or grade, they must be sorted according to their ID’s in increasing order.
Sample Input 1

3 1
000007 James 85
000010 Amy 90
000001 Zoe 60

Sample Output 1

000001 Zoe 60
000007 James 85
000010 Amy 90

Sample Input 2

4 2
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 98

Sample Output 2

000010 Amy 90
000002 James 98
000007 James 85
000001 Zoe 60

Sample Input 3

4 3
000007 James 85
000010 Amy 90
000001 Zoe 60
000002 James 90

Sample Output 3

000001 Zoe 60
000007 James 85
000002 James 90
000010 Amy 90

============================================
这个题目能想到最好的办法,就是用set然后重载一个自定义排序了 感谢郭嘉,感谢STL!
set这个东西嘛,好像是用红黑树还是什么实现的(印象中是这样的) 原来打算用vector的,但是vector好像不能在插入的时候就根据自定义的排序方法排序好插进去,这样如果全部录入完了整体排序一遍是不是更耗费时间呢?
参考文章:
http://blog.csdn.net/stone_sky/article/details/8471722 讲的是自定义排序 http://zhidao.baidu.com/question/189798009.html 讲的是输出填0格式化问题 http://www.189works.com/article-43335-1.html http://wenku.baidu.com/view/08c0eb0bff00bed5b9f31d29.html 这两个就是自定义排序了
这题目这么搞了以后就简单多了:)当然最后一个测试点还是用了100ms…不管了过了就行 ============================================

#include
#include
#include
#include
using namespace std;
int compareName(const char name1[],const  char name2[])
{
    for(int i=0; i<8; i++)
    {
        if( name1[i] < name2[i] )
            return 1;
        else if(name1[i] > name2[i])
            return -1;
        //相等的话继续比较
    }
    return 0; //相同的名字
}
struct StudentRecord
{
    int ID;
    char name[9];
    int grade;
    int sortType; //按哪一列排序,1,2,3
    StudentRecord(){}
    StudentRecord(int _id, char _name[], int _grade, int _sortType)
    {
        ID = _id;
        strcpy(name, _name);
        grade = _grade;
        sortType = _sortType;
    }
    void set(int _id, char _name[], int _grade)
    {
        ID = _id;
        strcpy(name, _name);
        grade = _grade;
    }
    //自定义排序方法
    bool operator< (const StudentRecord& other) const
    {
        int cmpResult;
        switch(sortType)
        {
        case 1:
            //按照ID升序排列
            return ID < other.ID;
        case 2:
            //按照姓名的升序排列,如果有同名再按ID升序
            cmpResult = compareName(name, other.name);
            if(cmpResult > 0)
                return true;
            else if (cmpResult < 0)
                return false;
            else
                return ID < other.ID;
        case 3:
            //按照成绩的升序排列,如果有同名成绩按ID升序
            if (grade == other.grade)
                return ID < other.ID;
            else
                return grade < other.grade;
        default:
            printf("ERROR!");
            return false;
        }
    }
};
set records;
int main()
{
    long N; int C;
    scanf("%ld %d", &N, &C);
    StudentRecord singleRec;
    singleRec.sortType = C;
    for(long i=0; i<N; i++)
    {
        int ID, grade;
        char name[8];
        scanf("%d %s %d", &ID, name, &grade);
        singleRec.set(ID, name, grade);
        records.insert(singleRec);
    }
    set::iterator it;
    for(it = records.begin(); it != records.end(); it++)
    {
        StudentRecord sr = *it;
        printf("%06d %s %dn", sr.ID, sr.name, sr.grade);
    }
    return 0;
}

====================================

测试点

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 790 5/5
1 答案正确 0 740 5/5
2 答案正确 0 750 5/5
3 答案正确 0 790 2/2
4 答案正确 0 790 2/2
5 答案正确 0 740 2/2
6 答案正确 100 8970 4/4
Categories
不学无术

1009. Product of Polynomials (25)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
This time, you are supposed to find A*B where A and B are two polynomials.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, and each line contains the information of a polynomial: K N1 aN1 N2 aN2 … NK aNK, where K is the number of nonzero terms in the polynomial, Ni and aNi (i=1, 2, …, K) are the exponents and coefficients, respectively. It is given that 1 <= K <= 10, 0 <= NK < … < N2 < N1 <=1000.
 
Output Specification:
For each test case you should output the product of A and B in one line, with the same format as the input. Notice that there must be NO extra space at the end of each line. Please be accurate up to 1 decimal place.
Sample Input

2 1 2.4 0 3.2
2 2 1.5 1 0.5

Sample Output

3 3 3.6 2 6.0 1 1.6

=====================================
这题目没什么难度的说,就是多项式乘积嘛~
唯一要注意的事情是,结果的输出时按照指数递减的…这里用了map里面的reverse_iterator,真心是好用!
另外,也不知道系数有没有负数,所以处理的时候考虑了一下相加为0的情况…貌似没啥必要哦…
总之是很简单的东西~前提是有STL这货…
=====================================

#include 
#include 
using namespace std;
#define MapIterator map::iterator
#define MapIteratorReverse map::reverse_iterator
int main()
{
    int K;
    scanf("%d", &K);
    map polyA; //第一行
    for(int i=0; i p(Ni, Ki);
        polyA.insert(p);
    }
    scanf("%d", &K);
    map result; //结果
    for(int i=0; ifirst + Ni;  //乘方
            float Kj = it->second * Ki;//系数
            if(result.count(Nj) > 0)
            {
                //原来有结果
                float sum = Kj + result[Nj];
                if(sum == 0)
                    //这项没了...
                    result.erase(Nj);
                else
                    result[Nj] = sum;
            }
            else
            {
                result.insert(pair(Nj, Kj));
            }
        }
    }
    printf("%d", result.size());
    MapIteratorReverse it;
    for(it = result.rbegin(); it != result.rend(); it++)
    {
        printf(" %d %.1f", it->first, it->second);
    }
    return 0;
}
Categories
不学无术

1058. A+B in Hogwarts (20)

If you are a fan of Harry Potter, you would know the world of magic has its own currency system — as Hagrid explained it to Harry, “Seventeen silver Sickles to a Galleon and twenty-nine Knuts to a Sickle, it’s easy enough.” Your job is to write a program to compute A+B where A and B are given in the standard form of “Galleon.Sickle.Knut” (Galleon is an integer in [0, 107], Sickle is an integer in [0, 17), and Knut is an integer in [0, 29)).
Input Specification:
Each input file contains one test case which occupies a line with A and B in the standard form, separated by one space.
Output Specification:
For each test case you should output the sum of A and B in one line, with the same format as the input.
Sample Input:

3.2.1 10.16.27

Sample Output:

14.1.28

===========================
很简单的问题,就不多写了~代码也很粗糙,嘿嘿

#include 
using namespace std;
int main()
{
	long galleon[2] = {0,0};
	int sickle[2] = {0,0}, knut[2] = {0,0};
	scanf("%ld.%d.%d", &galleon, &sickle, &knut);
	scanf("%ld.%d.%d", &galleon[1], &sickle[1], &knut[1]);
	long G = 0;
	int  S=0, K=0;
	K = knut[0] + knut[1];
	while(K > 28)
	{
		K -= 29;
		S ++;
	}
	S += sickle[0];
	S += sickle[1];
	while(S > 16)
	{
		S -= 17;
		G ++;
	}
	G += galleon[0];
	G += galleon[1];
	printf("%ld.%d.%d",G, S, K);
	return 0;
}
Categories
不学无术

1032. Sharing (25)

时间限制

100 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, “loading” and “being” are stored as showed in Figure 1.

Figure 1
You are supposed to find the starting position of the common suffix (e.g. the position of “i” in Figure 1).
Input Specification:
Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (<= 105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is the letter contained by this node which is an English letter chosen from {a-z, A-Z}, andNext is the position of the next node.
Output Specification:
For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output “-1” instead.
Sample Input 1:

11111 22222 9
67890 i 00002
00010 a 12345
00003 g -1
12345 D 67890
00002 n 00003
22222 B 23456
11111 L 00001
23456 e 67890
00001 o 00010

Sample Output 1:

67890

Sample Input 2:

00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1

Sample Output 2:

-1

 

====================================
没啥技术含量的题,记得早些年数据结构期中考考过,当时觉得好难
其实如果标记下走过哪里的话就好简单
没啥好说的,注意输出格式,比如1要变成00001
===================================

#include 
#include 
#include  // std::setfill, std::setw
using namespace std;
#define END_OF_NODE -1
#define MAX_NODE_QTY 100000
struct Node
{
	char letter;
	int nextPtr;
	bool visited;
	Node()
	{
		nextPtr = END_OF_NODE;
		visited = false;
	}
};
Node nodes[MAX_NODE_QTY];
int main()
{
	int root_A_ptr, root_B_ptr;
	int N;
	scanf("%d %d %d", &root_A_ptr, &root_B_ptr, &N);
	//Read node infomation
	for(int i=0; i

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 1650 10/10
1 答案正确 0 1770 1/1
2 答案正确 0 1900 8/8
3 答案正确 0 1630 1/1
4 答案正确 0 1780 2/2
5 答案正确 30 1900 3/3

Categories
不学无术

1007. Maximum Subsequence Sum (25)

时间限制

400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defined to be { Ni, Ni+1, …, Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.
Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.
Input Specification:
Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.
Output Specification:
For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.
Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

==============================================
这是一个经典问题,可以利用《编程珠玑》第8章<算法设计技术>里面的扫描算法
算法不写了,书上一模一样的原题,伪代码如下:

maxsofar = 0
maxendinghere = 0
for i = [0,n)
    /* invariant: maxendinghere and maxsofar
       are accurate for x[0..i-1] */
    maxendinghere = max(maxendinghere + x[i], 0)
    maxsofar = max(maxendinghere, maxsofar)

只不过这里输出的时候要输出首末位置的数字,所以加了一些变量。
什么时候会改变首末位置呢:
1. maxEndingHere == 0的时候,表示如果后面还会有更好的结果的话,肯定开始的位置是从这个点之后的一个数字开始的。此时要记录可能会产生gap,留给后续处理
2.当到达vStartIndex,别忘了记下<可能的>新的起点
3.当后面那一坨真的比原来的值还大时,如果存在gap,那要用新的起点替换掉旧的,然后吧gap开关关上,一个新的开始
4.当算出来总数maxEndingHere比maxSofar大时,别忘了记下当前位置的数值作为末尾数值
另外要注意,如果全是负值怎么处理,特别注意这样的输入,不能以maxSofar是否>0判断

3
-1 0 -1

代码中startIndex与endIndex是不必要存储的。
=============================================

#include 
#include  //max
using namespace std;
int main()
{
	int N;
	scanf("%d", &N);
	long maxSofar = 0;
	long maxEndingHere = 0;
	int startVal = 0;
	int startIndex = -1;  //
	int vStartIndex = 0;//
	int vStartVal = 0;
	int endIndex = -1;//
	int endVal = 0;
	bool hasGap = true;
	bool allNegative = true;
	//int *savedVals = new int[N];
	int totalStartVal;
	int totalEndVal;
	for(int i=0; i= 0 && allNegative)
			allNegative = false;
		if(i==0)
			totalStartVal = inputVal;
		else if(i == N-1)
			totalEndVal = inputVal;
		//savedVals[i] = inputVal;
		maxEndingHere = max( maxEndingHere + inputVal, (long)0);
		if(maxEndingHere == 0)
		{
			hasGap = true;
			vStartIndex = i+1;
		}
		if(i == vStartIndex)
			vStartVal = inputVal;
		if( maxEndingHere > maxSofar)
		{
			maxSofar = maxEndingHere;
			endIndex = i;
			endVal = inputVal;
			if(hasGap)
			{
				startIndex = vStartIndex;
				startVal = vStartVal;
				hasGap = false;
			}
		}
	}
	if(!allNegative)
		printf("%ld %d %d", maxSofar, startVal, endVal);
	else
		printf("%ld %d %d", maxSofar, totalStartVal, totalEndVal);
	return 0;
}

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 750 2/2
1 答案正确 0 790 1/1
2 答案正确 0 790 3/3
3 答案正确 0 790 4/4
4 答案正确 0 790 4/4
5 答案正确 0 790 3/3
6 答案正确 0 790 3/3
7 答案正确 0 750 5/5

Categories
不学无术

1027. Colors in Mars (20)

1027. Colors in Mars (20)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.
Input
Each input file contains one test case which occupies a line containing the three decimal color values.
 
Output
For each test case you should output the Mars RGB value in the following format: first output “#”, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a “0” to the left.
Sample Input

15 43 71

Sample Output

#123456

 

===============================
没啥技术含量。
===============================

#include 
#include 
using namespace std;
string GetRGBVal(int val)
{
	char ret[3];
	ret[2] = '';
	int count = 1;
	while( count >= 0)
	{
		if(val == 0)
			ret[count] = '0';
		int temp = val % 13;
		if(temp >9)
		{
			temp -= 10;
			temp += 0x41;
		}
		else
			temp += 0x30;
		val /= 13;
		ret[count--] = (char)temp;
	}
	return string(ret);
}
int main()
{
	int rgb[3];
	cin >> rgb[0] >> rgb[1] >> rgb[2];
	cout << "#";
	for(int i=0; i<3; i++)
	{
		cout << GetRGBVal(rgb[i]);
	}
	return 0;
}
Categories
不学无术

1039. Course List for Student (25)

1039. Course List for Student (25)

时间限制
200 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
Zhejiang University has 40000 students and provides 2500 courses. Now given the student name lists of all the courses, you are supposed to output the registered course list for each student who comes for a query.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers: N (<=40000), the number of students who look for their course lists, and K (<=2500), the total number of courses. Then the student name lists are given for the courses (numbered from 1 to K) in the following format: for each course i, first the course index i and the number of registered students Ni (<= 200) are given in a line. Then in the next line, Ni student names are given. A student name consists of 3 capital English letters plus a one-digit number. Finally the last line contains the N names of students who come for a query. All the names and numbers in a line are separated by a space.
Output Specification:
For each test case, print your results in N lines. Each line corresponds to one student, in the following format: first print the student’s name, then the total number of registered courses of that student, and finally the indices of the courses in increasing order. The query results must be printed in the same order as input. All the data in a line must be separated by a space, with no extra space at the end of the line.
Sample Input:

11 5
4 7
BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
1 4
ANN0 BOB5 JAY9 LOR6
2 7
ANN0 BOB5 FRA8 JAY9 JOE4 KAT3 LOR6
3 1
BOB5
5 9
AMY7 ANN0 BOB5 DON2 FRA8 JAY9 KAT3 LOR6 ZOE1
ZOE1 ANN0 BOB5 JOE4 JAY9 FRA8 DON2 AMY7 KAT3 LOR6 NON9

Sample Output:

ZOE1 2 4 5
ANN0 3 1 2 5
BOB5 5 1 2 3 4 5
JOE4 1 2
JAY9 4 1 2 4 5
FRA8 3 2 4 5
DON2 2 4 5
AMY7 1 5
KAT3 3 2 4 5
LOR6 4 1 2 4 5
NON9 0

=================================
PAT上通过率0.09的题目看着挺可怕
主要是时间的问题我想,做那么多次查询~
第一想法是直接用STL的map给名字做索引
这样的话搜索效率是O(log2n),因为map是用神马树实现的
不过…后来想想这个题目那么多跪了的,估计肯定不能用这个
只能拿空间换时间了~
权衡了一下,因为姓名是3个字母加1个数字,所以可以自己写个浪费空间的Hash函数,将名字的字母部分映射成数组的索引
哈希了一下嘛,时间复杂度就是O(1)了,邪恶一笑…
数组使用map>实现,显然前面那个int就是给字母最后的数字使用的,后面那个存的course的ID
这样可能稍微能节约点空间,不然26*26*26*10需要175760那么多地方…浪费地有点多
其他没有什么可以说的,里头有现成的sort,专门对付STL容器里头的排序,自己写个compare函数就行了,相当方便。
提交一次通过,虽然最长时间是180ms有点危险,不过不想再去折腾这个了。
==================================

#include 
#include 
#include 
#include 
using namespace std;
map< int, vector> stu_cou_map[17576];
inline int GetHashIndex(char name[])
{
	//给出一个名字比如BOB5,得到对应 stu_cou_map的索引号
	int index = 676 * (name[0] - 0x41) + 26 * (name[1] - 0x41) + (name[2] - 0x41);
	return index;
}
inline bool mySortFunc (int i, int j)
{
	return (i s;
				s.push_back(course_id);
				stu_cou_map[index].insert( pair>(suffix, s));
			}
			else
			{
				stu_cou_map[index][suffix].push_back(course_id);
			}
		}
	}
	for(int i=0; i s = stu_cou_map[index][suffix];
		printf(" %d", s.size());
		vector::iterator begin = s.begin();
		vector::iterator end = s.end();
		sort(begin, end, mySortFunc);
		for(vector::iterator it = begin; it!=end; it++)
		{
			printf(" %d", *it);
		}
		printf("n");
	}
	return 0;
}

==========================================
测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 1250 15/15
1 答案正确 0 1380 2/2
2 答案正确 0 1380 2/2
3 答案正确 0 1380 2/2
4 答案正确 0 1380 2/2
5 答案正确 180 21700 2/2

Categories
不学无术

1051. Pop Sequence (25)

Given a stack which can keep M numbers at most. Push N numbers in the order of 1, 2, 3, …, N and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if M is 5 and N is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): M (the maximum capacity of the stack), N (the length of push sequence), and K (the number of pop sequences to be checked). Then K lines follow, each contains a pop sequence of N numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line “YES” if it is indeed a possible pop sequence of the stack, or “NO” if not.
Sample Input:

5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2

Sample Output:

YES
NO
NO
YES
NO

=================================
数据结构是个好东西。
输入的序列1,2,3,…,N其实是(废话本来就是)一个队列(下面记作seq),然后在弄一个容易被填满的栈…
以下栈底、队首用#号标识
然后看测试的序列,比如5 6 4 3 7 2 1,
1.先看栈顶元素,如果栈顶元素就是输入的元素的话,那就弹栈,然后继续循环
2.如果不是,那么就要将队列的头一直取出,填到栈里面,
直到:(A)栈满了,那么失败了
(B)现在栈顶的元素就是我们的输入元素,那么继续读下一个测试的数字
测试序列5 6 4 3 7 2 1中,一开始的元素是5,于是不断的从1234567这个队列中取头部出来,压栈,然后
栈S : #1 2 3 4 5
seq: # 6 7
满足调节后,下一个循环查看的时候,发现栈顶的5刚好是我们的输入元素,于是序列指针移到6上面,并且弹出5,
经过差不多的过程,变成这样
栈S : #1 2 3 4 6
seq : #7
之后这样
栈S : #1 2 3 4
seq : #7
,
栈S : #1 2 3
seq : #7
,
栈S : #1 2
seq : #7
,
栈S : #1 2 7
seq : #
,
栈S : #1 2
seq : #
,
。。。直到尽头,然后成功了
=======================================================
具体实现可能跟刚才的说法略有不一致,但是原理是一样的。

#include 
#include 
#include 
using namespace std;
int main()
{
    int M, N, K;
    queue sequences;
    queue copy_of_seq; //为了防止重复生成浪费时间
    scanf("%d %d %d", &M, &N, &K);
    //生成seq队列
    for(int i=1; i s;
        bool failed = false;
        int input_val;
        for(int i=0; i= M) //要留一个给后面再读入的相等的元素
                {
                    failed = true;
                    break;
                }
            }
            if( failed )
            {
                printf("NOn");
            }
            else if( copy_of_seq.empty() )
            {
                failed = true;
                printf("NOn");
            }
            else
            {
                //成功了..
                //s.push( copy_of_seq.front() ); (反正还要pop,不搞了)
                copy_of_seq.pop();
            }
            //如果头部还是与数字不同,那就fail
        }
        if (!failed)
            printf("YESn");
    }
    return 0;
}

测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 740 15/15
1 答案正确 0 750 3/3
2 答案正确 0 790 2/2
3 答案正确 0 790 2/2
4 答案正确 0 750 1/1
5 答案正确 0 780 2/2

Categories
不学无术

1029. Median (25)

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
Given two increasing sequences of integers, you are asked to find their median.
Input
Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (<=1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.
Output
For each test case you should output the median of the two given sequences in a line.
Sample Input

4 11 12 13 14
5 9 10 15 16 17

Sample Output

13

====================================
这题其实没什么技术含量,因为输入序列都帮你排序好了,时间内存都没啥限制
要注意的一点就是,假定输入序列为A,B,当A或B某个已经被掏空时,需要另外处理,不然就会报段错误什么的…
在优化方面,其实可以先读第一个序列进队列,第二个序列读入时直接处理而不存储,能比原来节省一半空间。
由于题目比较松,没有做这方面处理。
====================================

#include 
#include 
using namespace std;
int main()
{
	///////////Init
	queue q_a ,q_b;
	long N1, N2; //两个序列的长度
	//不存储第二个序列,直接用来比较处理
	///////////
	//读第一个序列
	scanf("%ld", &N1);
	long temp;
	for(long i=0; i

另外测试结果比较恐怖,哈哈
测试点 结果 用时(ms) 内存(kB) 得分/满分
0 答案正确 0 660 4/4
1 答案正确 0 790 2/2
10 答案正确 320 17270 3/3
11 答案正确 320 17260 1/1
12 答案正确 320 17260 3/3
13 答案正确 0 740 1/1
2 答案正确 0 740 4/4
3 答案正确 0 750 1/1
4 答案正确 0 790 1/1
5 答案正确 0 750 1/1
6 答案正确 150 9000 1/1
7 答案正确 160 9000 1/1
8 答案正确 160 9010 1/1
9 答案正确 150 8870 1/1

Categories
不学无术

1008. Elevator (20)

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.
For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.
Output Specification:
For each test case, print the total time on a single line.
Sample Input:

3 2 3 1

Sample Output:

41

==============================================
此题初看还以为是电梯算法,其实啥都不是。。。。
注意的是第一个输入的数字是后面的总的输入个数,不是别的其他什么…
答案也很短…好久没碰到这么简单的了
==============================================

#include 
using namespace std;
int main(int argc, char* argv[])
{
	int N;
	cin >> N;
	int floor_now = 0; //当前在0层
	int next_floor = 0;
	int total_time = 0;
	for(int i=0; i> next_floor;
		int time_per_floor = (next_floor - floor_now > 0) ? 6 : -4; //上楼还是下楼
		total_time = total_time + time_per_floor * (next_floor - floor_now) + 5;
		floor_now = next_floor;
	}
	cout << total_time;
	return 0;
}