USACO 1.2.1 Milking Cows(milk2)

來源:互聯網
上載者:User

1、按照開始時間升序排序(快排),然後從左至右掃一遍,複雜度O(nlogn+n)

2、快排是自己寫的,因為是數組排序,比較容易出問題。

/*<br />ID: gengjia1<br />LANG: C<br />TASK: milk2<br />*/<br />#include <stdio.h><br />#include <stdlib.h><br />#include <string.h><br />#include <assert.h><br />//////////////////////// 快速排序 //////////////////////////<br />int partition(int t[][2], int low, int high)<br />{<br />int pivotkey = t[low][0];<br />while (low<high)<br />{<br />while (low<high && t[high][0] >= pivotkey) --high;<br />t[low][0] = t[high][0];<br />t[low][1] = t[high][1];<br />while (low<high && t[low][0] <= pivotkey) ++low;<br />t[high][0] = t[low][0];<br />t[high][1] = t[low][1];<br />}<br />t[low][0] = pivotkey;<br />return low;<br />}<br />void qsort(int t[][2], int low, int high)<br />{<br />int pivotloc;<br />if (low < high)<br />{<br />pivotloc = partition(t, low, high);<br />qsort(t, low, pivotloc-1);<br />qsort(t, pivotloc+1, high);<br />}<br />}<br />int main(void) {<br />FILE *fin = fopen ("milk2.in", "r");<br />FILE *fout = fopen ("milk2.out", "w");<br />int N, t;<br />int max_do = 0, max_undo = 0;<br />int time[5000][2];<br />int begin, end;<br />int tmp_begin, tmp_end;<br />int i, j;<br />fscanf (fin, "%d", &N);<br />assert(N >= 1);<br />assert(N <= 5000);<br />for(i = 0; i < N; i++)<br />{<br />for(j = 0; j < 2; j++)<br />{<br />fscanf (fin, "%d", &t);<br />assert(t > 0);<br />time[i][j] = t;<br />}<br />}<br />//按開始時間升序排序<br />qsort(time, 0, N-1);<br />//計算最大值<br />tmp_begin = time[0][0];<br />tmp_end = time[0][1];<br />for(i = 1; i < N; i++)<br />{<br />begin = time[i][0];<br />end = time[i][1];<br />if(begin < tmp_end)<br />{<br />if(tmp_end < end)<br />{<br />tmp_end = end;<br />}<br />if(max_do < tmp_end - tmp_begin)<br />{<br />max_do = tmp_end - tmp_begin;<br />}<br />}<br />else<br />{<br />if(max_undo < begin - tmp_end)<br />{<br />max_undo = begin - tmp_end;<br />}<br />tmp_begin = begin;<br />tmp_end = end;<br />}<br />}<br />fprintf (fout, "%d %d/n", max_do, max_undo);<br />fclose(fin);<br />fclose(fout);<br />exit(0);<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.