赞
踩
构造长度为n的数组,满足:
所有j的aj之和可以被k整除,其中j是k的倍数,k的取值为1~n。
构造序列1->n即可满足条件。
- void solve() {
- ll n; cin >> n;
- for (int i = 1; i <= n; i++)cout << i << " ";
- cout << "\n";
- }
给定a和b两个网格,网格中只存在0、1、2三种值。
给定操作,可以选择一个矩形的一条对角线上的两个端点,让他们加1/2;另一条对角线的端点值加2/1,然后对这些值取模3。问是否存在操作可以让a和b两个网格一致。
考虑网格的边上的值,边上的值的变化会受到其他的影响,所以我们优先去找如何让边上的值相等即可。
正扫一遍,反扫一遍,然后比较数组即可。
- void solve() {
- ll n, m; cin >> n >> m;
- vector<vector<char>>a(n + 1, vector<char>(m + 1));
- vector<vector<char>>b(n + 1, vector<char>(m + 1));
- for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> a[i][j];
- for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> b[i][j];
- for (int i = 1; i < n; i++) {
- for (int j = 1; j < m; j++) {
- if (a[i][j] != b[i][j]) {
- if (a[i][j] == '0') {
- if (b[i][j] == '1') {
- a[i][j] += 1;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i + 1][j + 1] += 1;
- if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
- if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
-
- a[i + 1][j] += 2;
- if (a[i + 1][j] == '3')a[i + 1][j] = '0';
- if (a[i + 1][j] == '4')a[i + 1][j] = '1';
-
- a[i][j + 1] += 2;
- if (a[i][j + 1] == '3')a[i][j + 1] = '0';
- if (a[i][j + 1] == '4')a[i][j + 1] = '1';
- }else if(b[i][j]=='2') {
- a[i][j] += 2;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i + 1][j + 1] += 2;
- if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
- if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
-
- a[i + 1][j] += 1;
- if (a[i + 1][j] == '3')a[i + 1][j] = '0';
- if (a[i + 1][j] == '4')a[i + 1][j] = '1';
-
- a[i][j + 1] += 1;
- if (a[i][j + 1] == '3')a[i][j + 1] = '0';
- if (a[i][j + 1] == '4')a[i][j + 1] = '1';
- }
- }
- else if (a[i][j] == '1') {
- if (b[i][j] == '2') {
- a[i][j] += 1;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i + 1][j + 1] += 1;
- if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
- if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
-
- a[i + 1][j] += 2;
- if (a[i + 1][j] == '3')a[i + 1][j] = '0';
- if (a[i + 1][j] == '4')a[i + 1][j] = '1';
-
- a[i][j + 1] += 2;
- if (a[i][j + 1] == '3')a[i][j + 1] = '0';
- if (a[i][j + 1] == '4')a[i][j + 1] = '1';
- }
- else if (b[i][j] == '0') {
- a[i][j] += 2;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i + 1][j + 1] += 2;
- if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
- if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
-
- a[i + 1][j] += 1;
- if (a[i + 1][j] == '3')a[i + 1][j] = '0';
- if (a[i + 1][j] == '4')a[i + 1][j] = '1';
-
- a[i][j + 1] += 1;
- if (a[i][j + 1] == '3')a[i][j + 1] = '0';
- if (a[i][j + 1] == '4')a[i][j + 1] = '1';
- }
- }
- else {
- if (b[i][j] == '0') {
- a[i][j] += 1;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i + 1][j + 1] += 1;
- if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
- if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
-
- a[i + 1][j] += 2;
- if (a[i + 1][j] == '3')a[i + 1][j] = '0';
- if (a[i + 1][j] == '4')a[i + 1][j] = '1';
-
- a[i][j + 1] += 2;
- if (a[i][j + 1] == '3')a[i][j + 1] = '0';
- if (a[i][j + 1] == '4')a[i][j+1] = '1';
- }
- else if (b[i][j] == '1') {
- a[i][j] += 2;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i + 1][j + 1] += 2;
- if (a[i + 1][j + 1] == '3')a[i + 1][j + 1] = '0';
- if (a[i + 1][j + 1] == '4')a[i + 1][j + 1] = '1';
-
- a[i + 1][j] += 1;
- if (a[i + 1][j] == '3')a[i + 1][j] = '0';
- if (a[i + 1][j] == '4')a[i + 1][j] = '1';
-
- a[i][j + 1] += 1;
- if (a[i][j + 1] == '3')a[i][j + 1] = '0';
- if (a[i][j + 1] == '4')a[i][j + 1] = '1';
- }
- }
- }
- }
- }
- for (int i = n; i > 1; i--) {
- for (int j = m; j > 1; j--) {
- if (a[i][j] != b[i][j]) {
- if (a[i][j] == '0') {
- if (b[i][j] == '1') {
- a[i][j] += 1;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i - 1][j - 1] += 1;
- if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
- if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
-
- a[i - 1][j] += 2;
- if (a[i - 1][j] == '3')a[i - 1][j] = '0';
- if (a[i - 1][j] == '4')a[i - 1][j] = '1';
-
- a[i][j - 1] += 2;
- if (a[i][j - 1] == '3')a[i][j - 1] = '0';
- if (a[i][j - 1] == '4')a[i][j - 1] = '1';
- }
- else if (b[i][j] == '2') {
- a[i][j] += 2;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i - 1][j - 1] += 2;
- if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
- if (a[i - 1][j - 1] == '4')a[i - 1][j -1] = '1';
-
- a[i - 1][j] += 1;
- if (a[i - 1][j] == '3')a[i - 1][j] = '0';
- if (a[i - 1][j] == '4')a[i - 1][j] = '1';
-
- a[i][j - 1] += 1;
- if (a[i][j - 1] == '3')a[i][j - 1] = '0';
- if (a[i][j - 1] == '4')a[i][j - 1] = '1';
- }
- }
- else if (a[i][j] == '1') {
- if (b[i][j] == '2') {
- a[i][j] += 1;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i - 1][j - 1] += 1;
- if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
- if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
-
- a[i - 1][j] += 2;
- if (a[i - 1][j] == '3')a[i - 1][j] = '0';
- if (a[i - 1][j] == '4')a[i - 1][j] = '1';
-
- a[i][j - 1] += 2;
- if (a[i][j - 1] == '3')a[i - 1][j] = '0';
- if (a[i][j - 1] == '4')a[i - 1][j] = '1';
- }
- else if (b[i][j] == '0') {
- a[i][j] += 2;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i - 1][j - 1] += 2;
- if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
- if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
-
- a[i - 1][j] += 1;
- if (a[i - 1][j] == '3')a[i - 1][j] = '0';
- if (a[i - 1][j] == '4')a[i - 1][j] = '1';
-
- a[i][j - 1] += 1;
- if (a[i][j - 1] == '3')a[i][j - 1] = '0';
- if (a[i][j - 1] == '4')a[i][j - 1] = '1';
- }
- }
- else {
- if (b[i][j] == '0') {
- a[i][j] += 1;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i - 1][j - 1] += 1;
- if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
- if (a[i - 1][j - 1] == '4')a[i - 1][j - 1] = '1';
-
- a[i - 1][j] += 2;
- if (a[i - 1][j] == '3')a[i - 1][j] = '0';
- if (a[i - 1][j] == '4')a[i - 1][j] = '1';
-
- a[i][j - 1] += 2;
- if (a[i][j - 1] == '3')a[i][j - 1] = '0';
- if (a[i][j - 1] == '4')a[i][j-1] = '1';
- }
- else if (b[i][j] == '1') {
- a[i][j] += 2;
- if (a[i][j] == '3')a[i][j] = '0';
- if (a[i][j] == '4')a[i][j] = '1';
-
- a[i - 1][j - 1] += 2;
- if (a[i - 1][j - 1] == '3')a[i - 1][j - 1] = '0';
- if (a[i - 1][j - 1] == '4')a[i - 1][j-1] = '1';
-
- a[i - 1][j] += 1;
- if (a[i - 1][j] == '3')a[i - 1][j] = '0';
- if (a[i - 1][j] == '4')a[i - 1][j] = '1';
-
- a[i][j - 1] += 1;
- if (a[i][j - 1] == '3')a[i][j - 1] = '0';
- if (a[i][j - 1] == '4')a[i][j-1] = '1';
- }
- }
- }
- }
- }
- for (int i = 1; i <= n; i++) {
- for (int j = 1; j <= m; j++) {
- if (a[i][j] != b[i][j]) {
- cout << "NO\n"; return;
- }
- }
- }
- cout << "YES\n";
- }
三个人吃蛋糕,每个人对蛋糕不同位置有自己的估价。
问如何分配蛋糕,使三个人获得的蛋糕价值均高于蛋糕价值和的三分之一。
分类讨论:三个人拿蛋糕的顺序
abc
acb
bac
bca
cab
cba
- ll a[2100000];
- ll b[2100000];
- ll c[2100000];
- void solve() {
- ll n; cin >> n;
- ll need = 0;
- a[0] = b[0] = c[0] = 0;
- for (int i = 1; i <= n; i++)cin >> a[i], need += a[i];
- for (int i = 1; i <= n; i++)cin >> b[i];
- for (int i = 1; i <= n; i++)cin >> c[i];
- need = ceil(need / 3.0);
- for (int i = 1; i <= n; i++)a[i] += a[i - 1];
- for (int i = 1; i <= n; i++)b[i] += b[i - 1];
- for (int i = 1; i <= n; i++)c[i] += c[i - 1];
- a[n + 1] = b[n + 1] = c[n + 1] = 1e22;
- //abc
- ll id = lower_bound(a + 1, a + 1 + n, need) - a;
- ll id2 = lower_bound(b + 1, b + 1 + n, b[id] + need) - b;
- if (c[n] - c[id2] >= need) {
- cout << 1 << " " << id << " " << id + 1 << " " << id2 << " " << id2 + 1 << " " << n << "\n";
- return;
- }
- //acb
- id = lower_bound(a + 1, a + 1 + n, need) - a;
- id2 = lower_bound(c + 1, c + 1 + n, c[id] + need) - c;
- if (b[n] - b[id2] >= need) {
- cout << 1 << " " << id << " " << id2 + 1 << " " << n << " " << id + 1 << " " << id2 << "\n";
- return;
- }
- //bca
- id = lower_bound(b + 1, b + 1 + n, need) - b;
- id2 = lower_bound(c + 1, c + 1 + n, c[id] + need) - c;
- if (a[n] - a[id2] >= need) {
- cout << id2 + 1 << " " << n << " " << 1 << " " << id << " " << id + 1 << " " << id2 << "\n";
- return;
- }
- //bac
- id = lower_bound(b + 1, b + 1 + n, need) - b;
- id2 = lower_bound(a + 1, a + 1 + n, a[id] + need) - a;
- if (c[n] - c[id2] >= need) {
- cout << id + 1 << " " << id2 << " " << 1 << " " << id << " " << id2 + 1 << " " << n << "\n";
- return;
- }
- //cab
- id = lower_bound(c + 1, c + 1 + n, need) - c;
- id2 = lower_bound(a + 1, a + 1 + n, a[id] + need) - a;
- if (b[n] - b[id2] >= need) {
- cout << id + 1 << " " << id2 << " " << id2 + 1 << " " << n <<" " << 1 << " " << id << "\n";
- return;
- }
- //cba
- id = lower_bound(c + 1, c + 1 + n, need) - c;
- id2 = lower_bound(b + 1, b + 1 + n, b[id] + need) - b;
- if (a[n] - a[id2] >= need) {
- cout << id2 + 1 << " " << n << " " << id + 1 << " " << id2 << " " << 1 << " " << id << "\n";
- return;
- }
- cout << "-1\n";
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。