日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

201712-2放学

發布時間:2024/4/17 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 201712-2放学 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


70分運行超時

#include <iostream> using namespace std;//結構體,記錄每個路口狀態和所剩時間 struct status {int color;int time; }; int r, y, g; status st[100001];//函數,得到下一個燈色,所剩時間為最大 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;}//函數:需要再此燈處耗費多少時間 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;}}//設計函數,參數為當前狀態,所剩時間和經過時間,返回末狀態和所剩時間 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;//經過時間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; //結構體,記錄每個路口狀態和所剩時間 struct status {int color;int time; }; int r, y, g; status st[100001]; //函數,得到下一個燈色,所剩時間為最大 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;} //函數:需要再此燈處耗費多少時間 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;}} //設計函數,參數為當前狀態,所剩時間和經過時間,返回末狀態和所剩時間 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;//經過時間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; }

就一個關鍵,將sum%(r+g+y)。迭代或者遞歸次數限制到了常數級

還有一個就是fun函數對于0即經過一段道路的處理

上面是迭代
下面是遞歸

#include <iostream> using namespace std;//結構體,記錄每個路口狀態和所剩時間 struct status {int color;int time; };int r, y, g; status st[100001];//函數,得到下一個燈色,所剩時間為最大 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;}//函數:需要再此燈處耗費多少時間 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;}}//設計函數,參數為當前狀態,所剩時間和經過時間,返回末狀態和所剩時間 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.color == 0){t.color = now.color;t.time = now.time;return t;}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;//經過時間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; }

遞歸調用耗時少點,偶然還是必然?

轉載于:https://www.cnblogs.com/WuDie/p/11332165.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的201712-2放学的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。