标签:acm codeforces
题目传送:Codeforces Round #Pi (Div. 2)
水题。。
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int n;
int a[100005];
int main() {
scanf("%d", &n);
for(int i = 0; i < n; i ++) {
scanf("%d", &a[i]);
}
for(int i = 0; i < n; i ++) {
int ma = a[i] - a[0], mi = INF;
ma = max(ma, a[n-1] - a[i]);
if(i - 1 >= 0 && i - 1 < n) {
mi = min(mi, a[i] - a[i-1]);
}
if(i + 1 >= 0 && i + 1 < n) {
mi = min(mi, a[i + 1] - a[i]);
}
printf("%d %d\n", mi, ma);
}
return 0;
}
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
set<int> s;
map<int, int> mp;
char str[10];
int a;
int main() {
int n;
int jin_max = 0;//维护一个通过‘+‘而进阅读室的最大值
int chu_max = 0;//维护一个之前就在阅读室的最大值,则jin_max+chu_max为答案
scanf("%d", &n);
for(int i = 0; i < n; i ++) {
scanf("%s %d", str, &a);
if(str[0] == ‘+‘) {//进
s.insert(a);//放到一个集合里面
mp[a] = 1;//标记为访问过
jin_max = max(jin_max + chu_max, (int)s.size()) - chu_max;//维护一下进去的最大值
}
else {//出
if(mp.find(a) == mp.end()) {//如果之前都没出现过,那说明刚开始就在阅读室。
chu_max ++;
}
else {
s.erase(a);//之前出现过,那么现在现在肯定在set里面,出set
}
}
// cout << jin_max + chu_max << endl;
}
cout << jin_max + chu_max << endl;
return 0;
}
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
const int maxn = 200005;
LL zuo[maxn];//维护一个值i左边的i/k的个数
LL you[maxn];//维护一个值i右边的i*k的个数(注意这里可能会爆int,又跪了TOT)
LL a[maxn];
map<LL, int> mp;//对应从左往右遍历
map<LL, int> mp2;//对应从右往左遍历
int n, k;
int main() {
scanf("%d %d", &n, &k);
for(int i = 0; i < n; i ++) {
scanf("%I64d", &a[i]);
if(a[i] % k == 0) {
if(mp.find(a[i] / k) != mp.end()) {
zuo[i] = mp[a[i] / k];
}
else {
zuo[i] = 0;
}
}
else {
zuo[i] = 0;
}
if(mp.find(a[i]) == mp.end()) mp[a[i]] = 1;
else {
mp[a[i]] ++;
}
}
for(int i = n - 1; i >= 0; i --) {
if(mp2.find(a[i] * k) != mp2.end()) {
you[i] = mp2[a[i] * k];
}
else {
you[i] = 0;
}
if(mp2.find(a[i]) == mp2.end()) mp2[a[i]] = 1;
else {
mp2[a[i]] ++;
}
}
LL ans = 0;
for(int i = 0; i < n; i ++) {
ans += zuo[i] * you[i];
// cout << zuo[i] << " " << you[i] << endl;
}
cout << ans << endl;
return 0;
}
set里可以只记录shot的位置,再另加0和n+1这两个边界
而之前存的每个区间太慢了。。完美超时。。
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int n, k, a;
int m;
int fun(int x) {//算出一个区间可以放几条船
return (x + 1) / (a + 1);
}
int main() {
map<int, int> mp;
scanf("%d %d %d", &n, &k, &a);
scanf("%d", &m);
mp[0] = mp[n + 1] = 1;
int ship_sum = fun(n);
for(int i = 1; i <= m; i ++) {
int shot;
scanf("%d", &shot);
if(mp.find(shot) != mp.end()) {
continue;
}
mp[shot] = 1;
map<int, int>::iterator low = mp.lower_bound(shot);
low --;
map<int, int>::iterator up = mp.upper_bound(shot);
int l = low->first;
int r = up->first;
ship_sum -= fun(r - l - 1);
ship_sum += fun(shot - l - 1) + fun(r - shot - 1);
if(ship_sum < k) {
cout << i << endl;
return 0;
}
}
cout << -1 << endl;
return 0;
}
TLE的代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;
int n, k, a;
int m, t;
map<int, int> mp;
struct node {
int l, r;
node() {
}
node(int _l, int _r) : l(_l), r(_r) {
}
bool operator < (const node& a) const {
return l < a.l;
}
};
//bool operator < (node &a, node &b) {
// return a.l < b.l;
//}
set<node> s;
bool fun() {
int cnt = 0;
for(set<node>::iterator it = s.begin(); it != s.end(); it ++) {
// cout << it->r - it->l + 1 << " ";
cnt += ( (it->r - it->l + 2) / (a + 1) );
}
// cout << endl;
if(cnt >= k) return true;
else return false;
}
int main() {
scanf("%d %d %d", &n, &k, &a);
s.insert(node(1, n));
scanf("%d", &m);
int ans;
int flag = 0;
for(int i = 0; i < m; i ++) {
scanf("%d", &t);
if(mp.find(t) == mp.end()) {
mp[t] = 1;
}
else continue;
if(flag == 1) continue;
set<node>::iterator it = s.upper_bound(node(t, t));
it --;
int rr = it->r;
int ll = it->l;
s.erase(it);
if(ll == rr && rr == t) {
}
else if(rr == t) {
s.insert(node(ll, rr - 1));
}
else if(ll == t) {
s.insert(node(t + 1, rr));
}
else {
s.insert(node(ll, t - 1));
s.insert(node(t + 1, rr));
}
if(!fun()) {
flag = 1;
ans = i + 1;
}
}
if(flag == 0) {
printf("-1\n");
}
else printf("%d\n", ans);
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:acm codeforces
原文地址:http://blog.csdn.net/u014355480/article/details/47319123