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