一開始我這么做超時(shí)了戴已,但是今天早上看了一下標(biāo)準(zhǔn)答案罗侯,感覺跟我的做法時(shí)間差不了多少啊,而且規(guī)定的時(shí)間是200ms音比,不可能超時(shí)啊妖滔,然后就檢查了一下隧哮,很驚喜的發(fā)現(xiàn),我的方法居然過了座舍。
好開心啊
這個(gè)方法就是用X和Y換向來填充沮翔,但是速度肯定是比不上標(biāo)準(zhǔn)答案的那個(gè)啦,標(biāo)準(zhǔn)答案的那個(gè)簸州,一看就知道是很快的方法
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int input[100010];
bool cmp(int a, int b) { return a > b; }
int directionX[4] = { 1,0,-1,0 };
int directionY[4] = { 0,-1,0,1 };
int matrix[10000][10000];
int main() {
int n; scanf("%d", &n);
int rows, cols, half = ceil(sqrt(n)) ;
for (int i = half; i <= n; i++)
if (n % i == 0) {
rows = i;
cols = n / i;
break;
}
for (int i = 0; i < n; i++) scanf("%d", &input[i]);
sort(input, input + n, cmp);
for (int i = 1; i <= cols; i++)matrix[1][i] = input[i - 1];
int index = 0, index_X = 1, index_Y = cols , substract = 0;//用來指示方向,code用來表示第幾輪了
for (int i = cols ; i < n;) {
if (index % 2 == 0)substract++;
int end;
if (directionX[index]) end = rows - substract;
else end = cols - substract;
for (int j = 0; j < end; j++) {
index_X += directionX[index];
index_Y += directionY[index];
matrix[index_X][index_Y] = input[i++];
}
index = (index + 1) % 4;
}
for (int i = 1; i <= rows; i++) {
printf("%d", matrix[i][1]);
for (int j = 2; j <= cols; j++)
printf(" %d", matrix[i][j]);
printf("\n");
}
}
A1017 優(yōu)先隊(duì)列實(shí)現(xiàn)
#include<cstdio>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
using namespace std;
int main() {
priority_queue<pair<float, float>, vector<pair<float, float> >, greater<pair<float, float> > > customers;
priority_queue<float,vector<float>,greater<float> > windows;
int customers_num, windows_num,customers_sum = 0; scanf("%d %d", &customers_num, &windows_num);
float close_time = 17 * 60;
for (int i = 0; i < customers_num; i++) {
float HH, SS, MM, proccesse; scanf("%f:%f:%f %f", &HH, &MM, &SS, &proccesse);
MM += SS / 60 + HH * 60;
if (proccesse > 60) proccesse = 60;
if (MM <= close_time) { customers.push(pair<float, float>(MM, proccesse)); customers_sum++; }
}
float waitTimeSum = 0, open_time = 8 * 60;
for (int i = 0; i < windows_num; i++)windows.push(open_time);
while (customers.size() && windows.size()) {
pair<float, float> now_customer = customers.top(); customers.pop();
if (windows.top() > now_customer.first){waitTimeSum += windows.top() - now_customer.first;windows.push(windows.top() + now_customer.second);}
else windows.push(now_customer.first + now_customer.second);
windows.pop();
}
printf("%.1f\n", waitTimeSum / (float)customers_sum);
}
用隊(duì)列實(shí)現(xiàn)鉴竭,第一次做的時(shí)候溢出了
使用const int INF = 0x3fffffff 一定要注意它會(huì)不會(huì)有好幾個(gè)數(shù)字相加
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int INF = 0x3fffffff;
int finish_time[1010], last_one[22];//用來記錄窗口的上一個(gè)處理的對(duì)象
int main(){
queue<int> window[22];
int windows_num, line_capacity,customers_num,queries,close_time = 17 * 60;
scanf("%d %d %d %d",&windows_num,&line_capacity,&customers_num,&queries);
for(int i = 0;i < windows_num;i++) last_one[i] = 480;
int first_deal = min(customers_num,line_capacity * windows_num), index = 0;//先處理黃線中的顧客
for(int i = 1;i <= first_deal;i++){
int processe;scanf("%d",&processe);
if(last_one[index] >= close_time) {last_one[index] = INF;finish_time[i] = INF;}
else {processe += last_one[index];last_one[index] = processe;finish_time[i] = processe;}
window[index++].push(i);
if(index == windows_num) index = 0;//用條件判斷來代替除法
}
for(int i = first_deal + 1 ;i <= customers_num;i++){
int processe;scanf("%d",&processe);
int minindex = 0,min_num = INF;
for(int j = 0;j < windows_num;j++)
if(finish_time[window[j].front()] < min_num){//隊(duì)首元素最早的結(jié)束的歧譬,那么這個(gè)就會(huì)空出來岸浑,下一個(gè)元素就能進(jìn)來
min_num = finish_time[window[j].front()];
minindex = j;
}
window[minindex].pop();
if(last_one[minindex] >= close_time) {last_one[minindex] = INF;finish_time[i] = INF;}
else {processe += last_one[minindex];last_one[minindex] = processe;finish_time[i] = processe;}
window[minindex].push(i);
}
for(int i = 0;i < queries;i++){
int q;scanf("%d",&q);
if(finish_time[q] == INF)printf("Sorry\n");
else printf("%02d:%02d\n",finish_time[q] / 60,finish_time[q] % 60);
}
}