The first part of the program design contest of the ninth Hunan College students

Source: Internet
Author: User
Tags cas cmath

Preface: This set of questions can not be found anywhere, the CSU can not submit the above. Finally in Hunan Normal University OJ found, but this normal university OJ is not good to use, each submission must be login again do not say, still do not support LLD, I also waste a lot of time to change WA.



Thought: The data quantity is small, direct brute force solves can.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
cmath> #include <map> #include <vector> using namespace std;
Char s[1050];
int pos[1050];
    int check (int b,int t,int k) {int num=0,l=b,r=t;
        for (int i=b; i<=t; i++) {if (s[l]!=s[r)) num++;
        if (num>2*k) return 0;
        l++;
    r--;
return 1;
    int main () {int k;
    int tot=1;
        while (~SCANF ("%d", &k)) {GetChar ();
        Gets (s);
        int Len=strlen (s), t=0;
                for (int i=0; i<len; i++) if (s[i]>= ' a ' &&s[i]<= ' Z ') {pos[t]=i;
            S[t++]=s[i];
                else if (s[i]>= ' A ' &&s[i]<= ' Z ') {pos[t]=i;
            s[t++]=s[i]-' a ' + ' a ';
        int ans=1,id=0;
          for (int i=0; i<t; i++) {for (int j=i+1; j<t; j + +)  {if (Pos[j]-pos[i]+1<=ans) continue;
                    if (check (i,j,k)) {ans=pos[j]-pos[i]+1;
                Id=pos[i];
    } printf ("Case%d:%d%d\n", tot++,ans,id+1);
return 0;
 }


Train of thought: Careful simulation problem, array simulation linked list operation can, the third operation is extremely troublesome, must be careful.

For the fourth operation, in fact, note that X and Y are the subscript of the original sequence, so the sequence is reversed and no objection to the 1,2 operation is only x inserted in the left and right side of Y reverse, the third operation has no effect

Code:

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
#define N 100050 struct Node {int qian,hou;} P[n];
    void init (int n) {p[1].qian=-1;
    P[n].hou=-1;
    for (int i=2; i<=n; i++) p[i].qian=i-1;
for (int i=1; i<n; i++) p[i].hou=i+1;
    int main () {int n,m,x,y,l;
    int tot=1;
        while (scanf ("%d%d", &n,&m)!=eof) {init (n);
        int num=0;
            while (m--) {scanf ("%d", &l);
            if (l==4) num++;
                else {if (num&1&&l<3) l=3-l;
                    if (l==1) {scanf ("%d%d", &x,&y);
                    if (p[y].qian==x) continue;
                    int Px=p[x].qian,py=p[y].qian;
                    int Hx=p[x].hou,hy=p[y].hou;
                    if (px!=-1) P[px].hou=hx;
                    if (hx!=-1) p[hx].qian=px;
     P[x].qian=p[y].qian;               P[x].hou=y;
                    P[y].qian=x;
                if (py!=-1) p[py].hou=x;
                    else if (l==2) {scanf ("%d%d", &x,&y);
                    if (p[y].hou==x) continue;
                    int Px=p[x].qian,py=p[y].qian;
                    int Hx=p[x].hou,hy=p[y].hou;
                    if (px!=-1) P[px].hou=hx;
                    if (hx!=-1) p[hx].qian=px;
                    P[x].hou=p[y].hou;
                    P[x].qian=y;
                    P[y].hou=x;
                if (hy!=-1) p[hy].qian=x;
                    else if (l==3) {scanf ("%d%d", &x,&y);
                    int Px=p[x].qian,py=p[y].qian;
                    int Hx=p[x].hou,hy=p[y].hou;
                        if (px==y) {P[x].qian=p[y].qian;
                        P[y].hou=p[x].hou; if (py!=-1) p[pY].hou=x;
                        if (hx!=-1) p[hx].qian=y;
                        P[y].qian=x;
                    P[x].hou=y;
                        else if (py==x) {P[y].qian=p[x].qian;
                        P[x].hou=p[y].hou;
                        if (px!=-1) p[px].hou=y;
                        if (hy!=-1) p[hy].qian=x;
                        P[x].qian=y;
                    P[y].hou=x;
                        else {p[x].qian=py;
                        P[Y].QIAN=PX;
                        P[x].hou=hy;
                        P[Y].HOU=HX;
                        if (px!=-1) p[px].hou=y;
                        if (hx!=-1) p[hx].qian=y;
                        if (py!=-1) p[py].hou=x;
                    if (hy!=-1) p[hy].qian=x;
        '}}} ' long ans=0;
        if (num&1){int head;
                    for (int i=1; i<=n; i++) if (p[i].hou==-1) {head=i;
                Break
                while (head!=-1) {ans+=head;
                Head=p[head].qian;
            if (head!=-1) Head=p[head].qian;
            } else {int head;
                    for (int i=1; i<=n; i++) if (p[i].qian==-1) {head=i;
                Break
                while (head!=-1) {ans+=head;
                Head=p[head].hou;
            if (head!=-1) Head=p[head].hou;
    } printf ("Case%d:%i64d\n", Tot++,ans);
return 0;
 }


Train of thought: direct comparison can

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
Char str[50][10];
int main ()
{
    int n;
    scanf ("%d", &n);
    for (int i=1;i<=5;i++) {
        scanf ("%s", str[i]+1);
    }
    int ans[20],id = 0;
    for (int i=1;i<=n;i++) {
        bool flag = FALSE;
        for (int j=4* (i-1) +1;j<=4*i&&!flag;j++) {
            printf ("%d%d\n", 4* (i-1) +1,4*i);
            if (str[1][j]== ' * ') {
                flag = true;
                if (str[2][j]!= '. ') ans[++id] = 1;
                else if (str[4][j]!= ' * ') ans[++id] = 3;
                else Ans[++id] = 2;
            }
    }} for (int i=1;i<=id;i++) {
        printf ("%d", ans[i]);
    printf ("\ n");
    return 0;
}



There are n cities and M roads, now from S to T, to ensure that you can go to

So the path has a pass time, but the road itself will open a time, turn off the b time so repeated cycle, from S to T of the shortest time

Thinking: At first thought very complex, in fact, is a problem of the shortest circuit, but added a point limit, will not affect the shortest distance s to T.

You can use Dijkstra or SPFA.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <
Cmath> #include <queue> using namespace std;
    #define INF 999999999 struct Edge {int v,next;
int a,b,t;
} edge[50050];
int cnt,head[305];
int n,m,s,e;
int vis[305],d[305];
    void Addedge (int u,int v,int a,int b,int t) {edge[cnt].v=v,edge[cnt].a=a,edge[cnt].b=b,edge[cnt].t=t;
edge[cnt].next=head[u],head[u]=cnt++;
    } void Init () {cnt=0;
Memset (head) (head,-1,sizeof);
    } void Spfa () {for (int i=1; i<=n; i++) D[i]=inf;
    memset (Vis) (vis,0,sizeof);
    d[s]=0;
    Vis[s]=1;
    queue<int>que;
    Que.push (s);
        while (!que.empty ()) {int Now=que.front ();
        Que.pop ();
        vis[now]=0;
            for (int i=head[now]; I!=-1 i=edge[i].next) {int v=edge[i].v,a=edge[i].a,b=edge[i].b,t=edge[i].t;
            int p=d[now]% (b+a), dis;
            if (p>a) dis=d[now]+a+b-p+t; Elseif (a-p<t) dis=d[now]+a+b-p+t;
            else dis=d[now]+t;
                if (D[v]>dis) {D[v]=dis;
                    if (!vis[v]) {Que.push (v);
                Vis[v]=1;
    ()}}} int main () {//freopen ("f.in", "R", stdin);
    Freopen ("F.txt", "w", stdout);
    int u,v,a,b,t;
    int tot=1;
        while (scanf ("%d%d%d%d", &n,&m,&s,&e)!=eof) {init ();
            while (m--) {scanf ("%d%d%d%d)%d", &u,&v,&a,&b,&t);
            if (t>a) continue;
        Addedge (u,v,a,b,t);
        } SPFA ();
    printf ("Case%d:%d\n", tot++,d[e]);
return 0;
 }







Train of thought: for a location just to the left and right side to find the nearest name, compare distance can

Code:

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int MAXN = 105;
int N,M,L[MAXN],R[MAXN];

Char str[maxn][5];
    int main () {scanf ("%d", &n);

    for (int i = 1; I <= n; i++) scanf ("%s", str[i));
    memset (l,-1,sizeof (L));
    memset (r,-1,sizeof (R));
            for (int i = 1; I <= n; i++) if (str[i][0]!= '? ')
        L[i] = i;
    else l[i] = l[i-1];
            for (int i = n; I >= 1; i--) if (str[i][0]!= '? ')
        R[i] = i;
    else r[i] = r[i+1];
    scanf ("%d", &m);
        while (m--) {int k;
        scanf ("%d", &k);
        if (str[k][0]!= '? ') printf ("%s\n", str[k));
                else {if (l[k] = = 1) {int cnt = R[k]-k + 1;
                for (int i = 1; i < cnt; i++) printf ("left of");
                printf ("%s\n", Str[r[k]);
            Continue
          } if (r[k] = =-1)  {int cnt = k-l[k] + 1;
                for (int i = 1; i < cnt; i++) printf ("Right of");
                printf ("%s\n", Str[l[k]);
            Continue
            } if (K-l[k] + 1 = r[k]-k + 1) printf ("middle of%s and%s\n", Str[l[k]],str[r[k]]);
                else if (K-l[k] + 1 < r[k]-K + 1) {int cnt = k-l[k] + 1;
                for (int i = 1; i < cnt; i++) printf ("Right of");
            printf ("%s\n", Str[l[k]);
                else if (K-l[k] + 1 > R[k]-k + 1) {int cnt = R[k]-k + 1;
                for (int i = 1; i < cnt; i++) printf ("left of");
            printf ("%s\n", Str[r[k]);
}} return 0;
 }


Train of thought: Every time the tide is actually a tree-like array of changes in the interval, then we each time to update the interval, the final evaluation can be

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

Std
const int MAXN = 100005;
int n,m,k,tree[maxn<<1];

int H[MAXN];

int lowbit (int x) {return x & x;}
        void Update (int x,int d) {while (x <= N) {tree[x] = D;
    x + + lowbit (x);
    } int sum (int x) {int ans = 0;
        while (x > 0) {ans = tree[x];
    x = Lowbit (x);
return ans;
    int main () {int a,b,cas = 1; while (scanf ("%d%d%d", &n,&m,&k)!=eof) {for (int i = 1; I <= n; i++) scanf ("%d", &am
        P;h[i]);
        memset (tree,0,sizeof (tree));
        Sort (h+1,h+1+n);
        int pre = 1;
            while (m--) {scanf ("%d%d", &a,&b);
            int pos = Lower_bound (h+1,h+1+n,1 + a)-H;
            Update (pre,1);
            Update (POS,-1);
        Pre = Lower_bound (h+1,h+1+n,b+1)-H;
   int ans = 0;     for (int i = 1; I <= n; i++) {int cnt = SUM (i);
        if (CNT >= k) ans++;
    printf ("Case%d:%d\n", Cas++,ans);
return 0;
 }


Two numbers x,y

To give a 3*10 matrix, the first line indicates the cost of adding 0~9 to the end of the number

The second line represents the current number plus the cost of 0~9

The third line represents the cost of the current number *0~9

Ask the minimum cost from the x->y, and the minimum number of operations in the case of minimum cost

Train of thought: Dp[i] represents the minimum cost required for the number of the I, triple cycle to transfer.

A num array is used to record the minimum number of operations during the transfer process.

Code:

#include <iostream> #include <cstdio> #include <cstring> using namespace std;
const int inf = 999999999;
int n,m;
int cost[4][10];

int dp[100005],num[100005];
    int main () {int cas = 1; while (scanf ("%d", &n,&m)!=eof) {for (int i = 1; I <= 3; i++) for (int j = 0; J <= 9 ;
        J + +) scanf ("%d", &cost[i][j]);
        for (int i = 0; I <= m i++) dp[i] = num[i] = inf;
        Dp[n] = 0;
        Num[n] = 0;
            if (n!= 0) {dp[0] = cost[3][0];
        Num[0] = 1;  for (int k = 0; k <= m. k++) for (int i = 1; I <= 3, i++) for (int j = 0; J <= 9;  J + +) {if (i = = 1) {int tmp = k% 10;
                        Remove the single if (TMP!= J) continue;
                        TMP = K/10;
           if (Dp[k] > Dp[tmp] + cost[i][j])             {Dp[k] = Dp[tmp] + cost[i][j];
                        NUM[K] = num[tmp] + 1; else if (dp[k] = = Dp[tmp] + cost[i][j]) num[k] = min (num[k],num[tmp]+
                    1);
                        else if (i = = 2) {int tmp = K-J;
                        if (TMP < 0) continue;
                            if (Dp[k] > Dp[tmp] + cost[i][j]) {dp[k] = Dp[tmp] + cost[i][j];
                        NUM[K] = num[tmp] + 1; else if (dp[k] = = Dp[tmp] + cost[i][j]) num[k] = min (num[k],num[tmp]+
                    1);
                        else {if (j = = 0) continue; if (k% J = = 0) {intTMP = k/j; if (Dp[k] > Dp[tmp] + cost[i][j]) {dp[k] = dp[tmp] + cost[i
                                ][J];
                            NUM[K] = num[tmp] + 1; else if (dp[k] = = Dp[tmp] + cost[i][j]) num[k] = min (num[k],n
                        UM[TMP]+1);
    } printf ("Case%d:%d%d\n", cas++,dp[m],num[m]);
return 0;
 }


Train of thought: direct violence enumeration A and B can be

Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main ()
{
    long long x,y;
    int tot=1;
    while (scanf ("%i64d%i64d", &x,&y)!=eof)
    {
        long long ans=0;
        For (Long long i=x i*i*i<=y*10+3; i++)
        {for
            (long long j=i; j*j*j<=y*10+3; j + +)
            {
                Long long m= I*i*i+j*j*j;
                if (m%10==3&&m/10>=x&&m/10<=y)
                    ans++
            }
        }
        printf ("Case%d:%i64d\n", tot++,ans*2);
    }
    return 0;
}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.