心碎美团笔试,图论的题做的还是太少

第一题,看了之后就放弃了……
第二题,暴力解决,TLE,91%……

之后再看第一题,还是没有思路……
心态爆炸之后就交卷了……
凉了呀……
#美团##笔试题目#
全部评论
附上我的暴力代码 public class Main { static int[] count = new int[100000+1]; static LinkedList<Integer> queue= new LinkedList<>(); static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); int k = sc.nextInt(); int t = sc.nextInt(); int res = 0; boolean first = true; int lastNum = 0; for(int i = 0; i < k; i++) { int temp = sc.nextInt(); queue.offer(temp); count[temp]++; //System.out.println(count[temp]); if(first && count[temp] >= t) { res++; first = false; lastNum = temp; } } for(int i = k; i < n; i++) { int last = queue.poll(); count[last]--; int temp = sc.nextInt(); queue.offer(temp); count[temp]++; if(count[lastNum] >= t) { res++; continue; } if(count[temp] >= t) { res++; lastNum = temp; continue; } if(count[last] >= t) { res++; lastNum = last; continue; } Iterator<Integer> it = queue.iterator(); while(it.hasNext()) { int num = it.next(); if(count[num] >= t) { res++; lastNum = num; break; } } } System.out.println(res); } }
点赞 回复 分享
发布于 2018-09-06 21:32
第一题其实是树吧?
点赞 回复 分享
发布于 2018-09-06 21:44
我他妈前面的题做的太浪费时间,做到编程题就剩40分钟,第一提想了10分钟直接放弃,第二题代码写出来有点bug一直调,一看时间不够赶紧先交上,等调好了已经自动交卷3分钟了。 我为什么这么菜
点赞 回复 分享
发布于 2018-09-06 21:40
第一题 #include<cstdio> #include<list> using namespace std; typedef struct Vertex {     list<int> houji; }Vertex; bool fetched[100020]; Vertex v[100020]; int routine[100020]; int maxLayer = 0; int bfs(int start, int layer) {     if (layer > maxLayer)         maxLayer = layer;     fetched[start] = true;     routine[start] = 0;     for (auto it = v[start].houji.begin(); it != v[start].houji.end(); it++) {         int no = *it;         routine[start] += bfs(no, layer+1);     }     routine[start] += v[start].houji.size() * 2;     return routine[start]; } int main() {     int n;     scanf("%d", &n);     for (int i = 0; i < n-1; i++) {         int from, to;         scanf("%d %d", &from, &to);         v[from].houji.push_back(to);     }     bfs(1, 0);     printf("%d", routine[1] - maxLayer);     return 0; } 第二题 #include<cstdio> #include<unordered_map> #include<list> using namespace std; int a[100040]; unordered_map<int, int> state; unordered_map<int, int> moreThanTNum; int writeState(int *a, int idx, int k, int t) {     unordered_map<int, int> digitNum;     for (int i = idx; i < idx + k; i++) {         int digit = a[i];         if (digitNum.find(digit) == digitNum.end()) {             digitNum[digit] = 1;         }         else {             digitNum[digit]++;         }     }     moreThanTNum[idx] = 0;     for (auto it = digitNum.begin(); it != digitNum.end(); it++) {         state[it->first] = it->second;         if (it->second >= t) {             moreThanTNum[idx]++;         }     }     return 0; } int main() {     int n, k, t;     scanf("%d %d %d", &n, &k, &t);     for (int i = 0; i < n; i++) {         scanf("%d", &a[i]);     }     int result = 0;     writeState(a, 0, k, t);     if (moreThanTNum[0] >= t) {         result++;     }     for (int idx = 1; idx <= n - k; idx++) {         int deleteDigit = a[idx - 1];         int addDigit = a[idx + k - 1];         state[deleteDigit]--;         if (state[deleteDigit] == t - 1) {             moreThanTNum[idx] = moreThanTNum[idx - 1] - 1;         }         else {             moreThanTNum[idx] = moreThanTNum[idx - 1];         }         if (state.find(addDigit) == state.end()) {             state[addDigit] = 1;         }         else {             state[addDigit]++;         }         if (state[addDigit] == t) {             moreThanTNum[idx] = moreThanTNum[idx] + 1;         }         if (moreThanTNum[idx] >= 1) {             result++;         }     }     printf("%d\n", result);     return 0; } 第二题O(n)的解法一直不对,直到交卷两分钟后找出来了bug……
点赞 回复 分享
发布于 2018-09-06 21:39
第一题直接print(4)就有18%的通过率,这测试用例很感人了
点赞 回复 分享
发布于 2018-09-06 21:38
你第二题和我的不一样?
点赞 回复 分享
发布于 2018-09-06 21:36
java就是好,我用python暴力解TLE才45%,然后改成On的解法,改了半小时,最后WA,97%
点赞 回复 分享
发布于 2018-09-06 21:34
第二题有不暴力的解法嘛
点赞 回复 分享
发布于 2018-09-06 21:31
第一题20%, 改了好几次一直超时,怀疑人生
点赞 回复 分享
发布于 2018-09-06 21:29
第二道是区间统计吧?
点赞 回复 分享
发布于 2018-09-06 21:25
第二个啥意思啊,我咋没看懂呢,a| a|+1,....an是啥意思啊
点赞 回复 分享
发布于 2018-09-06 21:24
算法的第二题看来不一样
点赞 回复 分享
发布于 2018-09-06 21:24

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务