http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=339
都是赛后做的。。。弱爆了
A题是找由2和5组成的数字的个数
直接打个表就行了
只是比赛的时候不知道怎么打表啊。。
View Code
#include<cstdio> #include<cstring> #include<algorithm> using namespace std;int a[1000]; #define ll long long const int inf = ~0U>>1; int cnt = 0; bool judge(ll n){int t = 0;ll tmp = n;while(tmp){t++;tmp/=10;}ll r = 1;while(t--)r *= 10;if(r % n == 0)return true;else return false; } void init(){ll p = 1, q = 1;for(int i = 0; i < 31;i ++){q = 1;for(int j = 0; j < 25; j ++){if(q > inf)break;if(p * q < inf && judge(p * q))a[cnt++] = p * q;q *= 5;}p *= 2;if(p > inf)break;} } int main() {int m, n;init();while(~scanf("%d%d", &m, &n)){int ans = 0;for(int i = 0; i < cnt; i ++){if(m <= a[i] && n >= a[i]) ans ++;}printf("%d\n", ans);}return 0; }
B题更是不会
一直以为排序后就选取第一个就是了
哪知道应该是所有的都计算选取最小的。。跪了Orz
当然,这题转换成0-1背包也行,我们当时也这样做了,不过还是做错了
View Code
#include<cstdio> #include<cstring> #include<algorithm> using namespace std;int main() {int n, l;while(~scanf("%d%d", &n, &l)){int cost, weight;int ans = 999999999;while(n--){scanf("%d%d", &cost, &weight);/*int len = 0;int tmp = 0;int cnt = 0;while(len < l){tmp ++;len += cnt * weight;if(tmp % cost == 0)cnt ++;}ans = min(ans, tmp);*/int sum = 0;for(int i = cost; i < 300; i ++){sum += weight * ((i-1)/cost);if(sum >=l){ans = min(ans, i);break;}}}printf("%d\n", ans);}return 0; }
J题更是奇葩了,吐嘈不能啊。。。
数据里应该是好多输出m的,不然为什么少一句if(ans == m)return;就TLE呢 ?
真心跪到惨。。。
代码里第一种方法是参考蛋蛋哥的
搜索到第k个的时候,如果sum加上剩下的和不超过m就直接return了,好想法啊,记住了
第二种是看大神的博客的
第三种是我自己的
怎么写怎么弱,还过不了
最后几乎都一样了还是过不了
不想再改了
View Code
#include<cstdio> #include<cstring> #include<algorithm> using namespace std;int a[40]; int b[40]; int ans; int n, m; /* void dfs(int k, int sum) {if(ans == m)return;if(k == 0){ans = max(sum, ans);return;}if(a[k] + sum <= m){if(b[k] + sum <= m){ans = max(ans, b[k] + sum);return;}else{ans = max(ans, sum + a[k]);dfs(k-1, sum);dfs(k-1, sum + a[k]);}}else{ans = max(ans, sum);dfs(k-1, sum);} }int main(){while(~scanf("%d%d", &n, &m)){for(int i = 1; i <= n; i ++) scanf("%d", a + i);sort(a+1, a +1+ n);//for(int i = 1; i <= n;i ++)printf("%d\n", a[i]);int count = 1;for(int i = 1; i <= n; i ++) if(a[i] <= m) a[count++] = a[i];n = count-1;//printf("%d\n", n);//for(int i = 1; i <= n;i ++)printf("%d\n", a[i]);b[1] = a[1];for(int i = 2; i <= n; i ++) b[i] = a[i] + b[i-1];//for(int i = 1; i <= n;i ++)printf("%d\n", b[i]);ans = 0;dfs(n, 0);printf("%d\n", ans);}return 0; } */void dfs(int id, int sum) {if(ans == m)return;//为什么少这一句会T ?if(sum > ans ) ans = sum;for(int i = id; i < n; i ++){if(sum + a[i] <= m)dfs(i+1, sum + a[i]);} } int main() {while(~scanf("%d%d", &n, &m)){int s = 0;for(int i = 0; i < n; i ++){scanf("%d", a + i);s += a[i];}if(s <= m){printf("%d\n", s);continue;}//memset(vis, 0, sizeof(vis));sort(a, a + n);ans = -1;dfs(0,0);if(ans == -1)puts("0");elseprintf("%d\n", ans);}return 0; }/*wrong answer int vis[40]; void dfs(int sum) {if(ans == m)return;if(sum > ans) ans = sum;for(int i = 0; i < n; i ++){if(vis[i] == 1)continue;if(vis[i] == 0){vis[i] = 1;//sum += a[i];if(sum + a[i] <= m)//printf("%d\n", sum);dfs(sum + a[i]);//sum -= a[i];// vis[i] = 0;}} } int main() {while(~scanf("%d%d", &n, &m)){int s = 0;for(int i = 0; i < n; i ++){scanf("%d", a + i);s += a[i];}if(s <= m){printf("%d\n", s);continue;}sort(a, a + n);memset(vis, 0, sizeof(vis));ans = -1;dfs(0);if(ans == -1)puts("0");elseprintf("%d\n", ans);}return 0; } */
本来还想总结一下自己的模板的,看来是没有时间了
加油吧!