A - Online Shopping
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 1e4 + 10, M = N * 2;
int n, s, k;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> s >> k;
int sum = 0;
for (int i = 0; i < n; i ++)
{
int p, q; cin >> p >> q;
sum += p * q;
}
if (sum >= s) cout << sum << endl;
else cout << sum + k << endl;
return 0;
}
B - Glass and Mug
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 1e5 + 10, M = N * 2;
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int k, g, m; cin >> k >> g >> m;
int s1 = g, s2 = m;
while (k --)
{
if (s1 == g) s1 = 0;
else if (!s2) s2 = m;
else {
int c = min(s2, g - s1);
s2 -= c;
s1 += c;
}
}
cout << s1 << " " << s2 << endl;
return 0;
}
C - T-shirts
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 1e5 + 10, M = N * 2;
string s;
int n, m;
bool check(int x)
{
int t1 = x, t2 = m;
for (int i = 0; i < s.size(); i ++)
{
if (s[i] == '1') {
if (!t2) t1 --;
else t2 --;
}
else if (s[i] == '2') t1 --;
else if (s[i] == '0') t1 = x, t2 = m;
if (t1 < 0 || t2 < 0) return false;
}
return true;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> m >> s;
int l = 0, r = 1000;
while (l < r)
{
int mid = l + r >> 1;
if (check(mid)) r = mid;
else l = mid + 1;
}
cout << l << endl;
return 0;
}
D - Swapping Puzzle
枚举行的所有移动情况和列的所有移动情况,然后计算二者的逆序对即为最少的移动次数。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <vector>
#include <cmath>
#include <stack>
#include <map>
#include <queue>
#include <set>
using namespace std;
#define PII pair<int,int>
typedef long long LL;
const int N = 6, M = N * 2, INF = 0x7f7f7f7f;
int A[N][N], B[N][N];
int n, m, res = INF;
int p[N], q[N];
bool check()
{
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
if (A[p[i]][q[j]] != B[i][j]) return false;
return true;
}
int main()
{
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
cin >> A[i][j];
for (int i = 0; i < n; i ++)
for (int j = 0; j < m; j ++)
cin >> B[i][j];
for (int i = 0; i < n; i ++) p[i] = i;
for (int i = 0; i < m; i ++) q[i] = i;
int res = INF;
do{
do{
if (!check()) continue;
int cnt = 0;
for (int i = 0; i < n; i ++)
for (int j = i + 1; j < n; j ++)
if (p[i] > p[j]) cnt ++;
for (int i = 0; i < m; i ++)
for (int j = i + 1; j < m; j ++)
if (q[i] > q[j]) cnt ++;
res = min(res, cnt);
}while(next_permutation(q, q + m));
}while(next_permutation(p, p + n));
cout << (res == INF ? -1 : res) << endl;
return 0;
}