Educational Codeforces Round 157 (Rated for Div2)
A. Treasure Chest
思路:
当钥匙在宝箱左边时\(ans=x\);
当宝箱在钥匙左边时,若\(k>=y-x\)时,\(ans=y\),否则\(ans=2*y-x-k\)
code
int x, y, k;
void solved()
{
cin >> x >> y >> k;
if (x > y)
{
cout << x << endl;
}
else
{
cout << max(2 * y - x - k, y) << endl;
}
}
B. Points and Minimum Distance
思路:

由图可总结出要最小值,即让\(x,y\)值相对集中,即对数组排序,前\(n\)做\(x\),后\(n\)做\(y\)
code:
int n, a[N];
void solved()
{
cin >> n;
for (int i = 1; i <= 2 * n; ++i)
{
cin >> a[i];
}
sort(a + 1, a + 1 + 2 * n);
cout << a[n] + a[2 * n] - a[1] - a[n + 1] << endl;
for (int i = 1; i <= n; ++i)
{
cout << a[i] << " " << a[i + n] << endl;
}
}
C. Torn Lucky Ticket
思路:
分两种情况:一种是相同长度的连接,\(i,j\)可以互换,开数组m统计接相同值的字符串数量\(ans+=m[i]*m[i]\)
一种是不同长度的连接分别对字符串可以接的前后缀存在book数组中,\(ans+=book[i]*m[i]\)
会爆\(int\)
code:
int n;
void solved()
{
cin >> n;
int book[6][50] = {0};
int m[6][50] = {0};
for (int i = 0; i < n; ++i)
{
string s;
cin >> s;
if (s.size() == 5)
{
m[5][(s[0] - '0') + (s[1] - '0') + (s[2] - '0') + (s[3] - '0') + (s[4] - '0')]++;
int tot = (s[0] - '0') + (s[1] - '0') + (s[2] - '0') + (s[3] - '0') - (s[4] - '0');
book[3][tot]++;
book[3][(s[4] - '0') + (s[1] - '0') + (s[2] - '0') + (s[3] - '0') - (s[0] - '0')]++;
tot = (s[0] - '0') + (s[1] - '0') + (s[2] - '0') - (s[3] - '0') - (s[4] - '0');
int op = (s[3] - '0') + (s[4] - '0') + (s[2] - '0') - (s[1] - '0') - (s[0] - '0');
book[1][tot]++;
book[1][op]++;
}
else if (s.size() == 4)
{
int tot = (s[0] - '0') + (s[1] - '0') + (s[2] - '0') + (s[3] - '0');
m[4][tot]++;
tot = (s[0] - '0') + (s[1] - '0') + (s[2] - '0') - (s[3] - '0');
book[2][tot]++;
int op = (s[3] - '0') + (s[1] - '0') + (s[2] - '0') - (s[0] - '0');
book[2][op]++;
}
else if (s.size() == 3)
{
int tot = (s[0] - '0') + (s[1] - '0') + (s[2] - '0');
m[3][tot]++;
tot = (s[0] - '0') + (s[1] - '0') - (s[2] - '0');
book[1][tot]++;
int op = (s[1] - '0') + (s[2] - '0') - (s[0] - '0');
book[1][op]++;
}
else if (s.size() == 2)
{
int tot = (s[0] - '0') + (s[1] - '0');
m[2][tot]++;
}
else
{
int tot = (s[0] - '0');
m[1][tot]++;
}
}
int ans = 0;
for (int i = 1; i <= 5; ++i)
{
for (int j = 0; j < 50; ++j)
{
if (book[i][j] != 0)
ans += book[i][j] * m[i][j];
ans += m[i][j] * m[i][j];
}
}
cout << ans << endl;
}
D. XOR Construction
思路:
可根据\(b_{i}\oplus b_{i+1}=a_i\)可推出\(b_1\oplus b_i=a_1\oplus \dots \oplus a_i\)
则有\(b_1\oplus(a_1\oplus\dots\oplus a_i)=b_i\)
可以假设\(b_1\)的每一位为0,与\(a_i\)的前缀异或和异或,并与\(0\sim n-1\)异或并统计\(1\)的数目是否相等,若相等则假设正确,否则\(b_1\)的这一位为1
code:
int n, a[N];
void solved()
{
cin >> n;
for (int i = 1; i < n; ++i)
{
int tot;
cin >> tot;
a[i] = a[i - 1] ^ tot;
}
int x = 0;
for (int i = 0; i < 30; ++i)
{
int p = 0, q = 0;
for (int j = 0; j < n; ++j)
{
if (j & (1 << i))
p++;
}
for (int j = 1; j < n; ++j)
{
if (a[j] & (1 << i))
q++;
}
if (q != p)
x += (1 << i);
}
cout << x << " ";
for (int i = 1; i < n; ++i)
{
cout << (x ^ a[i]) << " ";
}
cout << endl;
}

浙公网安备 33010602011771号