本文共 4483 字,大约阅读时间需要 14 分钟。
紫薯上的5-2和5-7的习题。
【最近三次了,题目读错,其实并不是样例的意思,然后就写题目去了】
uva12100其实并没有很难呀,就是优先极低的扔在后面,直到遇到优先极高的打印。
遇到了一些问题:
(1 )“直到” 优先极高的 最开始理解错了,错了很久。。。
(2)有关修炼
在纸上写出来真的很有用吧,对我来说
而且要分析题目,我们有什么条件,在最低限度上能得到什么,一点点写出来。
如果把过程都写好,最好清楚了,然后上手会容易很多。
虽然徒手写代码的能力会因此变差。。。
不过debug三小时也不好哇!!!!!
(3)【星号】结构体记得写构造函数。有时候有问题。
(4)会乱星人还是使用1-n吧,用的时候如果给的是0,再自行+1即可。代码在下面
前天做的,map和vector的妙用。。。
可以用来存储出现的次序.. 需要注意一下下就是set不行,因为顺序丢失了
我的写法最后检验全0的时候,要塞入等量的0进去然后看map里面是不是count
有的时候要好好用set。。 key 和value
5-7 uva 1594
#include#include #include #include #include #include #include #include
5-2 uva12100
#include#include #include #include #include #include using namespace std;typedef long long ll;struct node { int v; int id; node(int v = 0, int id = 0) : v(v), id(id) {}}nodee[105];int n, pos,cnt;int pr[11];int vis[105];int main() { int t; cin >> t; while (t--) { cin >> n >> pos; queue q; for (int i = 0; i <= 9; i++)pr[i] = 0; for (int i = 1; i <= n; i++) { cin >> nodee[i].v; nodee[i].id = i; pr[nodee[i].v]++; q.push(nodee[i]); } cnt = 0; memset(vis, 0, sizeof(vis)); while (q.size() >= 0) {//empty 可能会出问题 //cnt++; int reci=0; node reccc; while (vis[q.front().id]== 1)//&&!q.empty()) q.pop();//用过的我都拿出去了 node tmp = q.front(); for (int i = tmp.v; i <= 9; i++) {//找到最大的优先级 if (pr[i] != 0) reci = i; } // for (int i = 1; i <= 9; i++)cout << pr[i]; cout << endl; if (tmp.v == reci) {// 如果非常愉快的话 直接pop就好了 q.pop(); reccc = tmp; pr[tmp.v]--; vis[tmp.id] = 1; cnt++; } else { q.pop(); q.push(tmp); }if (reccc.id == pos + 1) { //cout << cnt << endl; break; } //}reccc是本次进行的操作 }cout << cnt << endl; } return 0;}
最后放一下错误的代码吧,一多了就很容易乱。
这个emmm如果按照我的理解,如果当前的不是最优的,就打印最优 的应该是对的
。。。脑子呢 当前的不是最优的怎么找到最优的啊。。 大部分情况下 这些东西都还是要符合生活实际的!!!!
#include#include #include #include #include #include using namespace std;typedef long long ll;struct node { int v; int id; node(int v = 0, int id = 0) : v(v), id(id) {}}nodee[105];int n, pos,cnt;int pr[11];int vis[105];int main() { int t; cin >> t; while (t--) { cin >> n >> pos; queue q; for (int i = 0; i <= 9; i++)pr[i] = 0; for (int i = 1; i <= n; i++) { cin >> nodee[i].v; nodee[i].id = i; pr[nodee[i].v]++; q.push(nodee[i]); } cnt = 0; memset(vis, 0, sizeof(vis)); while (q.size() >= 0) {//empty 可能会出问题 cnt++; int reci=0; node reccc; while (vis[q.front().id]== 1)//&&!q.empty()) q.pop();//用过的我都拿出去了 node tmp = q.front(); for (int i = tmp.v; i <= 9; i++) {//找到最大的优先级 if (pr[i] != 0) reci = i; } //for (int i = 1; i <= 9; i++)cout << pr[i]; cout << endl; if (tmp.v == reci) {// 如果非常愉快的话 直接pop就好了 q.pop(); reccc = tmp; pr[tmp.v]--; vis[tmp.id] = 1; } else { for (int i = 1; i <= n; i++) {//先找到优先级最大的是谁 if (nodee[i].v == reci && !vis[nodee[i].id]) { // recc = i; reccc = nodee[i]; vis[nodee[i].id] = 1; pr[nodee[i].v]--; q.pop(); q.push(tmp); break; } } } //if (flag == false) { if (tmp.id == pos + 1) break; } //else { if (reccc.id == pos + 1) { //cout << cnt << endl; break; } //}reccc是本次进行的操作 }cout << cnt << endl; } return 0;}
1,对于lower_bound
只有set、multiset、map、multimap能这么写,好像因为这是它们的函数 int k = *(a.lower_bound(y)); 2,set可以直接用 *a.end()和*a.begin()访问, 或者-- ++ if (y>*(--a.end())) 这里, set里面end是末尾+1的位置 想要拿到的话要-- 很多stl都是这样 数组和向量不能这么用 数组的话lower_bound(左边,右边+1,你要找的) 得到的是a[k]在内存中的位置sort(begin,end)
左臂右开的3,洛谷这个题,还是要理解一下这里,本来以为一个set足够了的
n虽然是8000 ,但是有这样的限制,所以要注意一下如果领养者累计的话,也是要等候的。