题目链接:https://codeforces.com/gym/102894
#include
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7
const long long inf = 0x3f3f3f3f;
#define endl '\ans'
int a[100005];
int main()
{
int n, x, y;
ll ansi = 0, ansm = 0;
cin >> n >> x >> y;
for (int i = 0; i < n; i++)
{
cin >> a[i];
if (a[i] + x > y || (a[i] - x > y && a[i] - x >= 0))
{
ansm++;
}
if (a[i] + x > y && (a[i] - x > y && a[i] - x >= 0))
{
ansi++;
}
}
cout << ansi << ' ' << ansm;
return 0;
}
模拟一下就行)
可以先算出不用通用礼物所需时间
先看x,y有没有比z大的,有的话就需要把差值减去
然后看是否还剩下通用礼物
#include
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f;
#define endl '\n'
int a[100005];
int main()
{
ll n, m, x, y, z, k, flag = 0;
cin >> n >> m >> x >> y >> z >> k;
ll ans = n * x + m * y;
int s = max(x - z, y - z);
if (s > 0)
{
if (s == x - z)
{
ans -= min(n, k) * s;
k -= min(n, k);
flag = 1;
}
else
{
ans -= min(m, k) * s;
k -= min(m, k);
flag = 2;
}
if (k > 0)
{
if (flag == 1)
{
if (y > z)
{
ans -= (y - z) * min(k, m);
}
}
else if (flag == 2)
{
if (x > z)
{
ans -= (x - z) * min(k, n);
}
}
}
}
cout << ans;
return 0;
}
画几个图就能看出一个多边形经过切割后最少多出两条边 最多四条边
#include
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f;
#define endl '\n'
int a[100005];
int main()
{
ll n, a, b;
cin >> n >> a >> b;
if (a + b >= n + 2 && a + b <= n + 4)
{
cout << "YES";
}
else
{
cout << "NO";
}
return 0;
}
等差数列求和
#include
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f;
#define endl '\n'
int a[100005];
ll cal(ll n)
{
return n * (1 + n) / 2;
}
int main()
{
ll t, n, ans;
cin >> t;
while (t--)
{
cin >> n;
ll l = 1, r = 1000000000;
while (l <= r)
{
ll mid = (l + r) >> 1;
if (n <= cal(mid))
{
r = mid - 1;
ans = mid;
}
else
{
l = mid + 1;
}
}
if (cal(ans) == n)
ans++;
cout << cal(ans) - n << endl;
}
return 0;
}
贪心+二分
#include
using namespace std;
typedef long long ll;
const long long mod = 1e9 + 7;
const long long inf = 0x3f3f3f3f;
#define endl '\n'
vector<int> room;
struct lei
{
int p, m;
} num[200005];
bool cmp(lei a, lei b)
{
return a.m > b.m;
}
int main()
{
ll n, k, x;
ll ans = 0;
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
scanf("%d", &x);
room.push_back(x);
}
for (int i = 1; i <= k; i++)
{
scanf("%d", &num[i].p);
}
for (int i = 1; i <= k; i++)
{
scanf("%d", &num[i].m);
}
int pos;
sort(room.begin(), room.end());
sort(num + 1, num + 1 + k, cmp);
for (int i = 1; i <= k; i++)
{
pos = lower_bound(room.begin(), room.end(), num[i].p) - room.begin();
if (room[pos] >= num[i].p && pos >= 0 && pos < room.size())
{
ans += num[i].m;
room.erase(room.begin() + pos);
}
}
cout << ans;
return 0;
}