Archive for July, 2008

POJ2418【二叉搜索树】

POJ2418【二叉搜索树】

题目描述:
Hardwood Species

Time Limit: 10000MS Memory Limit: 65536K
Total Submissions: 3576 Accepted: 1421

Description
Hardwoodsare the botanical group of trees that have broad leaves, produce afruit or nut, and generally go dormant in the winter.
America’s temperate climates produce forests with hundreds ofhardwood species — trees that share certain biologicalcharacteristics. Although oak, maple and cherry all are types ofhardwood trees, for example, they are different species. Together, allthe hardwood species represent 40 percent of the trees in the UnitedStates.

On the other hand, softwoods, or conifers, from the Latin wordmeaning “cone-bearing,” have needles. Widely available US softwoodsinclude cedar, fir, hemlock, pine, redwood, spruce and cypress. In ahome, the softwoods are used primarily as structural lumber such as2x4s and 2x6s, with some limited decorative applications.

Using satellite imaging technology, the Department of NaturalResources has compiled an inventory of every tree standing on aparticular day. You are to compute the total fraction of the treepopulation represented by each species.
Input
Inputto your program consists of a list of the species of every treeobserved by the satellite; one tree per line. No species name exceeds30 characters. There are no more than 10,000 species and no more than1,000,000 trees.
Output
Printthe name of each species represented in the population, in alphabeticalorder, followed by the percentage of the population it represents, to 4decimal places.
Sample Input
Red Alder
Ash
Aspen
Basswood
Ash
Beech
Yellow Birch
Ash
Cherry
Cottonwood
Ash
Cypress
Red Elm
Gum
Hackberry
White Oak
Hickory
Pecan
Hard Maple
White Oak
Soft Maple
Red Oak
Red Oak
White Oak
Poplan
Sassafras
Sycamore
Black Walnut
Willow
Sample Output
Ash 13.7931
Aspen 3.4483
Basswood 3.4483
Beech 3.4483
Black Walnut 3.4483
Cherry 3.4483
Cottonwood 3.4483
Cypress 3.4483
Gum 3.4483
Hackberry 3.4483
Hard Maple 3.4483
Hickory 3.4483
Pecan 3.4483
Poplan 3.4483
Red Alder 3.4483
Red Elm 3.4483
Red Oak 6.8966
Sassafras 3.4483
Soft Maple 3.4483
Sycamore 3.4483
White Oak 10.3448
Willow 3.4483
Yellow Birch 3.4483
Hint
This problem has huge input, use scanf instead of cin to avoid time limit exceeded.
题目大意:
有一大堆树,卫星上看到他们的种类,输入数据给出的;让你统计每一个种类的出现次数,按照字典序输出种类名称和频率;
数据范围:
树种类名字小于30字符;种类个数小于1000;树的个数小于1000000;
边界数据;
无;
可能陷进
知道不能直接统计就好;
解题思路:
1000000估计硬统计肯定难过;所以就找一种方法吧,让查找尽量的快;本题用二叉搜索树;建树时通过比较字符串的值;最后再输出;
AC代码:
#include “stdio.h”
#include “stdlib.h”
#include “string.h”
int totalnum = 1;
typedef struct _bst
{
double count;
char name[31];
struct _bst *left;
struct _bst *right;
}BST;
BST *head = NULL;
void insert(char *tmp)
{
int t;
BST *p1, *p2;
BST *_node;
p1 = head;
while(p1 != NULL)
{
p2 = p1;
t = strcmp(tmp, p1->name);
if (t == 0)
{
p1->count++;
return;
}
else if (t > 0)
{
p1 = p1->right;
continue;
}
else
{
p1 = p1->left;
continue;
}
}
_node = (BST *)malloc(sizeof(BST));
_node->count = 1;
_node->left = NULL;
_node->right = NULL;
strcpy(_node->name, tmp);
if (t > 0)
{
p2->right = _node;
}
else
{
p2->left = _node;
}
}
void output(BST *phead)
{
if (phead != NULL)
{
output(phead->left);
printf(“%s %.4f\n”, phead->name, (phead->count*100)/totalnum);
output(phead->right);
}
}
int main()
{

//freopen(“e:\\in.txt”, “r”, stdin);
char temp[31];
totalnum = 1;
head = (BST *)malloc(sizeof(BST));
head->count = 1;
head->left = NULL;
head->right = NULL;
gets(head->name);
while(gets(temp))
{
if (temp[0] == ‘\0′) break;
totalnum++;
insert(temp);
}
output(head);
return 0;
}
【可优化】

POJ2524【并查集】

POJ2524【并查集】

题目描述:

Ubiquitous Religions

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5586 Accepted: 2346

Description
Thereare so many different religions in the world today that it is difficultto keep track of them all. You are interested in finding out how manydifferent religions students in your university believe in.

You know that there are n students in your university (0 < n<= 50000). It is infeasible for you to ask every student theirreligious beliefs. Furthermore, many students are not comfortableexpressing their beliefs. One way to avoid these problems is to ask m(0 <= m <= n(n-1)/2) pairs of students and ask them whether theybelieve in the same religion (e.g. they may know if they both attendthe same church). From this data, you may not know what each personbelieves in, but you can get an idea of the upper bound of how manydifferent religions can be possibly represented on campus. You mayassume that each student subscribes to at most one religion.
Input
Theinput consists of a number of cases. Each case starts with a linespecifying the integers n and m. The next m lines each consists of twointegers i and j, specifying that students i and j believe in the samereligion. The students are numbered 1 to n. The end of input isspecified by a line in which n = m = 0.
Output
Foreach test case, print on a single line the case number (starting with1) followed by the maximum number of different religions that thestudents in the university believe in.
Sample Input
10 9
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
10 4
2 3
4 5
4 8
5 8
0 0
Sample Output
Case 1: 1
Case 2: 7
Hint
Huge input, scanf is recommended.
题目大意:
给出几个人两两之间的信仰相同;要求给出最多有多少个不同的信仰集合;
数据范围:
学生数0—50000;
边界数据:
几乎没有;
可能陷进:
几乎没有;
AC代码:
#include “stdio.h”
#include “iostream”
using namespace std;
int root[100001];
int heav[100001];
void makeset(int i)
{
root = i;
heav = 1;
}
int findset(int i)
{
if (root != i)
root = findset(root);
return root;
}
void unio(int i, int j)
{
if (heav > heav[j])
{
root[j] = i;
heav = heav + heav[j];
}
else if (heav < heav[j])
{
root = j;
heav[j] = heav + heav[j];
}
else
{
root[j] = i;
heav = heav + heav[j];
}
}
int main()
{
//freopen(“e:\\in.txt”, “r”, stdin);
int count, n;
int p, q;
int t, k;
int testcase = 0;
while(scanf(“%d%d”, &count, &n) != EOF)
{
if (count == 0 && n == 0) break;
testcase++;
for (int i = 1; i <= count; ++i)
makeset(i);
while(n–)
{
scanf(“%d%d”, &p,&q);
t = findset(p);
k = findset(q);
if (t != k)
{
count–;
unio(t, k);
}

}
printf(“Case %d: %d\n”,testcase, count);
}
return 0;
}

POJ2051【最小优先级队列】【堆实现】

题目描述:
Argus
Time Limit: 1000MS                 Memory Limit: 30000K
Total Submissions: 4468                 Accepted: 1916

Description
A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor data, Internet traffic, financial tickers, on-line auctions, and transaction logs such as Web usage logs and telephone call records. Likewise, queries over streams run continuously over a period of time and incrementally return new results as new data arrives. For example, a temperature detection system of a factory warehouse may run queries like the following.

Query-1: “Every five minutes, retrieve the maximum temperature over the past five minutes.”
Query-2: “Return the average temperature measured on each floor over the past 10 minutes.”

We have developed a Data Stream Management System called Argus, which processes the queries over the data streams. Users can register queries to the Argus. Argus will keep the queries running over the changing data and return the results to the corresponding user with the desired frequency.

For the Argus, we use the following instruction to register a query:

Register Q_num Period

Q_num (0 < Q_num <= 3000) is query ID-number, and Period (0 < Period <= 3000) is the interval between two consecutive returns of the result. After Period seconds of register, the result will be returned for the first time, and after that, the result will be returned every Period seconds.

Here we have several different queries registered in Argus at once. It is confirmed that all the queries have different Q_num. Your task is to tell the first K queries to return the results. If two or more queries are to return the results at the same time, they will return the results one by one in the ascending order of Q_num.

Input
The first part of the input are the register instructions to Argus, one instruction per line. You can assume the number of the instructions will not exceed 1000, and all these instructions are executed at the same time. This part is ended with a line of “#”.

The second part is your task. This part contains only one line, which is one positive integer K (<= 10000).

Output
You should output the Q_num of the first K queries to return the results, one number per line.

Sample Input

Register 2004 200
Register 2005 300
#
5

Sample Output

2004
2005
2004
2004
2005

题目大意:

给出任务的id(各个任务唯一)和执行间隔(各个任务不唯一);要求按照执行的时间顺序来输出要求的钱几个任务id号;当两个任务在同一个时间执行时,先输出id小的;

解题思路:

显然是要求按照执行的时间先后顺序来输出结果;首先考虑排序,发现排序的话,根本不能确定每个任务执行多少次能满足题目要求,所以肯定是不可行的;既 然要求按照时间的先后顺序来输出,那么可以构造优先队列,每次取队头,然后再根据取出任务的执行情况将下一次的执行时间插入到队列里;直到取出满足要求的 个数任务为止。

数据范围:

任务的id号:0—3000;任务时间间隔:0—3000;要求的输出个数:<10000;任务个数: <1000;

边界数据:

基本没有;

ac代码;

#include “stdio.h”
#include “iostream”
using namespace std;
typedef struct _node
{
int id;
int key;
}Node;
Node team[10001] = {0};
int mul[3001];
int totalwant = 0;
int heapsize1 = 0;
//many keywords to sort using qsort~~~
int compare(void const *a, void const *b)
{
if (((Node *)a)->key == ((Node *)b)->key) return 0;
return ((Node *)a)->key > ((Node *)b)->key?1:-1;
}
void increase(int i)
{
int parent = 0;
Node tmp;
parent = i/2;
if ((parent >= 1)&&((team.key < team[parent].key)||(team.key == team[parent].key&&team.id < team[parent].id)))
{
tmp.id = team.id;
tmp.key = team.key;
///
team.id = team[parent].id;
team.key = team[parent].key;
///
team[parent].id = tmp.id;
team[parent].key = tmp.key;
increase(parent);
}
}
void intoheap(int idx, int keyx)
{
heapsize1++;
team[0].key++;
team[heapsize1].id = idx;
team[heapsize1].key = keyx;
increase(heapsize1);
}
void adjust(int i)
{
int l, r, most;
Node tmp;
l = i*2;
r = l + 1;
if ((l <= team[0].key)&&((team[l].key < team.key)||(team[l].key == team.key&&team[l].id < team.id)))
{
most = l;
}
else
{
most = i;
}
if ((r <= team[0].key)&&((team[r].key < team[most].key)||(team[r].key == team[most].key&&team[r].id < team[most].id)))
{
most = r;
}
if (most != i)
{
tmp.id = team[most].id;
tmp.key = team[most].key;
///
team[most].id = team.id;
team[most].key = team.key;
///
team.id = tmp.id;
team.key = tmp.key;
adjust(most);
}
}
void makeheap()
{
for (int i = heapsize1/2; i >= 1; –i)
{
adjust(i);
}
}
Node gettop()
{
Node t;
printf(“%d\n”, team[1].id);
t.id = team[1].id;
t.key = team[1].key;
team[1].id = team[heapsize1].id;
team[1].key = team[heapsize1].key;
heapsize1–;
team[0].key–;
adjust(1);
return t;
}
int main()
{
freopen(“e:\\in.txt”, “r”, stdin);
char temp[10];
int id, key;
int index = 0;
Node getid;
int kk;
for (int i = 0; i < 3001; ++i)
mul = 1;
while(scanf(“%s”, temp))
{
if (temp[0] == ‘#’) break;
index++;
scanf(“%d%d”, &team[index].id, &team[index].key);
}
team[0].key = index;
heapsize1 = index;
scanf(“%d”, &totalwant);
//input the heap members~~~~ and ~~~~~how many they want to output~~~~
//qsort(team, (size_t)index, sizeof(Node), compare);
makeheap();
while(totalwant–)
{
getid = gettop();
kk = mul[getid.id];
mul[getid.id]++;
intoheap(getid.id, (getid.key/kk)*mul[getid.id]);
}
return 0;
}
【可优化】

Incoming search terms for the article:

NENU_SOFT ACM BBS

NENU_SOFT ACM BBS开张了。。。

以后,我们会把我们的解题报告,尤其是暑期集训时选做题的解题报告贴上去,希望大家有空能多去看看,也希望能指点指点。。。

如果你喜欢这个BBS,请告诉你的同学,如果你不喜欢这个BBS,也请你告诉你的同学。。。