标签:void amp ref single tin precision run 覆盖 合并
直接乱搞即可。
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/16 22:36:20
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
int n;
int a[N], b[N], c[N];
void run(){
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++) {
cin >> b[i];
c[i] = b[i] - a[i];
}
int cnt = 0;
for(int i = 1; i <= n; i++) {
if(c[i] < 0) return pt("NO");
}
int l = 1, r = n;
while(l <= r && c[l] == 0) ++l;
while(l <= r && c[r] == 0) --r;
int ok = 1;
for(int i = l + 1; i <= r; i++) if(c[i] != c[i - 1]) ok = 0;
if(l > r || ok) return pt("YES");
else return pt("NO");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T;
while(T--) run();
return 0;
}
贪心分组即可。
用一个\(map\)记录当前组一个人是否已经来过。
至于为什么用\(map\),方便清零= =
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/16 22:50:46
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e6 + 5;
int n;
int a[N];
map <int, int> mp, mp2;
void run(){
for(int i = 1; i <= n; i++) cin >> a[i];
int tot = 0, last = 0;
vector <int> ans;
for(int i = 1; i <= n; i++) {
if(a[i] > 0) {
if(mp.find(a[i]) == mp.end() && mp2.find(a[i]) == mp2.end()) {
mp[a[i]] = 1;
++tot;
} else {
cout << -1;
return ;
}
} else {
if(mp.find(-a[i]) != mp.end()) {
mp.erase(-a[i]);
--tot;
mp2[-a[i]] = 1;
} else {
cout << -1;
return;
}
}
if(tot == 0 && sz(mp) == 0) {
ans.push_back(i - last);
mp2.clear();
last = i;
}
}
if(tot || sz(mp) != 0) {
cout << -1;
return ;
}
cout << sz(ans) << '\n';
for(auto it : ans) cout << it << ' ';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n) run();
return 0;
}
顺序没关系,排序后贪心分组即可。
公式推一推就行。
难点是读题。
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/16 23:12:15
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;
int n, m;
int a[N];
ll ans[N], sum[N];
void run(){
for(int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + a[i];
if(i <= m) ans[i] = sum[i];
}
for(int i = m + 1; i <= n; i++) {
ans[i] = sum[i] + ans[i - m];
}
for(int i = 1; i <= n; i++) cout << ans[i] << " \n"[i == n];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> m) run();
return 0;
}
题意:
给出一个\(n\)个结点,\(m\)条边的无向图。
然后如果一个图\(l\)能到达\(r\),那么对于所有的\(m,l<m<r\),都有\(m\)能够到达\(r\)。
现在问这个图中最少添加多少条边,使得这个图满足上述情况。
思路:
那么最终就相当于一个区间合并的问题,注意一下单独的点也可以形成一个区间。
细节详见代码:
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/16 23:21:00
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 2e5 + 5;
int n, m;
vector<int> g[N];
bool chk[N], vis[N];
int mx, mn;
void dfs(int u) {
vis[u] = 1;
mx = max(mx, u);
mn = min(mn, u);
for(auto v : g[u]) if(!vis[v]) dfs(v);
}
struct node{
int l, r;
bool operator < (const node &A) const {
return l < A.l;
}
}a[N];
void run(){
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
int tot = 0;
for(int i = 1; i <= n; i++) {
if(!vis[i]) {
mx = 0, mn = INF;
dfs(i);
a[++tot] = node{mn, mx};
}
}
sort(a + 1, a + tot + 1);
int rb = 0;
int ans = 0;
for(int i = 1, j; i <= tot; i = j) {
j = i + 1;
rb = max(rb, a[i].r);
while(j <= tot && rb >= a[j].l) {
rb = max(rb, a[j].r);
++j;
}
++ans;
}
//dbg(tot, ans);
ans = tot - ans;
cout << ans << '\n';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> m) run();
return 0;
}
题意:
范围为\([1,m]\)的坐标轴上有\(n\)个点,每个点都有其半径\(r_i\)。
现在可以多次执行操作:选择一个点,使其半径增加\(1\)。
问至少多少次操作,使得这个坐标轴上每个整点至少被一个点覆盖。
思路:
解法一:
代码如下:
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/17 19:10:25
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
int n, m;
bool f[N];
int x[N], r[N];
struct Edge{
int u,v,w,next ;
}e[N * 80];
int head[N], tot;
struct node{
int d,u;
bool operator < (const node &A)const{
return d>A.d;
}
};
void adde(int u,int v,int w){
e[tot].v=v;e[tot].w=w;e[tot].next=head[u];head[u]=tot++;
}
int d[N];
bool vis[N];
void Dijkstra(int s){
priority_queue <node> q;
memset(d, INF, sizeof(d));
memset(vis, 0, sizeof(vis)); d[s]=0;
q.push(node{0, s});
while(!q.empty()){
node cur = q.top(); q.pop();
int u = cur.u;
if(vis[u]) continue ;
vis[cur.u] = 1;
for(int i = head[u]; i != -1; i = e[i].next){
int v = e[i].v;
if(d[v] > d[u] + e[i].w){
d[v] = d[u] + e[i].w;
q.push(node{d[v], v});
}
}
}
}
void run(){
memset(head, -1, sizeof(head));
for(int i = 1; i <= n; i++) {
cin >> x[i] >> r[i];
for(int j = max(1, x[i] - r[i]); j <= min(m, x[i] + r[i]); j++) f[j] = 1;
}
for(int i = 2; i <= m + 1; i++) {
if(f[i] && f[i - 1]) adde(i - 1, i, 0);
else adde(i - 1, i, 1);
}
for(int i = 1; i <= n; i++) {
int L = max(1, x[i] - r[i]), R = min(m, x[i] + r[i]);
int mx = max(L - 1, m - R);
for(int j = 0; j <= mx; j++) {
adde(max(1, L - j), min(m + 1, R + j + 1), j);
}
}
Dijkstra(1);
cout << d[m + 1];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> m) run();
return 0;
}
?
解法二:
emmm感觉说了一大堆还是自己对\(dp\)的理解不是很透彻,感觉在口胡一通= =
若有什么问题欢迎指出,thx~
代码如下:
Code
/*
* Author: heyuhhh
* Created Time: 2019/11/17 21:59:50
*/
#include <bits/stdc++.h>
#define MP make_pair
#define fi first
#define se second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << '\n'; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
#else
#define dbg(...)
#endif
void pt() {std::cout << '\n'; }
template<typename T, typename...Args>
void pt(T a, Args...args) {std::cout << a << ' '; pt(args...); }
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
int dp[N];
int x[N], r[N];
bool f[N];
int n, m;
void run(){
for(int i = 1; i <= n; i++) {
cin >> x[i] >> r[i];
for(int j = max(1, x[i] - r[i]); j <= min(m, x[i] + r[i]); j++) f[j] = 1;
}
for(int i = 0; i <= m; i++) dp[i] = i;
for(int i = 1; i <= m; i++) {
if(f[i]) dp[i] = min(dp[i], dp[i - 1]);
for(int j = 1; j <= n; j++) if(x[j] <= i) {
int cost = max(0, i - x[j] - r[j]);
dp[i] = min(dp[i], dp[max(0, x[j] - r[j] - cost - 1)] + cost);
}
}
cout << dp[m];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
while(cin >> n >> m) run();
return 0;
}
Codeforces Round #600 (Div. 2)
标签:void amp ref single tin precision run 覆盖 合并
原文地址:https://www.cnblogs.com/heyuhhh/p/11879117.html