今天终于弄明白了lemon评测的文件格式!
T1
经典的一道题,贪心:对于第一个元素,只能由第二个元素给他,给完之后,第二个元素就变成了第一个元素,一直循环下去就行
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<string>
using namespace std;
int n,a[105];
int sum,ans;
int main(){
freopen("Playcard.in","r",stdin);
freopen("Playcard.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",a+i);
sum += a[i];
}
sum /= n;
for(int i=1;i<=n;++i){
int x;
if(a[i] != sum){
ans++;
x = a[i] - sum;
a[i] = sum;
a[i+1] += x;
}
}
printf("%d",ans);
}
T2
贪心,每次找到第一个单调减的数字,删掉
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<string>
using namespace std;
int main(){
freopen("delete.in","r",stdin);
freopen("delete.out","w",stdout);
string a;
int s;
cin >> a >> s;
int len = a.length();
for(int i=1;i<=s;++i){
for(int j=0;j<len;++j){
if(a[j] > a[j+1]){
for(int k=j;k<len-1;++k)
a[k] = a[k+1];
break;
}
}
}
int flag = 1;
for(int i=0;i<len-s;++i){
if(a[i] != '0')flag = 0;
if(!flag)putchar(a[i]);
}
if(flag)putchar('0');
}
T3
要求最长不上升子序列个数,就是求最长上升序列的长度,树状数组维护dp即可
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<string>
using namespace std;
const int N = 30000 + 5;
int c[N],a[1005],n,ans,mx;
inline int lowbit(int x){
return x & (-x);
}
inline void update(int i,int x){
for(;i<=mx;i+=lowbit(i))c[i] = max(c[i],x);
}
inline int query(int i){
int res = 0;
for(;i;i-=lowbit(i))res = max(res,c[i]);
return res;
}
int main(){
freopen("missile.in","r",stdin);
freopen("missile.out","w",stdout);
int x;
while(scanf("%d",&x) == 1){
a[++n] = x;
mx = max(mx,x);
}
for(int i=1;i<=n;++i){
int now = query(a[i])+1;
ans = max(ans,now);
update(a[i]+1,now);
}
printf("%d",ans);
}
T4
这个就是线段覆盖问题,贪心的板子,注意这里的区间左闭右开,我就当成闭区间错了(
按右端点排序,如果当前左端点不小于之前最右端点,就能加入
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<string>
using namespace std;
struct qwq{
int st,ed;
bool operator <(qwq x)const{
if(ed != x.ed)return ed < x.ed;
return st > x.st;
}
}a[1005];
int main(){
freopen("act.in","r",stdin);
freopen("act.out","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d%d",&a[i].st,&a[i].ed);
sort(a+1,a+1+n);
int now_end = -1,ans = 0;
for(int i=1;i<=n;++i){
if(a[i].st >= now_end){
now_end = a[i].ed;
ans++;
}
}
printf("%d",ans);
}
T5
大体上和上一道题一样,右端点排序,但这回是闭区间,而且条件变成当前区间左端点不超过之前最右端点
#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cctype>
#include<vector>
#include<string>
using namespace std;
struct qwq{
int x,y;
bool operator <(qwq b)const{
return y < b.y;
}
}a[10005];
int main(){
freopen("a.in","r",stdin);
freopen("a.out","w",stdout);
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d%d",&a[i].x,&a[i].y);
sort(a+1,a+1+n);
int now = a[1].y;
int ans = 1;
for(int i=2;i<=n;++i){
if(now >= a[i].x)continue;
ans++;
now = a[i].y;
}
printf("%d",ans);
}