7.24-个人训练赛第四场

---恢复内容开始---

A题:http://exam.upc.edu.cn/problem.php?id=6594&csrf=FDYgUjTWEh6I6Egkz0kEqT2kYmHXNawx

直接按照题意写

 1 #include <iostream>
 2 #include<string>
 3 using namespace std;
 4 int a[30];
 5 string s;
 6 int main()
 7 {
 8     cin>>s;
 9     int len=s.size();
10     for(int i=0;i<len;i++)
11     {
12         a[s[i]-'a']=1;
13     }
14     int flag=0;
15     for(int i=0;i<26;i++)
16     {
17         if(a[i]==0)
18         {
19             cout<<(char)(i+'a')<<endl;
20             flag=1;
21             break;
22         }
23     }
24     if(!flag)
25         cout<<"None"<<endl;
26 //    cout << "Hello world!" << endl;
27     return 0;
28 }
View Code

E题:http://exam.upc.edu.cn/problem.php?id=6351%2B&csrf=7kz5LVAKQLybqH6DddbTODF0xpCgiZyP

直接按照题意写

 1 #include <iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int n,w[100005];
 5 bool cmp(int x,int y)
 6 {
 7     return x>y;
 8 }
 9 int main()
10 {
11     cin>>n;
12     for(int i=0;i<n;i++)
13         cin>>w[i];
14     sort(w,w+n,cmp);
15     int i;
16     for(i=0;i<n;i++)
17     {
18         if(w[i]<i)
19             break;
20     }
21     cout <<i<< endl;
22     return 0;
23 }
View Code

F题:http://exam.upc.edu.cn/problem.php?id=6359&csrf=qjwsmQOap4rhydYIIRBPyGU4JnlEYPdH

直接按照题意写

 1 #include <iostream>
 2 #include<string>
 3 #include<algorithm>
 4 using namespace std;
 5 int n;
 6 string s[100],t;
 7 int len,a[30];
 8 bool fun(string s)
 9 {
10     int flag=0;
11     for(int i=0;i<len;i++)
12     {
13         if(s[i]!=t[i])
14         {
15             flag=1;
16             break;
17         }
18     }
19     if(!flag)   return true;
20     else        return false;
21 }
22 int main()
23 {
24     cin>>n;
25     for(int i=0;i<n;i++)
26         cin>>s[i];
27     cin>>t;
28     sort(s,s+n);
29     len=t.size();
30     int cnt=0;
31     for(int i=0;i<n;i++)
32     {
33         if(fun(s[i]))
34         {
35             a[s[i][len]-'A']=1;
36         }
37     }
38     for(int i=0;i<3;i++)
39         cout<<"*";
40     for(int i=0;i<5;i++)
41     {
42         if(a[i]==1)
43             cout<<(char)(i+'A');
44         else
45             cout<<"*";
46     }
47     cout<<endl;
48     int temp;
49     for(int i=0;i<2;i++)
50     {
51         for(int j=0;j<8;j++)
52         {
53             temp=5+i*8+j;
54             if(a[temp]==1)
55                 cout<<(char)(temp+'A');
56             else
57                 cout<<"*";
58         }
59         cout<<endl;
60     }
61     for(int i=0;i<5;i++)
62     {
63         if(a[i+21]==1)
64             cout<<(char)(i+21+'A');
65         else
66             cout<<"*";
67     }
68     cout<<"***"<<endl;
69 //    cout << "Hello world!" << endl;
70     return 0;
71 }
View Code

D题:http://exam.upc.edu.cn/problem.php?id=6690&csrf=mTD29wWRdrfUz2bjbJyvoanUV4Skc1v3

建立一个树,告诉你一个树根k,寻找k到x,y距离之和的最小值

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<queue>
 4 #include<cstdio>
 5 using namespace std;
 6 const int MAX=100005;
 7  
 8 typedef long long ll;
 9 const ll INF= 1000000000000000010 ;
10 ll n,a,b,c,q,k,x,y;
11 ll d[MAX];
12 vector <pair<ll,ll> > tt[MAX];
13  
14 void fun(ll k)
15 {
16     priority_queue<pair<ll, ll> > PQ;
17     int flag[MAX];
18     for(ll i=0;i<n;i++)
19     {
20         d[i]=INF;
21         flag[i]=0;
22     }
23     d[k]=0;
24     PQ.push(make_pair(0,k));
25     flag[k]=1;
26     while(!PQ.empty())
27     {
28         pair<ll,ll>f=PQ.top();
29         PQ.pop();
30         ll u=f.second;
31         flag[u]=2;
32         if(d[u]<f.first*(-1))   continue;
33         for(ll j=0;j<tt[u].size();j++)
34         {
35             ll v=tt[u][j].first;
36             if(flag[v]==2)    continue;
37             if(d[v]>d[u]+tt[u][j].second)
38             {
39                 d[v]=d[u]+tt[u][j].second;
40                 PQ.push(make_pair(d[v]*(-1),v));
41                 flag[v]=1;
42             }
43         }
44  
45     }
46 //    for(int )
47 }
48 int main()
49  
50 {
51     scanf("%lld",&n);
52     for(ll i=0;i<n-1;i++)
53     {
54         scanf("%lld%lld%lld",&a,&b,&c);
55         tt[a-1].push_back(make_pair(b-1,c));
56         tt[b-1].push_back(make_pair(a-1,c));
57     }
58     scanf("%lld%lld",&q,&k);
59     fun(k-1);
60     for(ll i=0;i<q;i++)
61     {
62         scanf("%lld%lld",&x,&y);
63         printf("%lld\n",d[x-1]+d[y-1]);
64 //        cout<<d[x-1]+d[y-1]<<endl;
65     }
66     return 0;
67 }
View Code

J题:http://exam.upc.edu.cn/problem.php?id=5501&csrf=S0dvWndO5hJ1kqYWYzFgHq2NIj9pJkW1

求a^bmodp

 1 #include <iostream>
 2  
 3 using namespace std;
 4 typedef long long ll;
 5 ll m,n,k;
 6 ll fun(ll m,ll n,ll k)
 7 {
 8     ll ans=1;
 9     while(n)
10     {
11         if(n&1)
12         {
13             ans=(ans%k*m%k)%k;
14         }
15         n/=2;
16         m=(m*m)%k;
17     }
18     return ans;
19 }
20 int main()
21 {
22     cin>>m>>n>>k;
23     cout<<fun(m,n,k)<<endl;
24     return 0;
25 }
View Code

K题:http://exam.upc.edu.cn/problem.php?id=5502&csrf=yK5OJgXIGZQBoKaz4U4PPXZ5x6mJYGKT

贪心问题

先按照时间从小到大排序,如果时间相同就按照分值从大到小排。定义一个时间变量xx。如果当前地鼠的时间大于等于xx,就加到结果上,每次更新分值的最小值;如果小于xx,若当前的分值大于前面所有地鼠分值的最小值,就在结果上加上这个差值,并更新最小值

 1 #include <iostream>
 2 #include<algorithm>
 3 #include<set>
 4 using namespace std;
 5 typedef long long ll;
 6 struct node
 7 {
 8     ll t,v;
 9 }mp[100005];
10 bool cmp(node x,node y)
11 {
12     if(x.t==y.t)    return x.v>y.v;
13     else    return x.t<y.t;
14 }
15 ll n,a[100005];
16 set<int>vi;
17 set<int>::iterator iter;
18 int main()
19 {
20     cin>>n;
21     for(ll i=0;i<n;i++)
22         cin>>mp[i].t;
23     for(ll i=0;i<n;i++)
24         cin>>mp[i].v;
25     sort(mp,mp+n,cmp);
26     ll ans=0;
27     ll xx=1,minn=mp[0].v,cnt=0;
28     for(ll i=0;i<n;i++)
29     {
30         if(mp[i].t>=xx)
31         {
32             ans+=mp[i].v;
33             xx++;
34             if(minn>=mp[i].v)
35             {
36                 minn=mp[i].v;
37             }
38             a[cnt++]=mp[i].v;
39         }
40         else
41         {
42             if(mp[i].v>minn){
43                 ans+=(mp[i].v-minn);
44                 sort(a,a+cnt);
45                 a[0]=mp[i].v;
46  
47                 minn=min(a[1],a[0]);
48             }
49         }
50  
51     }
52     cout <<ans<< endl;
53     return 0;
54 }
View Code

猜你喜欢

转载自www.cnblogs.com/scott527407973/p/9375962.html