详情请见老师发的题解,本人在此就不赘述了。
拜拜
好啦好啦,不开玩笑啦
那我们直接进入正题。
比赛传送门
总分: 惨不忍睹啊
T1: P3817 小A的糖果
源代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, k, a[N], ans = 0;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
for(int i = 2; i <= n; ++i){
if(a[i] + a[i - 1] >= k){
if(a[i] > k && a[i - 1] > k){
ans += a[i] + a[i - 1] - k;
if(a[i] > a[i - 1]){
a[i] = k;
a[i - 1] = 0;
}
else{
a[i - 1] = k;
a[i] = 0;
}
}
else if(a[i] <= k && a[i - 1] > k){
int _ = a[i] + a[i - 1] - k;
a[i - 1] -= _, ans += _;
}
else{
int _ = a[i] + a[i - 1] - k;
a[i] -= _, ans += _;
}
}
}
cout << ans;
return 0;
}一个very简单的贪心就过了,我居然没有想到。
虽然简单,但本宝宝还是要讲一下湿路的。
开一个从2~N的一个循环,每次也判断和的和是否大于等于,如果大于等于那么就判断其中有没有大于的,如果有就把它给消掉,如果没有,那么就在那里把它给消掉。
直接上代码。
AC Code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, k, a[N], ans = 0;
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> k;
for(int i = 1; i <= n; ++i){
cin >> a[i];
if(a[i] > k){
ans += a[i] - k;
a[i] = k;
}
}
for(int i = 2; i <= n; ++i){
if(a[i] + a[i - 1] > k){
int _ = a[i] + a[i - 1] - k;
ans += _;
a[i] -= _;
}
}
cout << ans;
return 0;
}T3: P4826 [USACO15FEB] Superbull S
源代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e3 + 5;
struct P{
int x, y, fen;
};
int n, a[N], cnt[N], ans;
vector<P> v;
bool cmp(P a, P b){
return a.fen > b.fen;
}
bool box(){
for(int i = 1; i <= n; ++i){
if(cnt[i] == 0){
return false;
}
}
return true;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
}
for(int i = 1; i < n; ++i){
for(int j = i + 1; j <= n; ++j){
int cnt = a[i] xor a[j];
v.push_back({i, j, cnt});
}
}
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < (n - 1) * n / 2; ++i){
if(cnt[v[i].x] == 0 || cnt[v[i].y] == 0){
ans += v[i].fen;
//cout << v[i].x << ' ' << v[i].y << '\n';
++cnt[v[i].x];
++cnt[v[i].y];
}
else{
continue;
}
}
cout << ans;
return 0;
}前面一半都是对的,只不过到了后面一半的话是我想多了,其实就是一个简简单单的最大生成树哩呀。
AC Code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e3 + 5;
struct P{
int x, y, fen;
};
int n, a[N], cnt[N], fa[N], ans, sum, Line;
vector<P> v;
bool cmp(P a, P b){
return a.fen > b.fen;
}
bool box(){
for(int i = 1; i <= n; ++i){
if(cnt[i] == 0){
return false;
}
}
return true;
}
int find(int x){
if(fa[x] == x) return fa[x];
return fa[x] = find(fa[x]);
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
fa[i] = i;
}
for(int i = 1; i < n; ++i){
for(int j = i + 1; j <= n; ++j){
int cnt = a[i] xor a[j];
v.push_back({i, j, cnt});
}
}
sort(v.begin(), v.end(), cmp);
for(int i = 0; i < (n - 1) * n / 2; ++i){
if(Line == n - 1){
cout << sum;
return 0;
}
int t1 = find(v[i].x), t2 = find(v[i].y);
if(t1 != t2){
fa[t1] = t2;
++Line;
sum += v[i].fen;
}
}
return 0;
}T4: P2058 [NOIP 2016 普及组] 海港
源代码:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
struct P{
int t, l, r;
}a[N];
int n, c[N];
set<int> b[N];
int get_zuo(int x){
int l = 1 - 1, r = n + 1;
while(l + 1 < r){
int mid = (l + r) / 2;
if(mid >= x - 86400){
r = mid;
}
else{
l = mid;
}
}
return r;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
for(int i = 1, x; i <= n; ++i){
cin >> a[i].t >> x;
for(int j = 1, _; j <= x; ++j){
cin >> _;
b[i].insert(_);
}
for(auto j = b[i - 1].begin(); j != b[i - 1].end(); ++j){
b[i].insert(*j);
}
c[i] = b[i].size();
//cout << c[i] << ' ';
}
//cout << '\n';
for(int i = 1; i <= n; ++i){
int cnt = get_zuo(a[i].t);
cout << c[i] - c[cnt - 1]/* << ' ' << get_zuo(a[i].t)*/ << '\n';
}
return 0;
}可能有童鞋看不懂我在写啥,那我就来一一解释一下吧。
get_zuo:从左边开始找到第个的位置。
b[i].insert(_)和b[i].insert(*j)把和里面的所有乘客都传到里面去。
c[i] - c[cnt - 1]其实我本来是想写一个到之间所有乘客的set的,可惜那个不会写,就只能用这个来骗一下分咯。
正确代码:
其实正确的是桶+queue。
把每一次有的乘客都给他记录到队列里面去,如果queue不为空,那么就先进行判断,如果队头他不在24个小时之内,则把队头踢出,并且同时在桶里记录,如果同一个位置里面已经为0了,则人数量减1。
AC Code:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 5;
int n, t, m, x, na[N], ans;
struct node{
int s, t;
}h;
queue<node> ship;
signed main(){
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> t >> m;
while(!ship.empty()){
h = ship.front();
if(h.t + 86400 <= t){
--na[h.s];
if(na[h.s] == 0) --ans;
ship.pop();
}
else break;
}
for(int j = 1; j <= m; ++j){
cin >> x;
h.s = x, h.t = t;
ship.push(h);
++na[x];
if(na[x] == 1) ++ans;
}
cout << ans << '\n';
return 0;
}