题目:1210. 连号区间数
题解:枚举。复杂度最多为0(10^7)
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[10010];
int main(){
cin>>n;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
LL ans=0;
for(int i=0;i<n;i++){
int maxx=-100,minn=10010;
for(int j=i;j<n;j++){
maxx=max(maxx,a[j]);
minn=min(minn,a[j]);
if(maxx-minn==j-i)
ans++;
}
}
printf("%lld",ans);
return 0;
}
题目:1236. 递增三元组
题解:枚举+二分
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[N],b[N],c[N];
int main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
}
for(int i=1;i<=n;i++){
scanf("%d",&c[i]);
}
sort(a+1,a+n+1);
sort(b+1,b+1+n);
sort(c+1,c+1+n);
LL ans=0;
for(int i=1;i<=n;i++){
int ct_a=lower_bound(a+1,a+1+n,b[i])-a-1;
int ct_c=n-(upper_bound(c+1,c+1+n,b[i])-c)+1;
ans+=1ll*ct_a*ct_c;//注意10^5*10^5会超过int
}
printf("%lld",ans);
return 0;
}
题目:1245. 特别数的和
题解:模拟
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int main(){
cin>>n;
LL ans=0;
for(int i=1;i<=n;i++){
int t=i;
while(t){
int x=t%10;
t/=10;
if(x==2||x==0||x==1||x==9){
ans+=i;
break;
}
}
}
printf("%lld",ans);
return 0;
}
题目:1204. 错误票据
题解:排序+模拟
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[10010];
int k=0;
int main(){
cin>>n;
getchar();
string s;
while(n--){
getline(cin,s);
stringstream ssin(s);
while(ssin>>a[k])k++;//这里的k++,不能放在a[k++];
}
sort(a,a+k);
int l=0,r=0;
for(int i=1;i<k;i++){
if(a[i]==a[i-1]) r=a[i];
else if(a[i]>=a[i-1]+2) l=a[i]-1;
}
cout<<l<<" "<<r;
return 0;
}
题目:466. 回文日期
题解:枚举+模拟
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int date1,date2;
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int u){
if(u<date1||u>date2) return false;
int y=u/10000;
int m=u%10000/100;
int d=u%100;
if(m<=0||m>12|| d<=0) return false;
if(m!=2 &&d>mon[m]) return false;
if(m==2){
int t=0;
if(y%400==0||y%4==0&&y%100!=0) t=1;
if(d>mon[m]+t) return false;
}
return true;
}
int main(){
cin>>date1>>date2;
int s=date1/10000;
int e=date2/10000;
int ans=0;
for(int i=s;i<=e;i++){
int t=i;
t=t*10000+t%10*1000+t%100/10*100+t%1000/100*10+t/1000;
//cout<<endl<<t;
if(check(t)) ans++;
}
cout<<ans;
return 0;
}
题目:787. 归并排序
题解:归并排序模板题
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[N];
void merge_sort(int l,int r){
if(l>=r) return ;
int mid=l+r>>1;
merge_sort(l,mid);
merge_sort(mid+1,r);
int t[r-l+5];
int k=0;
int i=l,j=mid+1;
while(i<=mid&&j<=r){
if(a[i]<=a[j])t[k++]=a[i++];
else t[k++]=a[j++];
}
while(i<=mid) t[k++]=a[i++];
while(j<=r) t[k++]=a[j++];
for(int i=0;i<k;i++)
a[l+i]=t[i];
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
merge_sort(1,n);
for(int i=1;i<=n;i++){
printf("%d ",a[i]);
}
return 0;
}
题目:1219. 移动距离
题解:模拟
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int m,n,w;
int main(){
cin>>w>>m>>n;
m--,n--;
int r1=m/w;
int r2=n/w;
int c1=m%w;
int c2=n%w;
if(r1%2){
c1=w-c1-1;
}
if(r2%2){
c2=w-c2-1;
}
cout<<abs(r1-r2)+abs(c1-c2);
return 0;
}
题目:1229. 日期问题
题解:模拟+枚举。可能的日期范围内时间复杂度是10^6。所以模拟更便捷
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int u){
int y=u/10000;
int m=u%10000/100;
int d=u%100;
if(m<=0||m>12|| d<=0) return false;
if(m!=2 &&d>mon[m]) return false;
if(m==2){
int t=0;
if(y%400==0||y%4==0&&y%100!=0) t=1;
if(d>mon[m]+t) return false;
}
return true;
}
int main(){
int a,b,c;
scanf("%d/%d/%d",&a,&b,&c);
for(int i=19600101;i<=20591231;i++){
if(check(i)){
int d=i%100;
int m=i%10000/100;
int y=i%1000000/10000;
if(y==a&&m==b&&d==c||a==d&&b==m&&c==y||a==m&&b==d&&c==y){
printf("%d-%02d-%02d\n",i/10000,m,d);
}
}
}
return 0;
}
题目:1231. 航班时间
题解:模拟
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=1e5+10;
const int mod=100000007;
int t;
int main(){
cin>>t;
//getchar();
while(t--){
int a,b,c,d,e,f,m=0;
scanf("%d:%d:%d %d:%d:%d (+%d)",&a,&b,&c,&d,&e,&f,&m);
int t1=d*60*60+e*60+f+m*24*60*60-a*60*60-b*60-c;
m=0;
scanf("%d:%d:%d %d:%d:%d (+%d)",&a,&b,&c,&d,&e,&f,&m);
int t2=d*60*60+e*60+f+m*24*60*60-a*60*60-b*60-c;
//cout<<endl<<t1<<" "<<t2<<endl;
int t3=(t1+t2)/2;
printf("%02d:%02d:%02d\n",t3/3600,t3/60%60,t3%60);
}
return 0;
}
题目:1241. 外卖店优先级
题解:模拟。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e5+10;
const int mod=100000007;
int n,m,t;
bool sta[N];
int a[N];
int b[N];
PII ts[N];
int main(){
cin>>n>>m>>t;
int x,y;
for(int i=1;i<=m;i++){
scanf("%d %d",&ts[i].first,&ts[i].second);
}
sort(ts+1,ts+1+m);
for(int i=1;i<=m;i++){
x=ts[i].first,y=ts[i].second;
if(a[y]<x){
b[y]=max(0,b[y]-(x-a[y]-1));
}
if(sta[y]&&b[y]<=3) sta[y]=0;//一定要在下面+2之前进行判断
b[y]+=2;
if(b[y]>5) sta[y]=1;
a[y]=x;
}
int ans=0;
for(int i=1;i<=n;i++){
if(sta[i]){
b[i]=max(0,b[i]-(t-a[i]));
if(b[i]<=3) sta[i]=0;
}
if(sta[i]){
ans++;
//cout<<i<<endl;
}
}
cout<<ans;
return 0;
}
题目:788. 逆序对的数量
题解:归并排序
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=1e5+10;
const int mod=100000007;
int n;
int a[N];
LL ans=0;
void merge_sort(int l,int r){
if(l>=r) return;
int mid=l+r>>1;
merge_sort(l,mid);
merge_sort(mid+1,r);
int t[r-l+5];
int k=0;
int i=l,j=mid+1;
while(i<=mid&&j<=r){
if(a[i]<=a[j]){
t[k++]=a[i++];
}else{
ans+=mid-i+1;
t[k++]=a[j++];
}
}
while(i<=mid){
//ans+=r-mid;
t[k++]=a[i++];
}
while(j<=r){
t[k++]=a[j++];
}
for(int i=0;i<k;i++){
a[l+i]=t[i];
}
}
int main(){
cin>>n;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
merge_sort(1,n);
printf("%lld",ans);
return 0;
}