微软Azure•英雄会第五届在线编程大赛月赛第四题:Core allocation
2014-08-20 10:49
423 查看
/*===============
*
* declartion here
*
* every test case is individually.
*
=================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct query
{
char opcode;
int opnum;
char result;
char succ_mach_num; //case opcode is A, save acquired mach num
struct query *next;
};
struct data{
// int num_case; // num of test case
int num_mach; // num of machine in this test case
int num_query; // num of query in this test case
int *CiCore;
struct query *m_query;
struct data *next;
};
typedef struct box{
int num_case; //num of test case
struct data m_data;
}Bigbox;
#define LEN_INT 10
int gMalloc =0;
int gFree =0;
void *m_malloc(int size)
{
void *temp;
gMalloc += size;
temp = (void *)malloc(size);
printf("malloc %p\n",temp);
return temp;
}
void m_free(void *addr)
{
printf("free %p\n",addr);
free(addr);
}
void m_setbuf(void)
{
char ch;
while((ch = getchar()) != '\n');
}
void m_scanf_num(int *num)
{
char ch[LEN_INT] = {0};
int ch_num = 0;
*num = 0;
scanf("%d",num);
return;
while(1)
{
ch[ch_num] = getchar();
if((ch[ch_num] < '0' || ch[ch_num] > '9') && ch[ch_num] != '\n')
{
//printf("bad input, try again\n");
ch_num = 0;
*num =0;
m_setbuf();
continue;
}
if(ch[ch_num] == '\n')
{
if(ch_num == 0)
continue;
//m_setbuf();
break;
}
*num = *num*10 + ch[ch_num] - '0';
ch_num++;
if(ch_num > LEN_INT)
{
m_setbuf();
ch_num = 0;
*num = 0;
// printf("too long, try again\n");
continue;
}
}
}
void m_input(Bigbox *box)
{
int loop, loop_case;
int isshow =1;
Step1: // get num of test case
//printf("pls input the num of test case\n");
m_scanf_num(&(box->num_case));
struct data *head = &(box->m_data);
loop_case = 0;
head->n
4000
ext = NULL;
head->m_query = NULL;
head->CiCore = NULL;
Step2: // get n_mach and n_query
//printf("Case#%d:pls input the num of machine\n",loop_case);
m_scanf_num(&(head->num_mach));
//printf("Case#%d:pls input the num of query\n",loop_case);
m_scanf_num(&(head->num_query));
if(head->num_mach <=0 || head->num_query <=0)
{
// printf("illegal input args, try again %d %d\n",head->num_mach,head->num_query);
goto Step2;
}
Step3: // get machines and querys
head->CiCore = (int*)m_malloc(sizeof(int)*head->num_mach);
struct query *temp = head->m_query;
for(loop = 0; loop < head->num_mach; loop++)
{
// printf("Case#%d:input num of cores in NO.%d machine\n",loop_case,loop);
m_scanf_num(&((head->CiCore)[loop]));
}
m_setbuf();
for(loop = 0;loop < head->num_query; loop++)
{
if(isshow)
{
if(loop == 0)
{
head->m_query = (struct query*)m_malloc(sizeof(struct query));
temp = head->m_query;
temp->next = NULL;
}
else
{
temp->next = (struct query*)m_malloc(sizeof(struct query));
temp = temp->next;
temp->next = NULL;
}
}
if(isshow)
//printf("Case#%d: query_%d input the opcode\n",loop_case,loop);
isshow = 1;
#if 1
scanf("%c %d",&(temp->opcode),&(temp->opnum));
m_setbuf();
continue;
#endif
while(1)
{
scanf("%c",&(temp->opcode));
if(temp->opcode != 0x0A)
break;
}
m_setbuf();
if(temp->opcode != 'A' && temp->opcode != 'F')
{
isshow=0;
//printf("bad opcode, try again\n");
loop--;
continue;
}
//printf("Case#%d: query_%d input the opnum\n",loop_case,loop);
m_scanf_num(&(temp->opnum));
}
loop_case++;
if(loop_case < box->num_case)
{
//printf("test case%d\n",loop_case+1);
head->next = (struct data*)m_malloc(sizeof(struct data));
head = head->next;
memset(head, 0 , sizeof(struct data));
head->m_query = NULL;
head->CiCore = NULL;
head->next = NULL;
goto Step2;
}
}
void showdata(Bigbox head)
{
//printf("num of case: %d\n",head.num_case);
int temp = 1,ii;
struct data *temp_data;
struct query *temp_query;
temp_data = &(head.m_data);
while(temp <= head.num_case)
{
temp_query = temp_data->m_query;
//printf("Case#%d:\n",temp);
//printf("%d %d\n",temp_data->num_mach,temp_data->num_query);
for(ii=0;ii<temp_data->num_mach;ii++)
{
//printf("%d\n",temp_data->CiCore[ii]);
}
while(temp_query != NULL)
{
//printf("%c %d\n",temp_query->opcode,temp_query->opnum);
temp_query = temp_query->next;
}
temp_data = temp_data->next;
temp++;
}
}
void m_output(Bigbox box)
{
struct data *temp_data = &(box.m_data);
struct query *temp_query;
int **free_mach_now = (int**)m_malloc(sizeof(int**)*box.num_case);
int loop_temp;
int loop_mach;
for(loop_temp=0; loop_temp < box.num_case; loop_temp++)
{
free_mach_now[loop_temp] = (int*)m_malloc(sizeof(int*)*temp_data->num_mach*2);
for(loop_mach= 0; loop_mach < temp_data->num_mach; loop_mach++)
{
*(free_mach_now[loop_temp] + loop_mach) = temp_data->CiCore[loop_mach]; //save CiCores
*(free_mach_now[loop_temp] + loop_mach + temp_data->num_mach) = 0; //save first pos of free CiCores
}
temp_data = temp_data->next;
}
temp_data = &(box.m_data); //set head link
//reponse acquire
struct data dummy;
struct query *dummy_query ;
char case_num = 0;
char ii;
//printf("result here:\n");
while(temp_data != NULL)
{
printf("Case #%d:\n",case_num+1);
temp_query = temp_data->m_query;
dummy_query = temp_data->m_query;
for(loop_temp = 0; loop_temp < temp_data->num_query; loop_temp++)
{
switch(temp_query->opcode)
{
case 'A':
case 'a':
//m_acquire(temp_query->opnum,case_num);
for(loop_mach= 0; loop_mach < temp_data->num_mach; loop_mach++)
{
if(*(free_mach_now[case_num]+ loop_mach) >= temp_query->opnum)
{
printf("%d %d\n",loop_mach+1,*(free_mach_now[case_num]+ loop_mach + temp_data->num_mach)+1 );
*(free_mach_now[case_num]+ loop_mach + temp_data->num_mach) += temp_query->opnum; //re-locate first free cores pos.
*(free_mach_now[case_num] + loop_mach) -= temp_query->opnum; //minus some Cicores
temp_query->result = 1; //this acquire done
temp_query->succ_mach_num = loop_mach; //save the num of acquire mach
break;
}
}
if(loop_mach >= temp_data->num_mach)
{
temp_query->result = 0; //this acquire undone
printf("-1 -1\n");
}
break;
case 'F':
case 'f':
//free acquire
ii = temp_query->opnum;
while(dummy_query != NULL)
{
if(dummy_query->result == 1/*dummy_query->opcode == 'A' || dummy_query->opcode == 'a'*/)
{
if(ii -1 == 0)
{
//printf("dummy_query->result:%d\n",dummy_query->result);
if((dummy_query->opcode == 'A' || dummy_query->opcode == 'a'))
{
//printf("Case#%d free here mach_num%d\n",case_num+1, dummy_query->succ_mach_num);
*(free_mach_now[case_num]+ dummy_query->succ_mach_num + temp_data->num_mach) -= dummy_query->opnum; //re-locate first free cores pos.
*(free_mach_now[case_num] + dummy_query->succ_mach_num) += dummy_query->opnum; //minus some Cicores
dummy_query = temp_data->m_query;
temp_query->result = 1;
break;
}
}
ii--;
}
dummy_query = dummy_query->next;
}
dummy_query = temp_data->m_query;
break;
default:
break;
}
temp_query = temp_query->next;
}
case_num++;
temp_data = temp_data->next; //next test case;
}
//free malloc memory
for(loop_temp=0; loop_temp < box.num_case; loop_temp++)
{
m_free(free_mach_now[loop_temp]);
}
m_free(free_mach_now);
}
int main()
{
Bigbox datahead;
memset(&datahead,0,sizeof(Bigbox));
m_input(&datahead);
//showdata(datahead); //test whether data struct is ok or not
m_output(datahead); //really Algorithm
struct data *temp_data = &(datahead.m_data);
struct data *temp_next;
struct query *query_next;
struct query *temp_query;
int num=0;
//free malloc memory
while(temp_data != NULL)
{
temp_query = temp_data->m_query;
m_free(temp_data->CiCore);
while(temp_query != NULL)
{
query_next = temp_query->next;
m_free(temp_query);
temp_query = query_next ;
}
temp_next = temp_data->next;
if(num != 0)
m_free(temp_data);
num =1;
temp_data = temp_next;
}
return 0;
}
*
* declartion here
*
* every test case is individually.
*
=================*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct query
{
char opcode;
int opnum;
char result;
char succ_mach_num; //case opcode is A, save acquired mach num
struct query *next;
};
struct data{
// int num_case; // num of test case
int num_mach; // num of machine in this test case
int num_query; // num of query in this test case
int *CiCore;
struct query *m_query;
struct data *next;
};
typedef struct box{
int num_case; //num of test case
struct data m_data;
}Bigbox;
#define LEN_INT 10
int gMalloc =0;
int gFree =0;
void *m_malloc(int size)
{
void *temp;
gMalloc += size;
temp = (void *)malloc(size);
printf("malloc %p\n",temp);
return temp;
}
void m_free(void *addr)
{
printf("free %p\n",addr);
free(addr);
}
void m_setbuf(void)
{
char ch;
while((ch = getchar()) != '\n');
}
void m_scanf_num(int *num)
{
char ch[LEN_INT] = {0};
int ch_num = 0;
*num = 0;
scanf("%d",num);
return;
while(1)
{
ch[ch_num] = getchar();
if((ch[ch_num] < '0' || ch[ch_num] > '9') && ch[ch_num] != '\n')
{
//printf("bad input, try again\n");
ch_num = 0;
*num =0;
m_setbuf();
continue;
}
if(ch[ch_num] == '\n')
{
if(ch_num == 0)
continue;
//m_setbuf();
break;
}
*num = *num*10 + ch[ch_num] - '0';
ch_num++;
if(ch_num > LEN_INT)
{
m_setbuf();
ch_num = 0;
*num = 0;
// printf("too long, try again\n");
continue;
}
}
}
void m_input(Bigbox *box)
{
int loop, loop_case;
int isshow =1;
Step1: // get num of test case
//printf("pls input the num of test case\n");
m_scanf_num(&(box->num_case));
struct data *head = &(box->m_data);
loop_case = 0;
head->n
4000
ext = NULL;
head->m_query = NULL;
head->CiCore = NULL;
Step2: // get n_mach and n_query
//printf("Case#%d:pls input the num of machine\n",loop_case);
m_scanf_num(&(head->num_mach));
//printf("Case#%d:pls input the num of query\n",loop_case);
m_scanf_num(&(head->num_query));
if(head->num_mach <=0 || head->num_query <=0)
{
// printf("illegal input args, try again %d %d\n",head->num_mach,head->num_query);
goto Step2;
}
Step3: // get machines and querys
head->CiCore = (int*)m_malloc(sizeof(int)*head->num_mach);
struct query *temp = head->m_query;
for(loop = 0; loop < head->num_mach; loop++)
{
// printf("Case#%d:input num of cores in NO.%d machine\n",loop_case,loop);
m_scanf_num(&((head->CiCore)[loop]));
}
m_setbuf();
for(loop = 0;loop < head->num_query; loop++)
{
if(isshow)
{
if(loop == 0)
{
head->m_query = (struct query*)m_malloc(sizeof(struct query));
temp = head->m_query;
temp->next = NULL;
}
else
{
temp->next = (struct query*)m_malloc(sizeof(struct query));
temp = temp->next;
temp->next = NULL;
}
}
if(isshow)
//printf("Case#%d: query_%d input the opcode\n",loop_case,loop);
isshow = 1;
#if 1
scanf("%c %d",&(temp->opcode),&(temp->opnum));
m_setbuf();
continue;
#endif
while(1)
{
scanf("%c",&(temp->opcode));
if(temp->opcode != 0x0A)
break;
}
m_setbuf();
if(temp->opcode != 'A' && temp->opcode != 'F')
{
isshow=0;
//printf("bad opcode, try again\n");
loop--;
continue;
}
//printf("Case#%d: query_%d input the opnum\n",loop_case,loop);
m_scanf_num(&(temp->opnum));
}
loop_case++;
if(loop_case < box->num_case)
{
//printf("test case%d\n",loop_case+1);
head->next = (struct data*)m_malloc(sizeof(struct data));
head = head->next;
memset(head, 0 , sizeof(struct data));
head->m_query = NULL;
head->CiCore = NULL;
head->next = NULL;
goto Step2;
}
}
void showdata(Bigbox head)
{
//printf("num of case: %d\n",head.num_case);
int temp = 1,ii;
struct data *temp_data;
struct query *temp_query;
temp_data = &(head.m_data);
while(temp <= head.num_case)
{
temp_query = temp_data->m_query;
//printf("Case#%d:\n",temp);
//printf("%d %d\n",temp_data->num_mach,temp_data->num_query);
for(ii=0;ii<temp_data->num_mach;ii++)
{
//printf("%d\n",temp_data->CiCore[ii]);
}
while(temp_query != NULL)
{
//printf("%c %d\n",temp_query->opcode,temp_query->opnum);
temp_query = temp_query->next;
}
temp_data = temp_data->next;
temp++;
}
}
void m_output(Bigbox box)
{
struct data *temp_data = &(box.m_data);
struct query *temp_query;
int **free_mach_now = (int**)m_malloc(sizeof(int**)*box.num_case);
int loop_temp;
int loop_mach;
for(loop_temp=0; loop_temp < box.num_case; loop_temp++)
{
free_mach_now[loop_temp] = (int*)m_malloc(sizeof(int*)*temp_data->num_mach*2);
for(loop_mach= 0; loop_mach < temp_data->num_mach; loop_mach++)
{
*(free_mach_now[loop_temp] + loop_mach) = temp_data->CiCore[loop_mach]; //save CiCores
*(free_mach_now[loop_temp] + loop_mach + temp_data->num_mach) = 0; //save first pos of free CiCores
}
temp_data = temp_data->next;
}
temp_data = &(box.m_data); //set head link
//reponse acquire
struct data dummy;
struct query *dummy_query ;
char case_num = 0;
char ii;
//printf("result here:\n");
while(temp_data != NULL)
{
printf("Case #%d:\n",case_num+1);
temp_query = temp_data->m_query;
dummy_query = temp_data->m_query;
for(loop_temp = 0; loop_temp < temp_data->num_query; loop_temp++)
{
switch(temp_query->opcode)
{
case 'A':
case 'a':
//m_acquire(temp_query->opnum,case_num);
for(loop_mach= 0; loop_mach < temp_data->num_mach; loop_mach++)
{
if(*(free_mach_now[case_num]+ loop_mach) >= temp_query->opnum)
{
printf("%d %d\n",loop_mach+1,*(free_mach_now[case_num]+ loop_mach + temp_data->num_mach)+1 );
*(free_mach_now[case_num]+ loop_mach + temp_data->num_mach) += temp_query->opnum; //re-locate first free cores pos.
*(free_mach_now[case_num] + loop_mach) -= temp_query->opnum; //minus some Cicores
temp_query->result = 1; //this acquire done
temp_query->succ_mach_num = loop_mach; //save the num of acquire mach
break;
}
}
if(loop_mach >= temp_data->num_mach)
{
temp_query->result = 0; //this acquire undone
printf("-1 -1\n");
}
break;
case 'F':
case 'f':
//free acquire
ii = temp_query->opnum;
while(dummy_query != NULL)
{
if(dummy_query->result == 1/*dummy_query->opcode == 'A' || dummy_query->opcode == 'a'*/)
{
if(ii -1 == 0)
{
//printf("dummy_query->result:%d\n",dummy_query->result);
if((dummy_query->opcode == 'A' || dummy_query->opcode == 'a'))
{
//printf("Case#%d free here mach_num%d\n",case_num+1, dummy_query->succ_mach_num);
*(free_mach_now[case_num]+ dummy_query->succ_mach_num + temp_data->num_mach) -= dummy_query->opnum; //re-locate first free cores pos.
*(free_mach_now[case_num] + dummy_query->succ_mach_num) += dummy_query->opnum; //minus some Cicores
dummy_query = temp_data->m_query;
temp_query->result = 1;
break;
}
}
ii--;
}
dummy_query = dummy_query->next;
}
dummy_query = temp_data->m_query;
break;
default:
break;
}
temp_query = temp_query->next;
}
case_num++;
temp_data = temp_data->next; //next test case;
}
//free malloc memory
for(loop_temp=0; loop_temp < box.num_case; loop_temp++)
{
m_free(free_mach_now[loop_temp]);
}
m_free(free_mach_now);
}
int main()
{
Bigbox datahead;
memset(&datahead,0,sizeof(Bigbox));
m_input(&datahead);
//showdata(datahead); //test whether data struct is ok or not
m_output(datahead); //really Algorithm
struct data *temp_data = &(datahead.m_data);
struct data *temp_next;
struct query *query_next;
struct query *temp_query;
int num=0;
//free malloc memory
while(temp_data != NULL)
{
temp_query = temp_data->m_query;
m_free(temp_data->CiCore);
while(temp_query != NULL)
{
query_next = temp_query->next;
m_free(temp_query);
temp_query = query_next ;
}
temp_next = temp_data->next;
if(num != 0)
m_free(temp_data);
num =1;
temp_data = temp_next;
}
return 0;
}
相关文章推荐
- 【题解】 英雄会-第五届在线编程大赛月赛第二题:走格子
- 第五届在线编程大赛月赛第一题:完全平方
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- 第五届在线编程大赛月赛指定题目:反向互补子串
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- CSDN英雄会——第五届在线编程大赛月赛指定题目:反向互补子串
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- csdn英雄会题解之第五届在线编程大赛月赛指定题目:反向互补子串
- 第五届在线编程大赛月赛第一题:完全平方数的个数
- csdn英雄会题解之第五届在线编程大赛月赛第三题:石子游戏 --多堆博弈游戏 多堆拈游戏
- CSDN英雄会-第五届在线编程大赛月赛第三题:石子游戏(1)
- 第五届在线编程大赛月赛指定题目:反向互补子串。测试版
- 第五届在线编程大赛月赛指定题目:反向互补子串
- 第五届在线编程大赛月赛第三题:石子游戏(1)
- 庞果网英雄会第一届在线编程大赛:单词博弈
- csdn第五届在线编程大赛-完全平方
- 庞果网英雄会第一届在线编程大赛:单词博弈
- 英雄会第一届在线编程大赛:单词博弈 (C++代码) ---miss若尘