CCF-CSP刷题库0-10

CCF-CSP刷题库0-10

 

模拟题

201312-1 出现次数最多的数

#include<bits/stdc++.h>
#define MAX 10005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&x);
        a[x]++;
    }
    int maxx=0,maxa=0;
    for(i=1;i<=10000;i++){
        if(a[i]>maxx){
            maxx=a[i];
            maxa=i;
        }
    }
    printf("%d\n",maxa);
    return 0;
}
View Code

201312-2 ISBN号码

#include<bits/stdc++.h>
#define MAX 10005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

string s;

int main()
{
    int t,n,m,x,y,i,j,k;
    cin>>s;
    int c=0,ans=0;
    for(i=0;i<12;i++){
        if('0'<=s[i]&&s[i]<='9'){
            c++;
            ans+=(s[i]-'0')*c;
        }
    }
    if(ans%11==10){
        if(s[12]=='X'){
            printf("Right\n");
        }
        else{
            s[12]='X';
            cout<<s<<endl;
        }
    }
    else{
        if(ans%11==s[12]-'0'){
            printf("Right\n");
        }
        else{
            s[12]=ans%11+'0';
            cout<<s<<endl;
        }
    }
    return 0;
}
View Code

201312-3 最大的矩形

//有坑点,矩形高度有>10000的

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    int ans=0;
    for(i=1;i<=n;i++){
        int minn=a[i];
        for(j=i;j<=n;j++){
            minn=min(minn,a[j]);
            ans=max(ans,(j-i+1)*minn);
        }
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201312-4 有趣的数

 

第一次认证

201403-1 相反数

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int b[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&x);
        if(x<0) b[-x]++;
        else b[x]++;
    }
    int ans=0;
    for(i=1;i<=1000;i++){
        if(b[i]==2) ans++;
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201403-2 窗口

//栈

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int x1,y1,x2,y2;
    int i;
}no;
stack<Node> s,s0;

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        scanf("%d%d%d%d",&no.x1,&no.y1,&no.x2,&no.y2);
        no.i=i;
        s.push(no);
    }
    while(m--){
        scanf("%d%d",&x,&y);
        int f=0;
        while(s.size()){
            if(s.top().x1<=x&&s.top().y1<=y&&x<=s.top().x2&&y<=s.top().y2){
                f=s.top().i;
                no=s.top();
            }
            s0.push(s.top());
            s.pop();
            if(f!=0) break;
        }
        while(s0.size()){
            s.push(s0.top());
            s0.pop();
        }
        if(f==0) printf("IGNORED\n");
        else{
            printf("%d\n",f);
            s.push(no);
        }
    }
    return 0;
}
View Code

201403-3 命令行选项

201403-4 无线网络

//bfs,用b[当前点][增设点个数]记录状态

#include<bits/stdc++.h>
#define MAX 105 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int x,y;
}a[MAX],c[MAX];
struct Node2{
    int x,y,c,p;
}no;

queue<Node2> q;
int b[MAX*2][MAX];

double dis(int x1,int y1,int x2,int y2){
    return fabs(x1-x2)*fabs(x1-x2)+fabs(y1-y2)*fabs(y1-y2);
}
int main()
{
    int t,n,m,x,y,i,j,k;
    double r;
    scanf("%d%d%d%lf",&n,&m,&k,&r);
    for(i=1;i<=n;i++){
        scanf("%d%d",&a[i].x,&a[i].y);
    }
    for(i=1;i<=m;i++){
        scanf("%d%d",&c[i].x,&c[i].y);
    }
    no.x=a[1].x;
    no.y=a[1].y;
    no.c=0;
    no.p=0;
    q.push(no);
    b[1][0]=1;
    int ff=0;
    while(q.size()){
        Node2 f=q.front();
        for(i=1;i<=n;i++){
            if(b[i][f.c]) continue;
            if(dis(f.x,f.y,a[i].x,a[i].y)>r*r) continue;
            if(i==2){
                printf("%d\n",f.p);
                ff=1;
                break;
            }
            b[i][f.c]=1;
            no.x=a[i].x;
            no.y=a[i].y;
            no.c=f.c;
            no.p=f.p+1;
            q.push(no);
        }
        if(ff==1) break;
        for(i=1;i<=m;i++){
            if(f.c+1>k||b[n+i][f.c+1]) continue;
            if(dis(f.x,f.y,c[i].x,c[i].y)>r*r) continue;
            b[n+i][f.c+1]=1;
            no.x=c[i].x;
            no.y=c[i].y;
            no.c=f.c+1;
            no.p=f.p+1;
            q.push(no);
        }
        q.pop();
    }
    return 0;
}
View Code

 

第二次认证

201409-1 相邻数对

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    int ans=0;
    for(i=1;i<n;i++){
        for(j=i+1;j<=n;j++){
            if(abs(a[i]-a[j])==1) ans++;
        }
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201409-2 画图

//标记,点边处理

#include<bits/stdc++.h>
#define MAX 105 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int b[MAX][MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    int x1,y1,x2,y2;
    while(n--){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        for(i=x1+1;i<=x2;i++){
            for(j=y1+1;j<=y2;j++){
                b[i][j]=1;
            }
        }
    }
    int ans=0;
    for(i=1;i<=100;i++){
        for(j=1;j<=100;j++){
            if(b[i][j]) ans++;
        }
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201409-3 字符串匹配

//s.find(s0)==s.npos找不到

#include<bits/stdc++.h>
#define MAX 105 
#define MOD 1000000007
using namespace std;
typedef long long ll;

string s0,s,ss;

string cg(string s){
    int len=s.length();
    for(int i=0;i<len;i++){
        if('a'<=s[i]&&s[i]<='z') s[i]-=32;
    }
    return s;
}
int main()
{
    int t,n,m,x,y,i,j,k;
    cin>>s0;
    scanf("%d%d",&n,&m);
    while(m--){
        cin>>s;
        ss=s;
        if(n==0){
            s0=cg(s0);
            s=cg(s);
        }
        if(s.find(s0)!=s.npos) cout<<ss<<endl;
    }
    return 0;
}
View Code

201409-4 最优配餐

 

第三次认证

201412-1 门禁系统

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int b[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&x);
        if(i>1) printf(" ");
        printf("%d",++b[x]);
    }
    return 0;
}
View Code

201412-2 Z字形扫描

//dfs斜着分成若干层处理,奇数层(横纵坐标和为偶数)正方向(顺序自左下至右上),偶数层反方向

#include<bits/stdc++.h>
#define MAX 505 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX][MAX];
vector<int> v;

void dfs(int n,bool zf,int x,int y){
    if(x<1||y<1||x>n||y>n) return;
    if(zf){
        v.push_back(a[x][y]);
        dfs(n,zf,x-1,y+1);
    }
    else{
        v.push_back(a[x][y]);
        dfs(n,zf,x+1,y-1);
    }
}
int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(i=1;i<=n;i++){
        if((i+1)%2==0) dfs(n,true,i,1);
        else dfs(n,false,1,i);
    }
    for(i=2;i<=n;i++){
        if((n+i)%2==0) dfs(n,true,n,i);
        else dfs(n,false,i,n);
    }
    for(i=0;i<v.size();i++){
        if(i>0) printf(" ");
        printf("%d",v[i]);
    }
    return 0;
}
View Code

201412-3 集合竞价

//开盘价一定是给出价格之一,整理排序后逆序枚举找出最大成交量

#include<bits/stdc++.h>
#define MAX 5005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node2{
    string s;
    double x;ll c;
}rec[MAX];
struct Node{
    double x;ll c;
}buy[MAX],sell[MAX];

bool cmp(Node a,Node b){
    return a.x<b.x;
}
int main()
{
    int t,n,m,y,i,j,k;
    t=0;
    double x;ll c;
    string s0;
    while(cin>>s0){
        rec[++t].s=s0;
        if(s0[0]=='c'){
            scanf("%d",&y);
            rec[y].s="cancel";
            continue;
        }
        scanf("%lf%lld",&x,&c);
        rec[t].x=x;
        rec[t].c=c;
    }
    n=m=0;
    for(i=1;i<=t;i++){
        if(rec[i].s[0]=='c') continue;
        if(rec[i].s[0]=='b'){
            n++;
            buy[n].x=rec[i].x;
            buy[n].c=rec[i].c;
        }
        else{
            m++;
            sell[m].x=rec[i].x;
            sell[m].c=rec[i].c;
        }
    }
    sort(buy+1,buy+n+1,cmp);
    sort(sell+1,sell+m+1,cmp);
    ll buyc=0,sellc=0,maxx=0;
    int maxi=0;
    for(i=1;i<=m;i++){
        sellc+=sell[i].c;
    }
    j=m;
    for(i=n;i>=1;i--){
        buyc+=buy[i].c;
        for(;j>=1;j--){
            if(sell[j].x<=buy[i].x) break;
            sellc-=sell[j].c;
        }
        if(min(buyc,sellc)>maxx){
            maxx=min(buyc,sellc);
            maxi=i;
        }
    }
    printf("%.2f %lld\n",buy[maxi].x,maxx);
    return 0;
}
View Code

201412-4 最优灌溉

//最小生成树

#include<bits/stdc++.h>
#define MAX 100005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int u,v,w;
}edge[MAX];

int f[1005];

int find(int x){
    return f[x]==x?x:f[x]=find(f[x]);
}
bool cmp(Node a,Node b){
    return a.w<b.w;
}
int kru(int n,int m){
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    sort(edge+1,edge+m+1,cmp);
    int cnt=0,ans=0;
    for(int i=1;i<=m;i++){
        int u=edge[i].u;
        int v=edge[i].v;
        int w=edge[i].w;
        int fu=find(u),fv=find(v);
        if(fu!=fv){
            f[fv]=fu;
            cnt++;
            ans+=w;
            if(cnt==n-1) return ans;
        }
    }
    return -1;
}
int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++){
        scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
    }
    printf("%d\n",kru(n,m));
    return 0;
}
View Code

 

第四次认证

201503-1 图像旋转

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX][MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(j=m;j>=1;j--){
        for(i=1;i<=n;i++){
            if(i>1) printf(" ");
            printf("%d",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
View Code

201503-2 数字排序

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int x,c;
}a[MAX];

bool cmp(Node a,Node b){
    if(a.c==b.c) return a.x<b.x;
    return a.c>b.c;
}
int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&x);
        a[x].x=x;
        a[x].c++;
    }
    sort(a,a+1000+1,cmp);
    for(i=0;i<=1000;i++){
        if(a[i].c==0) break;
        printf("%d %d\n",a[i].x,a[i].c);
    }
    return 0;
}
View Code

201503-3 节日

//先确定每年该月1日的星期,向后推算即可

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int t[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
            {0,31,29,31,30,31,30,31,31,30,31,30,31}};

bool isRun(int x){
    if((x%4==0&&x%100!=0)||x%400==0) return true;
    return false;
}
int xq(int y,int m){
    int ans=2;
    for(int i=1850;i<y;i++){
        if(isRun(i)) ans+=366;
        else ans+=365;
    }
    for(int i=1;i<m;i++){
        if(isRun(y)) ans+=t[1][i];
        else ans+=t[0][i];
    }
    return ans;
}
int main()
{
    int n,m,x,y,i,j,k;
    int a,b,c;
    scanf("%d%d%d%d%d",&a,&b,&c,&x,&y);
    c%=7;
    for(i=x;i<=y;i++){
        int first=xq(i,a);
        int f=0,cnt=0;
        if(isRun(i)){
            for(j=0;j<t[1][a];j++){
                if((first+j)%7==c) cnt++;
                if(cnt==b){
                    printf("%d/%02d/%02d\n",i,a,j+1);
                    f=1;
                    break;
                }
            }
        }
        else{
            for(j=0;j<t[0][a];j++){
                if((first+j)%7==c) cnt++;
                if(cnt==b){
                    printf("%d/%02d/%02d\n",i,a,j+1);
                    f=1;
                    break;
                }
            }
        }
        if(!f) printf("none\n");
    }
    return 0;
}
View Code

201503-4网络延时

//树的直径

#include<bits/stdc++.h>
#define MAX 20005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

vector<int> v[MAX];
int maxi,maxx;

void dfs(int pre,int x,int c){
    if(c>maxx){
        maxi=x;
        maxx=c;
    }
    for(int i=0;i<v[x].size();i++){
        int to=v[x][i];
        if(to==pre) continue;
        dfs(x,to,c+1);
    }
}
int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=2;i<=n;i++){
        scanf("%d",&x);
        v[i].push_back(x);
        v[x].push_back(i);
    }
    for(i=1;i<=m;i++){
        scanf("%d",&x);
        v[i+n].push_back(x);
        v[x].push_back(i+n);
    }
    maxi=1;maxx=0;
    dfs(-1,1,0);
    dfs(-1,maxi,0);
    printf("%d\n",maxx);
    return 0;
}
View Code

 

第五次认证

201509-1 数列分段

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    int ans=0;
    a[0]=-1;
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(a[i-1]!=a[i]) ans++;
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201509-2 日期计算

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int t[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},
            {0,31,29,31,30,31,30,31,31,30,31,30,31}};

bool isRun(int x){
    if((x%4==0&&x%100!=0)||x%400==0) return true;
    return false;
}
int main()
{
    int n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    int ans=0;
    if(isRun(n)){
        for(i=1;i<=12;i++){
            if(ans+t[1][i]>=m){
                printf("%d\n%d\n",i,m-ans);
                break;
            }
            ans+=t[1][i];
        }
    }
    else{
        for(i=1;i<=12;i++){
            if(ans+t[0][i]>=m){
                printf("%d\n%d\n",i,m-ans);
                break;
            }
            ans+=t[0][i];
        }
    }
    return 0;
}
View Code

201509-3 模板生成系统

//确定左右括号位置,锁定子串替换

#include<bits/stdc++.h>
#define MAX 105 
#define MOD 1000000007
using namespace std;
typedef long long ll;

string s[MAX];
map<string,string> mp;

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d ",&m,&n);
    for(i=0;i<m;i++){
        getline(cin,s[i]);
    }
    string s0,pre,now;
    for(i=0;i<n;i++){
        getline(cin,s0);
        int p=s0.find(" ");
        int l=s0.length();
        pre="{{ "+s0.substr(0,p)+" }}";
        now=s0.substr(p+2,l-p-3);
        mp[pre]=now;
    }
    for(i=0;i<m;i++){
        int p=0;
        int l=s[i].find("{{ ",p);
        int r=s[i].find(" }}",p)+2;  //锁定子串 
        string var;
        while(l!=s[i].npos&&r!=s[i].npos){
            var=s[i].substr(l,r-l+1); 
            if(!mp.count(var)){   //注意写法 
                s[i].erase(l,r-l+1);
                p=l;
            }
            else{
                s[i].replace(l,r-l+1,mp[var]);
                p=l+mp[var].length();
            }
            l=s[i].find("{{ ",p);
            r=s[i].find(" }}",p)+2;
        }
        cout<<s[i]<<endl;
    }
    return 0;
}
View Code

201509-4 高速公路

//有向图强连通分量中任意两点可互相到达,Kosaraju利用转置图具有和原图一样的强连通分量性质,O(n+m)(Tarjan求强连通分量

#include<bits/stdc++.h>
#define MAX 10005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

vector<int> v[MAX],rv[MAX];
vector<int> qlt[MAX];  //强连通分量 
stack<int> s;
int b[MAX];

void dfs(int x){  //正向
    b[x]=1;
    for(int i=0;i<v[x].size();i++){
        int to=v[x][i];
        if(b[to]) continue;
        dfs(to);
    }
    s.push(x);  //点结束时间 
}
void rdfs(int x,int k){  //逆向
    b[x]=1;
    qlt[k].push_back(x);
    for(int i=0;i<rv[x].size();i++){
        int to=rv[x][i];
        if(b[to]) continue;
        rdfs(to,k);
    }
}
int kos(int n){  //Kosaraju算法 
    memset(b,0,sizeof(b));
    for(int i=1;i<=n;i++){
        if(!b[i]) dfs(i);
    }
    memset(b,0,sizeof(b));
    int c=0;
    while(s.size()){  //点结束时间逆序 
        if(!b[s.top()]) rdfs(s.top(),++c);
        s.pop();
    }
    int ans=0;
    for(int i=1;i<=c;i++){
        int x=qlt[i].size();  //每个强连通分量点的个数 
        ans+=x*(x-1)/2;   //两两一对所有组合情况 
    }
    return ans;
}
int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        v[x].push_back(y);
        rv[y].push_back(x);
    }
    printf("%d\n",kos(n));
    return 0;
}
View Code

 

第六次认证

201512-1 数位之和

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    int ans=0;
    while(n){
        ans+=n%10;
        n/=10;
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201512-2 消除类游戏

#include<bits/stdc++.h>
#define MAX 35 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX][MAX];
int b[MAX][MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if(1<=i-1&&i+1<=n&&a[i-1][j]==a[i][j]&&a[i][j]==a[i+1][j]){
                b[i][j]=1;
                continue;
            }
            if(1<=j-1&&j+1<=m&&a[i][j-1]==a[i][j]&&a[i][j]==a[i][j+1]){
                b[i][j]=1;
                continue;
            }
            if(1<=i-2&&a[i][j]==a[i-1][j]&&a[i][j]==a[i-2][j]){
                b[i][j]=1;
                continue;
            }
            if(i+2<=n&&a[i][j]==a[i+1][j]&&a[i][j]==a[i+2][j]){
                b[i][j]=1;
                continue;
            }
            if(1<=j-2&&a[i][j]==a[i][j-1]&&a[i][j]==a[i][j-2]){
                b[i][j]=1;
                continue;
            }
            if(j+2<=m&&a[i][j]==a[i][j+1]&&a[i][j]==a[i][j+2]){
                b[i][j]=1;
                continue;
            }
        }
    }
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if(j>1) printf(" ");
            if(b[i][j]) printf("0");
            else printf("%d",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
View Code

201512-3 画图

//坐标系转化行列阵,(x,y) -> (n-y-1,x)

#include<bits/stdc++.h>
#define MAX 105 
#define MOD 1000000007
using namespace std;
typedef long long ll;

char a[MAX][MAX];
int t[4][2]={0,1,1,0,-1,0,0,-1};
int n,m;

void dfs(int x,int y,char c){
    if(x<0||y<0||x>=n||y>=m) return;
    if(a[x][y]=='-'||a[x][y]=='|'||a[x][y]=='+') return;
    if(a[x][y]==c) return;
    a[x][y]=c;
    for(int i=0;i<4;i++){
        int tx=x+t[i][0];
        int ty=y+t[i][1];
        dfs(tx,ty,c);
    }
}
int main()
{
    int q,x,y,i,j,k;
    scanf("%d%d%d",&m,&n,&q);
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            a[i][j]='.';
        }
    }
    int p,x1,y1,x2,y2;
    char c;
    while(q--){
        scanf("%d",&p);
        if(p==0){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            y1=n-y1-1;
            y2=n-y2-1;
            if(x1==x2){
                if(y1>y2){
                    y1^=y2;
                    y2^=y1;
                    y1^=y2;
                }
                for(i=y1;i<=y2;i++){
                    if(a[i][x1]=='-'||a[i][x1]=='+') a[i][x1]='+';
                    else a[i][x1]='|';
                }
            }
            else{
                if(x1>x2){
                    x1^=x2;
                    x2^=x1;
                    x1^=x2;
                }
                for(i=x1;i<=x2;i++){
                    if(a[y1][i]=='|'||a[y1][i]=='+') a[y1][i]='+';
                    else a[y1][i]='-';
                }
            }
        }
        else{
            scanf("%d%d %c",&x,&y,&c);
            dfs(n-y-1,x,c);
        }
    }
    for(i=0;i<n;i++){
        for(j=0;j<m;j++){
            printf("%c",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
View Code

201512-4 送货

//Hierholzer算法,欧拉路径充要条件,无向图需1.连通 2.存在0或2个奇数度点,若2则为起点和终点

//80分做法并查集判连通,再判断是否为半欧拉图(即存在欧拉路径),若合法用dfs搜路径(b数组标记边)结束时入栈

//100分需用栈模拟dfs将递归转化为非递归方法避免运行错误,递归本质就是栈

#include<bits/stdc++.h>
#define MAX 10005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

vector<int> v[MAX];
bool b[MAX][MAX];
int f[MAX];
stack<int> s;
vector<int> vv;

int find(int x){
    return f[x]==x?x:f[x]=find(f[x]);
}
void dfs(int x){  //while(s.size())
    //int x=s.top();
    for(int i=0;i<v[x].size();i++){
        int to=v[x][i];
        if(!b[x][to]){
            b[x][to]=b[to][x]=1;
            //s.push(to);
            //break;
            dfs(to);
        }
    }
//    if(i==v[x].size()){
//        vv.push_back(x);
//        s.pop();
//    }
    vv.push_back(x);
}
int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        f[i]=i;
    }
    for(i=1;i<=m;i++){
        scanf("%d%d",&x,&y);
        int fx=find(x),fy=find(y);
        if(fx!=fy) f[fy]=fx;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    int c=0,odd=0;
    for(i=1;i<=n;i++){
        if(find(i)==i) c++;
        sort(v[i].begin(),v[i].end());
        if(v[i].size()&1) odd++;
    }
    if(c!=1||odd!=0&&odd!=2||odd==2&&!(v[1].size()&1)){
        printf("-1\n");
    }
    else{
        //dfs(1);
        s.push(1);
        while(s.size()){
            int x=s.top();
            for(i=0;i<v[x].size();i++){
                int to=v[x][i];
                if(!b[x][to]){
                    b[x][to]=b[to][x]=1;
                    s.push(to);
                    break;
                }
            }
            if(i==v[x].size()){
                vv.push_back(x);
                s.pop();
            }
        }
        for(i=vv.size()-1;i>=0;i--){
            if(i<vv.size()-1) printf(" ");
            printf("%d",vv[i]);
        }
    }
    return 0;
}
View Code

 

第七次认证

201604-1 折点计数

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    int ans=0;
    for(i=2;i<n;i++){
        if(a[i-1]<a[i]&&a[i]>a[i+1]||a[i-1]>a[i]&&a[i]<a[i+1]) ans++;
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201604-2 俄罗斯方块

//枚举下落情况,两图覆盖相加不超过1即为合法,90分卡点见code数据

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[20][15],b[10][10];

int main()
{
    int t,n,m,x,y,i,j,k;
    for(i=1;i<=15;i++){
        for(j=1;j<=10;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(i=16;i<20;i++){
        for(j=1;j<=10;j++){
            a[i][j]=1;
        }
    }
    for(i=1;i<=4;i++){
        for(j=1;j<=4;j++){
            scanf("%d",&b[i][j]);
        }
    }
    scanf("%d",&t);
    int f=0;
    for(k=1;k<=15;k++){
        for(i=1;i<=4;i++){
            for(j=1;j<=4;j++){
                if(a[i+k][j+t-1]+b[i][j]>1){
                    f=1;
                    break;
                }
            }
            if(f==1) break;
        }
        if(f==1){
            for(i=1;i<=4;i++){
                for(j=1;j<=4;j++){
                    a[i+k-1][j+t-1]+=b[i][j];
                }
            }
            break;
        }
    }
    for(i=1;i<=15;i++){
        for(j=1;j<=10;j++){
            if(j>1) printf(" ");
            printf("%d",a[i][j]);
        }
        printf("\n");
    }
    return 0;
}
/*
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 1 1 1 1 1
1 1 1 1
0 0 0 0
0 0 0 0
0 0 0 0
1
*/
View Code

201604-3 路径解析

201604-4 游戏

//方法1:用三维bfs,每次+1步,第三维时间上限300。方法2:用优先队列,遇障碍+2步处理回当前点。以下用1

#include<bits/stdc++.h>
#define MAX 105 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int x,y,s;
}no;
queue<Node> q;
int p[MAX][MAX][MAX*3];
int b[MAX][MAX][MAX*3];
int to[4][2]={1,0,0,1,-1,0,0,-1};

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d%d",&n,&m,&t);
    while(t--){
        scanf("%d%d%d%d",&x,&y,&i,&j);
        for(k=i;k<=j;k++){
            p[x][y][k]=1;
        }
    }
    if(n==1&&m==1){
        printf("0\n");
        return 0;
    }
    no.x=1;no.y=1;no.s=0;
    q.push(no);
    int ans=0;
    while(q.size()){
        Node f=q.front();
        for(i=0;i<4;i++){
            int tx=f.x+to[i][0];
            int ty=f.y+to[i][1];
            int ts=f.s+1;
            if(tx<1||ty<1||tx>n||ty>m) continue;
            if(p[tx][ty][ts]||b[tx][ty][ts]) continue;
            b[tx][ty][ts]=1;
            if(tx==n&&ty==m){
                ans=ts;
                break;
            }
            no.x=tx;no.y=ty;no.s=ts;
            q.push(no);
        }
        if(ans>0) break;
        q.pop();
    }
    printf("%d\n",ans);
    return 0;
}
View Code

 

第八次认证

201609-1 最大波动

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    int ans=0;
    for(i=1;i<=n;i++){
        scanf("%d",&a[i]);
        if(i>1) ans=max(ans,abs(a[i]-a[i-1]));
    }
    printf("%d\n",ans);
    return 0;
}
View Code

201609-2 火车购票

//贪心,从前往后从左往右坐,标记每排最右边座位

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int a[25][10];
int b[25];
vector<int> v;

int main()
{
    int t,n,m,x,y,i,j,k;
    int c=0;
    for(i=1;i<=20;i++){
        for(j=1;j<=5;j++){
            a[i][j]=++c;
        }
    }
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        v.clear();
        int f=0;
        for(i=1;i<=20;i++){
            if(5-b[i]>=n){
                for(j=b[i]+1;j<=b[i]+n;j++){
                    v.push_back(a[i][j]);
                }
                b[i]+=n;
                f=1;
                break;
            }
        }
        if(f==0){
            int ff=0;
            for(i=1;i<=20;i++){
                if(b[i]<5){
                    for(j=b[i]+1;j<=5;j++){
                        n--;
                        b[i]++;
                        v.push_back(a[i][j]);
                        if(n==0){
                            ff=1;
                            break;
                        }
                    }
                    if(ff==1) break;
                }
            }
        }
        for(i=0;i<v.size();i++){
            if(i>0) printf(" ");
            printf("%d",v[i]);
        }
        printf("\n");
    }
    return 0;
}
View Code

201609-3 炉石传说

//vector模拟

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int att,hp;
}no;

vector<Node> v[10];
string s;

int main()
{
    int t,n,m,x,y,i,j,k;
    no.att=0;no.hp=30;
    v[0].push_back(no);
    v[1].push_back(no);
    scanf("%d",&n);
    t=0;
    while(n--){
        cin>>s;
        if(s[0]=='s'){
            scanf("%d%d%d",&x,&no.att,&no.hp);
            v[t%2].insert(v[t%2].begin()+x,no);
        }
        else if(s[0]=='a'){
            scanf("%d%d",&x,&y);
            v[t%2][x].hp-=v[(t+1)%2][y].att;
            v[(t+1)%2][y].hp-=v[t%2][x].att;
            if(v[t%2][x].hp<=0){
                if(x==0) break;
                v[t%2].erase(v[t%2].begin()+x);
            }
            if(v[(t+1)%2][y].hp<=0){
                if(y==0) break;
                v[(t+1)%2].erase(v[(t+1)%2].begin()+y);
            }
        }
        else{
            t++;
        }
    }
    if(v[1][0].hp<=0) printf("1\n");
    else if(v[0][0].hp<=0) printf("-1\n");
    else printf("0\n");
    for(i=0;i<2;i++){
        printf("%d\n",v[i][0].hp);
        printf("%d",v[i].size()-1);
        for(j=1;j<v[i].size();j++){
            printf(" %d",v[i][j].hp);
        }
        printf("\n");
    }
    return 0;
}
View Code

201609-4 交通规划

//Dijkstra,铁路长度为源点到其他各点的最短路径所经过边的权和(多种方案取最小且不计重复边)

//松弛操作时记录每个点的最近前驱(保证最优),在该点加入集合时其与前驱点所连的边即为最短路径经过的边

#include<bits/stdc++.h>
#define MAX 10005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int v,w;
}no;
vector<Node> v[MAX];
int b[MAX],dis[MAX],pre[MAX];
int ans;

void dij(int n,int k){
    for(int i=1;i<=n;i++){
        dis[i]=INT_MAX;
    }
    dis[k]=0;pre[k]=k;
    for(int i=1;i<=n;i++){
        int minn=INT_MAX,minj;
        for(int j=1;j<=n;j++){
            if(b[j]||dis[j]==INT_MAX) continue;
            if(dis[j]<minn){
                minn=dis[j];
                minj=j;
            }
        }
        b[minj]=1;
        ans+=dis[minj]-dis[pre[minj]];
        for(int j=0;j<v[minj].size();j++){
            int to=v[minj][j].v;
            if(b[to]) continue;
            if(dis[minj]+v[minj][j].w<dis[to]){
                dis[to]=dis[minj]+v[minj][j].w;
                pre[to]=minj;
            }
            else if(dis[minj]+v[minj][j].w==dis[to]){
                if(dis[to]-dis[minj]<dis[to]-dis[pre[to]]){
                    pre[to]=minj;
                }
            }
        }
    }
}
int main()
{
    int t,n,m,x,y,z,i,j,k;
    scanf("%d%d",&n,&m);
    while(m--){
        scanf("%d%d%d",&x,&y,&z);
        no.v=y;no.w=z;
        v[x].push_back(no);
        no.v=x;
        v[y].push_back(no);
    }
    ans=0;
    dij(n,1);
    printf("%d\n",ans);
    return 0;
}
View Code

 

第九次认证

201612-1 中间数

//前后缀

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int b[MAX],pre[MAX],suf[MAX];

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&x);
        b[x]++;
    }
    for(i=2;i<=1000;i++){
        pre[i]=pre[i-1]+b[i-1];
    }
    for(i=999;i>=1;i--){
        suf[i]=b[i+1]+suf[i+1];
    }
    int f=-1;
    for(i=1;i<=1000;i++){
        if(b[i]&&pre[i]==suf[i]){
            f=i;
            break;
        }
    }
    printf("%d\n",f);
    return 0;
}
View Code

201612-2 工资计算

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d",&n);
    if(n<=3500) printf("%d\n",n);
    else for(i=3500;i<=154400;i+=100){
        int A=i-3500;
        double tax=0;
        if(A<=1500) tax+=A*0.03;
        else{
            tax+=1500*0.03;
            if(1500<=A&&A<=4500) tax+=(A-1500)*0.1;
            else{
                tax+=3000*0.1;
                if(4500<=A&&A<=9000) tax+=(A-4500)*0.2;
                else{
                    tax+=4500*0.2;
                    if(9000<=A&&A<=35000) tax+=(A-9000)*0.25;
                    else{
                        tax+=26000*0.25;
                        if(35000<=A&&A<=55000) tax+=(A-35000)*0.3;
                        else{
                            tax+=20000*0.3;
                            if(55000<=A&&A<=80000) tax+=(A-55000)*0.35;
                            else{
                                tax+=25000*0.35;
                                tax+=(A-80000)*0.45;
                            }
                        }
                    }
                }
            }
        }
        if(i-tax==n){
            printf("%d\n",i);
            break;
        }
    }
    return 0;
}
View Code

201612-3 权限查询

201612-4 压缩编码

 

第十次认证

201703-1 分蛋糕

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&k);
    y=0;
    int ans=0;
    for(i=1;i<=n;i++){
        scanf("%d",&x);
        y+=x;
        if(y>=k){
            y=0;
            ans++;
        }
    }
    if(y>0) ans++;
    printf("%d\n",ans); 
    return 0;
}
View Code

201703-2 学生排队

//vector处理移动(删增),先删后增顺序可同时适用向前向后移

#include<bits/stdc++.h>
#define MAX 1005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

vector<int> v;

int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        v.push_back(i);
    }
    vector<int>::iterator it;
    while(m--){
        scanf("%d%d",&x,&y);
        for(i=0;i<n;i++){
            if(v[i]==x){
                it=v.begin();
                v.erase(it+i);
                v.insert(it+i+y,x);  //先删后增 
                break;
            }
        }
    }
    for(i=0;i<n;i++){
        if(i>0) printf(" ");
        printf("%d",v[i]);
    }
    return 0;
}
View Code

201703-3 Markdown

201703-4 地铁修建

//使最大边最小,Kruskal从最小边开始加,加到1和n连通时输出当前边

#include<bits/stdc++.h>
#define MAX 100005 
#define MOD 1000000007
using namespace std;
typedef long long ll;

struct Node{
    int u,v,w;
}edge[MAX*2];

int f[MAX];

bool cmp(Node a,Node b){
    return a.w<b.w;
}
int find(int x){
    return f[x]==x?x:f[x]=find(f[x]);
}
int kru(int n,int m){
    for(int i=1;i<=n;i++){
        f[i]=i;
    }
    sort(edge+1,edge+m+1,cmp);
    for(int i=1;i<=m;i++){
        int u=edge[i].u;
        int v=edge[i].v;
        int w=edge[i].w;
        int fu=find(u),fv=find(v);
        if(fu!=fv) f[fv]=fu;
        if(find(1)==find(n)) return w;
    }
    return -1;
}
int main()
{
    int t,n,m,x,y,i,j,k;
    scanf("%d%d",&n,&m);
    for(i=1;i<=m;i++){
        scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
    }
    printf("%d\n",kru(n,m));
    return 0;
}
View Code

 

 

待更

posted @ 2020-02-23 22:24  yzm10  阅读(547)  评论(0编辑  收藏  举报