201712-2放学
生活随笔
收集整理的這篇文章主要介紹了
201712-2放学
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
70分運行超時
#include <iostream> using namespace std;//結(jié)構(gòu)體,記錄每個路口狀態(tài)和所剩時間 struct status {int color;int time; }; int r, y, g; status st[100001];//函數(shù),得到下一個燈色,所剩時間為最大 status nextOf(status now) {status next = { 0,0 };switch (now.color){case 1: {//rednext.color = 3;next.time = g;break;}case 2: {//yellownext.color = 1;next.time = r;break;}case 3: {//greennext.color = 2;next.time = y;break;}default:break;}return next;}//函數(shù):需要再此燈處耗費多少時間 int cost(status now) {switch (now.color){case 1: {//redreturn now.time;}case 2: {//yellowreturn now.time + r;}case 3: {//greenreturn 0;}default:return now.time;}}//設(shè)計函數(shù),參數(shù)為當前狀態(tài),所剩時間和經(jīng)過時間,返回末狀態(tài)和所剩時間 status fun(status now, long long ltime) {status t = { 0,0 };if (now.color == 0){t.color = now.color;t.time = now.time;return t;}while (now.time < ltime){ltime -= now.time;now = nextOf(now);}t.color = now.color;t.time = now.time - ltime;/*if (now.time >= ltime){t.color = now.color;t.time = now.time - ltime;}else{t = fun(nextOf(now), ltime - now.time);}*/return t;}int main() {cin >> r >> y >> g;int n;cin >> n;for (int i = 0; i < n; i++){cin >> st[i].color >> st[i].time;}long long sum = 0;//經(jīng)過時間for (int i = 0; i < n; i++){if (st[i].color == 0){sum += st[i].time;for (int j = i + 1; j < n; j++){st[j] = fun(st[j], st[i].time);}}else{sum += cost(st[i]);for (int j = i + 1; j < n; j++){st[j] = fun(st[j], cost(st[i]));}}}cout << sum << endl;;return 0; }滿分瞅他人的,
#include <iostream> using namespace std; //結(jié)構(gòu)體,記錄每個路口狀態(tài)和所剩時間 struct status {int color;int time; }; int r, y, g; status st[100001]; //函數(shù),得到下一個燈色,所剩時間為最大 status nextOf(status now) {status next = { 0,0 };switch (now.color){case 1: {//rednext.color = 3;next.time = g;break;}case 2: {//yellownext.color = 1;next.time = r;break;}case 3: {//greennext.color = 2;next.time = y;break;}default:break;}return next;} //函數(shù):需要再此燈處耗費多少時間 int cost(status now) {switch (now.color){case 1: {//redreturn now.time;}case 2: {//yellowreturn now.time + r;}case 3: {//greenreturn 0;}default:return now.time;}} //設(shè)計函數(shù),參數(shù)為當前狀態(tài),所剩時間和經(jīng)過時間,返回末狀態(tài)和所剩時間 status fun(status now, long long ltime) {status t = { 0,0 };if (now.color == 0){t.color = now.color;t.time = now.time;return t;}while (now.time < ltime){ltime -= now.time;now = nextOf(now);}t.color = now.color;t.time = now.time - ltime;/*if (now.time >= ltime){t.color = now.color;t.time = now.time - ltime;}else{t = fun(nextOf(now), ltime - now.time);}*/return t;}int main() {cin >> r >> y >> g;int n;cin >> n;long long sum = 0;//經(jīng)過時間for (int i = 0; i < n; i++){cin >> st[i].color >> st[i].time;if (st[i].color == 0){sum += st[i].time;}else{sum+= cost(fun(st[i], sum%(r+g+y)));}}cout << sum << endl;;return 0; }就一個關(guān)鍵,將sum%(r+g+y)。迭代或者遞歸次數(shù)限制到了常數(shù)級
還有一個就是fun函數(shù)對于0即經(jīng)過一段道路的處理
上面是迭代
下面是遞歸
遞歸調(diào)用耗時少點,偶然還是必然?
轉(zhuǎn)載于:https://www.cnblogs.com/WuDie/p/11332165.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的201712-2放学的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Google 各语言网站合集
- 下一篇: Cocoa/iPhone App/静态库