标签:long 代码 switch can eof turn 它的 题解 max
题解:看着像优化问题,实际上因为奇数的位置或者偶数的位置一定会被填满,所以暴力的对每个元素寻找它的适合位置。
感受:比赛的时候想多了,如果棋子的个数较少的话感觉会有点麻烦。。。
#pragma warning(disable:4996) #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #define lson root<<1 #define rson root<<1|1 #define mem(arr,in) memset(arr,in,sizeof(arr)) using namespace std; const int maxn = 105; int n; int a[maxn], use[maxn]; int ca_1() { mem(use, 0); for (int i = 1; i <= n; i++) if (a[i] % 2) use[a[i]] = 1; int cnt = 0; for (int i = 1; i <= n; i++) if (a[i] % 2 == 0) { for (int j = 1; j <= 2*n; j += 2) if (!use[j]) { cnt += abs(j - a[i]); use[j] = 1; break; } } return cnt; }
int ca_2() { mem(use, 0); for (int i = 1; i <= n; i++) if (a[i] % 2 == 0) use[a[i]] = 1; int cnt = 0; for (int i = 1; i <= n; i++) if (a[i] % 2) { for (int j = 2; j <= 2*n; j += 2) if (!use[j]) { cnt += abs(j - a[i]); use[j] = 1; break; } } return cnt; } int main() { while (cin >> n) { n = n / 2; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); int ans = min(ca_1(), ca_2()); cout << ans << endl; } return 0; }
题解:很直白的一道题,判断删除一个开关会影响那盏灯的状态。
感受:这题出的很晚很无奈,,,,
#pragma warning(disable:4996) #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #define lson root<<1 #define rson root<<1|1 #define mem(arr,in) memset(arr,in,sizeof(arr)) using namespace std; const int maxn = 2005; int n, m; int mp[maxn][maxn], d[maxn]; char p[maxn][maxn]; int main() { while(scanf("%d%d",&n,&m)!=EOF){ for (int i = 0; i < n; i++) scanf("%s", p[i]); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (p[i][j] == ‘1‘) mp[i][j] = 1; else mp[i][j] = 0; } } mem(d, 0); for (int j = 0; j < m; j++) { int cnt = 0; for (int i = 0; i < n; i++) { if (mp[i][j] == 1) cnt++; } d[j] = cnt; } bool flag; for (int i = 0; i < n; i++) { flag = true; for (int j = 0; j < m; j++) { if (mp[i][j] == 1) { if (d[j] <= 1) flag = false; } else { if (d[j] == 0) flag = false; } } if (flag) break; } if (flag) printf("YES\n"); else printf("NO\n"); } return 0; }
题解:我的想法是先排序得到最优的情况,判断是否满足,如果从位置pos就不满足的话,就从pos往前找后面剩下的桶(need)的价值,同时在从前往后找每个桶的价值。具体看代码。。。
感受:翻车最严重的一道题,,,同样的贪心思想,代码却写挂了,死活还找不到错误。。。注释掉的是我的代码,如果有朋友知道哪错了,麻烦告知一声,灰常感谢!
#pragma warning(disable:4996) #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define ll long long #define lson root<<1 #define rson root<<1|1 #define mem(arr,in) memset(arr,in,sizeof(arr)) using namespace std; const int maxn = 100005; int n, k, l, m; int a[maxn]; bool use[maxn]; int main() { while (scanf("%d %d %d", &n, &k, &l) != EOF) { m = n * k; for (int i = 1; i <= m; i++) scanf("%d", &a[i]); sort(a + 1, a + m + 1); if (a[n] - a[1] > l) printf("0\n"); else { ll ans = 0; int pos = m; for (int i = n; i <= m; i++) if (a[i] - a[1] > l) { pos = i - 1; break; } mem(use, 0); int cnt = 0; for (int i = 1; i <= pos; i += k) { ans += a[i]; cnt++; use[i] = 1; } if (cnt == n) printf("%I64d\n", ans); else { for (int i = pos; i >= 1; i--) if (!use[i]) { ans += a[i]; cnt++; if (cnt == n) break; } printf("%I64d\n", ans); } } /* int pos = -1; for (int i = 1; i <= m; i +=k) if (a[i] - a[1] > l) { pos = i; break; } ll ans = 0; if (pos == -1) { for (int i = 1; i <= m; i += k) ans += a[i]; printf("%I64d\n", ans); } else { int need = n - (pos - 1) / k; int cnt = 0; int x = -1; for (int i = pos; i >= 1; i--) if (a[i] - a[1] <= l) { cnt++; ans += a[i]; if (cnt == need) { x = i; break; } } for (int i = 1; i < x; i += k) { ans += a[i]; cnt++; } if (cnt != n) printf("0\n"); else printf("%I64d\n", ans); } */ } return 0; }
Educational Codeforces Round 44 (Rated for Div. 2)
标签:long 代码 switch can eof turn 它的 题解 max
原文地址:https://www.cnblogs.com/zgglj-com/p/9086099.html