题意:
给定n
使得序列 p [1,2,3····n] pi / i 的值最小
思路:
让相邻两数互质
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 200010;
ll n, k, res;
ll a[N];
void solve()
{
cin >> n;
for (int i = 1; i <= n; i ++)
a[i] = i;
for (int i = 1; i <= n-1; i += 2)
{
a[i]=i+1;
a[i+1]=i;
}
if (n==1)
{
cout << 1 << endl;
return;
}
if (n % 2==1)
swap(a[1], a[n]);
for (int i = 1; i <= n; i ++)
printf("%ld ", a[i]);
printf("\\n");
}
int main()
{
int t;
cin >> t;
while (t --)
{
solve();
}
system("pause");
return 0;
}
题意:
n个不高兴值ai
m对朋友
蛋糕一次做两个=>蛋糕数必须得是偶数(唯一限制条件)
=>朋友对数只能是偶数
思路:
-
m是偶数 就能全邀请 即不高兴值为0
-
m是奇数 不邀请一个有奇数关系的人 不邀请一对朋友 使得 最后总对数为偶数
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 100010;
ll n, m, res;
ll a[N], b[N];
PII p[N];
void solve()
{
memset(b, 0, sizeof(b));
res = 1e9;
cin >> n >> m;
for (int i = 1; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= m; i ++) cin >> p[i].first >> p[i].second;
if (m % 2 == 0)
{
cout << 0 << endl;
return;
}
for (int i = 1; i <= m; i ++)
{
b[p[i].first] ++;
b[p[i].second] ++;
}
for (int i = 1; i <= n; i ++)
if (b[i] % 2 == 1)
res = min(res, a[i]);
for (int i = 1; i <= m; i ++)
res = min(res, a[p[i].first] + a[p[i].second]);
cout << res << endl;
return;
}
int main()
{
int t;
cin >> t;
while (t --)
{
solve();
}
system("pause");
return 0;
}
题意:
给出一个矩形
行n 列m 颜色总数k 以及每种颜色使用次数ai .... ak
思路:
全部按行(列)染色 看看有没有机会染色成功
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 100010;
LL n, m, k;
LL a[N], b[N];
PII p[N];
void solve()
{
cin >> n >> m >> k;
for (int i = 1; i <= k; i ++) cin >> a[i];
sort(a + 1,a + 1+k);
LL cols = 0, rows = 0;
for (int i = 1; i <= k; i ++)
{
if (a[i] / n > 1) cols += a[i] / n;
if (m - cols == 1) cols -= 2; // 若只剩一列 则要少染两列使得下次有机会全填满
if (a[i] / m > 1) rows += a[i] / m;
if (n - rows == 1) rows -= 2;
}
cout<< ((cols >= m || rows >= n) ? "Yes\\n" : "No\\n");
}
int main()
{
int t;
cin >> t;
while (t --)
{
solve();
}
system("pause");
return 0;
}