Training 7

6003: Shaolin

Meaning of the questions: Shaolin monks, each monk has a unique serial number and a unique combat value, each new monks to be fighting with a value closest test of the old monks (Rusi greater the number, the later time), seeking a test of all records;

#include<bits/stdc++.h>
using namespace std;
map<int,int> m;
set<int> s;
int main()
{
    int n;
    while(scanf("%d",&n),n)
    {
        m.clear();
        s.clear();
        m [1E9] = . 1 ; // Map value corresponding to the recording fighting ID 
        s.insert (1E9); // SET stored combat value, and implements sorting function 
        for ( int I = . 1 ; I <= n-; I ++ )
        {
            int x,y;
            Scanf ( " % D% D " , & X, & Y);
             SET < int > :: = s.lower_bound IT Iterator (Y); // find the old battle a first sequence of values greater than or equal monk new monk iterator combat 
            the printf ( " % D " , X); // new monk ID 
            IF (IT s.end == ()) // IT tail iterator set point, the sequence is not present than or equal to the value of y , it-- fighting value of the closest 
            {
                it--;
                printf("%d\n",m[*it]);
            }
            the else        // Otherwise 
            {        
                 IF (IT == s.begin ())
                    printf("%d\n",m[*it]);
                else
                {
                    set<int>::iterator it1=it;
                    IT1 - ;
                     IF (ABS (Y- * IT) <ABS (Y- * IT1)) // find the nearest value and the minimum 
                        the printf ( " % D \ n- " , m [* IT]);
                     the else 
                        the printf ( " D% \ n- " , m [* IT1]);
                }
            }
            m[y]=x;
            s.insert(y);
        }
    }
}

// stl container

3988: Find the Numbers

Meaning of the questions: to find an integer k in line with n1 + n2 + ... nk = s, n1 * n2 * .. * nk = p;

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int s,p,k,flag=0;
    cin>>s>>p>>k;
    if(k==1)
    {
        if(s==p)flag=1;
    }
    if(k==2)
    {
        for(int i=1;i*i<=p;i++)
        {
            if(p%i==0&&i+p/i==s)flag=1;
        }
    }
    if(k==3)
    {
        for(int i=1;i*i<=p;i++)
        {
            if(p%i!=0)
            continue;
            int d=p/i;
            for(int j=i;j*j<=d;j++)
            {
                if(d%j==0&&i+j+d/j==s)flag=1;
            }
        }
    }
    if(k==4)
    {
        for(int i=1;i*i<=p;i++)
        {
            if(p%i!=0)
            continue;
            int d=p/i;
            for(int j=i;j*j<=d;j++)
            {
                if(d%j!=0)continue;
               int e=d/j;
                for(int z=j;z*z<=e;z++)
                if(e%z==0&&i+j+z+e/z==s)flag=1;
            }
        }
    }
    if(flag)cout<<"YES\n";
    else cout<<"NO\n";
}

// k small number of layers, method of direct exhaustion

6017: Mobile phones 

That Italy: 0 instruction, setting the size of the matrix, an instruction, in the (x, y) plus a; 2 command, query (l, b, r, t) corresponding to the phone number of the matrix area, exit instruction 3

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll num[1050][1050];
void init()
{
    int x;
    scanf("%d",&x);
    memset(num,0,sizeof(num));
}
void update(int x,int y,int a)
{
    int temp=y;
    for(x;x<1050;x+=x&-x)
    {
        for(y=temp;y<1050;y+=y&-y)
        {
             num[x][y]+=a;
        }
    }

}
ll getsum(int x,int y)
{
    ll ans=0;
    int temp=y;
    for(x;x>0;x-=x&-x)
    {
        for(y=temp;y>0;y-=y&-y)
        {
            years + = num [x] [y];
        }
    }
    return years;
}
int main ()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==3)break;
        if(n==0)init();
        if(n==1)
        {
          int x,y,a;
          scanf("%d%d%d",&x,&y,&a);
          update(x+1,y+1,a);
        }
        if(n==2)
        {
            int l,b,r,t;
            scanf("%d%d%d%d",&l,&b,&r,&t);
            long long sum=getsum(r+1,t+1)+getsum(l,b)-getsum(r+1,b)-getsum(l,t+1);
            printf("%lld\n",sum);
        }
    }
}

// two-dimensional array of tree

3386: Fair Division

Meaning of the questions: n people chip in to buy a gift, in the case of the pursuit of fairness as much as possible so that people pay more money

#include<bits/stdc++.h>
using namespace std;
struct node{
   int id,money,cost;
}a[105];
void init()
{
    for(int i=1;i<=100;i++)
        a[i].id=a[i].money=a[i].cost=0;
}
int cmp(node x,node y)
{
    return x.money>y.money||x.money==y.money&&x.id<y.id;
}
int cmp1(node x,node y)
{
    return x.id<y.id;
}
int main ()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        the init (); // initialize 
        int costsum, n-;
        scanf("%d%d",&costsum,&n);
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i].money);
            sum+=a[i].money;
            a[i].id=i;
        }
        IF (SUM <costsum) // total amount of money is insufficient to cover the cost of buying gifts 
        {
            printf("IMPOSSIBLE\n");
            continue;
        }
        Sort (A + . 1 , + n-A + . 1 , CMP); // sorted in descending order according to the amount of money 
        int Human = n-;
         the while (costsum)
        {
            if(costsum/human)
            {
                int ave=costsum/human,realcut=0,humancut=0;//ave平均费用
                for(int i=1;i<=human;i++)
                {
                    if(a[i].money-a[i].cost>ave)
                        A [I] .cost + = Ave, realcut + = Ave; // Tyrant sufficient to pay more money to Ave 
                    the else 
                        realcut + = A [I] .money-A [I] .cost, A [I] .cost = A [I ] .money, humancut ++; // insufficient to cover ave, the number of how many pay 
                }
                Human - = humancut; // humancut is unable to participate in the next round of the number of cost-sharing 
                costsum- = realcut; // in the current round of cost-sharing, the actual assessed value 
            }
             the else // remaining Tyrant enough money remaining share 
            {
                for ( int I = . 1 ; I <= costsum; I ++) // among the top costsum Tyrant shared on a cents 
                  a [I] .cost ++ ;
                   BREAK ;
            }
        }
        Sort (A + . 1 , + n-A + . 1 , CMP1); // by number sort 
        for ( int I = . 1 ; I <= n-; I ++ )
        {
           if(i!=1)printf(" ");
           the printf ( " % D " , A [I] .cost); // output planned spent 
        }
        printf("\n");
    }
}

//greedy

6060: Anniversary party 

Meaning of the questions: n personal form of relationship between each tree, each person has their own degree of happiness, when his immediate boss was out, degree of happiness into force, seek the maximum degree of happiness

#include<bits/stdc++.h>
using namespace std;
int x,y,n,dp[6005][2],node,father[6005],son[6005],vis[6005];
void init()
{
    memset(dp,0,sizeof(dp));
    memset(vis,0,sizeof(vis));
    memset(son,0,sizeof(son));
}
void dfs(int root)
{
    vis[root]=1;
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0&&father[i]==root)
        {
            dfs(i);
            dp[root][1]+=dp[i][0];
            dp[root][0]+=max(dp[i][1],dp[i][0]);
        }
    }
}
int main ()
{
    while(~scanf("%d",&n))
    {
     init();//初始化
     for(int i=1;i<=n;i++)
     {
        Scanf ( " % D " , & DP [i] [ . 1 ]); // DP [i] [. 1] indicates the i-th person when the maximum value of the degree of happiness to a party of 
     }
      the while (Scanf ( " % D% D " , X &, & Y), X || Y)
     {
        Father [x] = Y; // parent node x 
        Son [x] ++ ;
     }
     for(int i=1;i<=n;i++)
     {
        IF (Son [I] == 0 ) // from the root node starts 
        {
            dfs(i);
            printf("%d\n",max(dp[i][1],dp[i][0]));
            break;
        }
     }
    }
}

// tree DP

6070: Twin Prime Conjecture

Seeking the extent consistent with the number of double primes conditions: title meaning

#include<bits/stdc++.h>
using namespace std;
const int Max=100005;
int f[Max],num[Max];
void init()
{
    f[0]=f[1]=1;int sum=0;
    for(int i=2;i<Max;i++)
    {
        if(f[i]==0)
        {
            if(f[i-2]==0)sum++;
            for(int j=i*2;j<Max;j+=i)
            {
                f[j]=1;
            }
        }
        Surely, [i] = I;
    }
}
int main ()
{
    init();
    int n;
    while(scanf("%d",&n),n>=0)
    {
        printf("%d\n",num[n]);
    }
}

// required prime

3677: Manhattan Sort 

#include<bits/stdc++.h>
using namespace std;
struct node
{
    an int mentioned id, the num;
}a[50];
int cmp(node x,node y)
{
    return x.num < y.num;
}
int main ()
{
    int t,n;
    scanf("%d",&t);
    for(int j=1;j<=t;j++)
    {
        scanf("%d",&n);
       for(int i=1;i<=n;i++)
       {   int x;
           scanf("%d",&x);
           a[i].id=i;
           a [i] .num = x;
       }
       sort(a+1,a+n+1,cmp);
       int sum=0;
       for(int i=1;i<=n;i++)
       {
           sum+=abs(a[i].id-i);
       }
       printf("Case #%d: %d\n",j,sum/2);
    }
}

// Sort

 

Guess you like

Origin www.cnblogs.com/llhsbg/p/11785011.html