A. Changing Volume
這道題都轉化爲從小到大求解。優先按大的按鈕就可以。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
void solve()
{
LL a, b;
cin >> a >> b;
if(a > b) swap(a, b);
long long ret = 0;
LL diff = b - a;
while(diff)
{
if(diff / 5)
{
ret += diff / 5;
diff %= 5;
}
if(diff / 2)
{
ret += diff / 2;
diff %= 2;
}
if(diff / 1)
{
ret += diff / 1;
diff %= 1;
}
}
cout << ret << endl;
}
int main()
{
int t;
cin >> t;
while(t--)
{
solve();
}
return 0;
}
B. Fridge Lockers
這道題改了好幾次題目。最重要的就是每個點要和另外兩個不同的點連接。所以每個點至少要連兩條邊。每個點的值都會至少加兩遍。後面題目改爲 n<=m。n<m和n<3的時候肯定沒有解。n==m的時候,只能連成一個環。這樣每個點的值都加了兩遍。
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n, m;
cin >> n >> m;
int sum = 0;
for(int i = 0; i < n; i++)
{
int x;
cin >> x;
sum += x;
}
if(n > m || n < 3)
{
cout << -1 << endl;
return;
}
cout << sum * 2 << endl;
for(int i = 1; i <= n; i++)
{
if(i == 1)
{
cout << n << ' ' << 1 << endl;
continue;
}
cout << i - 1 << ' ' << i << endl;
}
return;
}
int main()
{
int t;
cin >> t;
while(t--)
{
solve();
}
return 0;
}
C. League of Leesins
對於一個序列(1,4,2,3,5)。每三個一組分爲(1,4,2), (4,2,3), (2,3,5)。每組內部可以調換順序,組之間也可以調換順序。最後得到(4,3,2), (2,3,5),(4,1,2)。如何根據最後的數據得到最初的序列。
可以觀察到1,5出現了一次。4,3出現了兩次。1,5肯定是首尾元素。其實是無法確定哪個是開頭的。就認爲1是開頭的。(4,1,2)肯定就是前三個元素。2出現了三次,4出現了兩次。所以4肯定是第二個元素。知道了第一個元素1和第二個元素4就能推出第三個元素2。再根據4和2就能推出下一個元素3。從而可以推出整個序列。
#include<bits/stdc++.h>
using namespace std;
const int N = 1000010;
int h[N], e[N], ne[N], idx;
int x, y, z, cnt[N];
bool st[N];
void add(int a, int b)
{
e[idx] = b; ne[idx] = h[a]; h[a] = idx++;
}
int main()
{
memset(h, -1, sizeof h);
int n;
cin >> n;
for(int i = 0; i < n - 2; i++)
{
cin >> x >> y >> z;
add(x, y);
add(x, z);
add(y, x);
add(y, z);
add(z, x);
add(z, y);
cnt[x]++;
cnt[y]++;
cnt[z]++;
}
for(int i = 1; i <= n; i++)
if(cnt[i] == 1)
{
x = i;
st[x] = true;
break;
}
cout << x << ' ';
for(int i = h[x]; i != -1; i = ne[i])
{
y = e[i];
if(cnt[y] == 2)
{
st[y] = true;
break;
}
}
cout << y << ' ';
for(int i = 0; i < n - 2; i++)
{
for(int i = h[x]; i != -1; i = ne[i])
{
z = e[i];
if(!st[z]) break;
}
cout << z << ' ';
st[z] = true;
x = y;
y = z;
}
return 0;
}
D. Feeding Chicken
沒思路。過幾天看看題解再做吧。