Codeforces Round #356 (Div. 2)
2016-07-17 02:25
567 查看
680A
- Bear and Five Cards
数据很少,只有5个。情况也少。只有两种,一种是三个数相同,一种是两个数相等。把两种情况的最大和求出来,再减去即可。
#include <iostream> #include <algorithm> using namespace std; int main() { int a[10], ans = 0; for(int i = 0 ; i < 5 ; i ++) { cin >> a[i]; ans += a[i]; } sort(a, a+5); int t = 0; for(int i = 0 ; i < 4 ; i ++) if(a[i] == a[i + 1]) if(t < 2*a[i]) t = 2*a[i]; for(int i = 0 ; i < 3 ; i ++) if(a[i] == a[i+1]&&a[i+1] == a[i+2]) if(t < 3*a[i]) t = 3*a[i]; cout << ans - t << endl ; return 0; }
680B - Bear and Finding Criminals
题意:抓犯人。在一条线上,自己在某个城市,犯人分布在各个城市。自己只知道离自己距离多远的地方一共有几个犯人。需要求出自己能够确定几个地方的犯人。一共有两种情况:距离自己d的城市有一个,距离自己d的城市有两个。
第一种情况,只要有犯人就可以确定那个城市有犯人。
第二种情况,需要那两个城市才能确定那两个城市有犯人。
#include <iostream> using namespace std; int main() { int a, n, ans = 0; int t[105]; cin >> n >> a; for(int i = 1 ; i <= n ; i ++) cin >> t[i]; for(int i = 1 ; i <= n ; i ++) { if(i >= 2*a||i < 2*a - n) {if(t[i]) ans ++;} else if(t[i]&&t[2*a-i]) ans ++; } cout << ans << endl; return 0; }
679A - Bear and Prime 100
题意:系统有一个隐含的数,你要知道它是质数还是合数。你可以给不超过20个操作。每次操作你给定一个数,系统返回yes或者no。yes代表你给的数能够整除隐含的数,no同理。通过系统返回的信息作判断。最后输出“prime”或者“comprise”。你给的数和隐含的数的范围都是[2, 100]。该题是一个需要跟系统交互信息的题。但是输出是有缓冲的,取消缓冲在c++中需要用到函数fflush(stdout)。在头文件<cstdio>中。
分析:先把100以内所有的素数打出来,发现一共有20多个,所以20个操作是遍历不了所有的素数的,这说明这个题目肯定不用遍历所有的素数。而第20个素数是大于50的,100以内的合数肯定只被50以内的素数整除。所以不被50以内整除的数全是素数。最初的想法,只要被一个素数整除的还是素数,被两个素数整除的才是合数。交了一发wa了。才知道4和8只被2整除,却是合数。所以增加4,9,25,47放入素数数组中一起检测即可。
#include <iostream> #include <cstdio> #include <string> using namespace std; int a[100]; bool prime(int x){ for(int i = 1 ; a[i]*a[i] <= x ; i ++ ) if(x%a[i] == 0) { x /= a[i]; if(x%a[i] == 0&&x/a[i] == 1) return true; else return false; } return true; } int main() { fflush(stdout); int k = 1; a[k++] = 2; for(int i = 3 ; i <= 100 ; i ++ ) if(prime(i)) a[k++] = i; // for(int i = 1 ; i < k ; i ++) // cout << a[i] << endl; int t = 0, flag = 0; while(++t <= 20) { string s; cout << a[t] << endl; cin >> s; if(s == "yes") flag ++; if(flag == 2) break; } if(flag == 2) cout << "composite\n"; else cout << "prime\n"; return 0; }
679B - Bear and Tower of Cubes
光题意就看了几个小时,最后还是看别人的博客才懂了题目意思和样例。。。题意:一只北极熊要建立一座塔,塔由一个个正方体的管子组成。给出一座塔的体积m,则在体积m内能够使用尽可能多的管子,建成一个体积为x的塔。因为北极熊贪心,使用管子时必须遵守一个规则,即每次都选择能够使用的最大体积的管子,求出在这种规则下,最多能够使用多少个管子,有多种情况时,x取最大值。输出管子数和x。
分析:对体积m,有两种情况可以取得最优解。
第一种情况:把m取完,m/(n*n*n)个管子再加上剩下的m%(n*n*n)的最优解。
第二种情况:不把m取完,留下一个m%(n*n*n)+n*n*n变为n*n*n-1, 所以一共有m/(n*n*n)-1加上(n*n*n-1)的最优解。
即f(m)=max(f(m%(n*n*n)), f(n*n*n-1)-1) - m/(n*n*n);
但是这样还是会超时,考虑到f(n*n*n-1)是固定值,而且只有1e5个,可以在求值时把f(n*n*n-1)存下来。
#include <iostream> #include <cstring> #include <cmath> #include <algorithm> #define LL long long using namespace std; const LL maxn = 1e5 + 5; struct node{ LL ans, n; }q[maxn]; LL m; LL book[maxn]; node sovle(LL n){ node a, b; a.n = a.ans = 0; if(n == 0) return a; LL i = (LL)pow(n+1, 1/3.); // 这里是n+1是为了防止当n = i*i*i-1时的情况包含到上一个i里去 if(n == i*i*i-1&&book[i]) return q[i]; a = sovle(n%(i*i*i)); a.n += n/(i*i*i); a.ans += n/(i*i*i)*(i*i*i); if(!book[i]) { b = sovle(i*i*i-1); book[i] = 1; q[i] = b; } else b = q[i]; b.n += n/(i*i*i) -1; b.ans += (n/(i*i*i)-1)*(i*i*i); if(a.n != b.n) return a.n>b.n?a:b; else return a.ans>b.ans?a:b; } int main() { memset(book, 0, sizeof(book)); cin >> m; node ans = sovle(m); cout << ans.n << ' ' << ans.ans << endl; return 0; }
相关文章推荐
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #361 (Div. 2)
- 7.13Codeforces Round #360 (Div. 2)
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- CodeForces 603A_Alternative Thinking (DP)
- CodeForces 602B_Approximating a Constant Range_DP
- Codeforces round #247 for Div. 2
- Codeforces Round #246 (Div. 2)
- Codeforces #264(div 2)D.Gargari and Permutations