javascript
2018 Spring Team Contest D HDU - 6023、HDU - 6024、HDU - 6025 、HDU - 6027 、HDU - 6029
??HDU - 6023??//編譯錯誤不算罰時
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int vis[100]; int num[20]; int main() {int t;cin>>t;while(t--){int n,m;cin>>n>>m;int id,h1,f1;char str[5];int cnt=0,sum=0;memset(vis,0,sizeof(vis));memset(num,0,sizeof(num));for(int i=0;i<m;i++){scanf("%d",&id);scanf("%d",&h1);getchar();scanf("%d",&f1);scanf("%s",str);id=id-1000;if(vis[id]==1)continue;if(strcmp(str,"AC")==0){vis[id]=1;sum+=h1*60+f1+num[id];}elsenum[id]+=20;}for(int i=0;i<=n;i++)if(vis[i]==1)cnt++;cout<<cnt<<" "<<sum<<endl;}return 0; }HDU - 6024
HDU’s?nn?classrooms are on a line ,which can be considered as a number line. Each classroom has a coordinate. Now Little Q wants to build several candy shops in these?nn?classrooms.?The total cost consists of two parts. Building a candy shop at classroom?ii?would have some cost?cici. For every classroom?PP?without any candy shop, then the distance between?PP?and the rightmost classroom with a candy shop on?PP's left side would be included in the cost too. Obviously, if there is a classroom without any candy shop, there must be a candy shop on its left side.?
Now Little Q wants to know how to build the candy shops with the minimal cost. Please write a program to help him.?
InputThe input contains several test cases, no more than 10 test cases.?
In each test case, the first line contains an integer? n(1≤n≤3000)n(1≤n≤3000), denoting the number of the classrooms.?
In the following? nn?lines, each line contains two integers? xi,ci(?109≤xi,ci≤109)xi,ci(?109≤xi,ci≤109), denoting the coordinate of the? ii-th classroom and the cost of building a candy shop in it.?
There are no two classrooms having same coordinate.OutputFor each test case, print a single line containing an integer, denoting the minimal cost.Sample Input 3 1 2 2 3 3 4 4 1 7 3 1 5 10 6 1Sample Output 5 11
題意是,花費的代價是:要是在這個位置上建立糖果屋就花菲建糖果屋的代價,要不就在這個位置上建糖果屋花費的代價就是,這個屋子的坐標減去離他最近的建糖果屋的坐標,還有最左面的屋子一定要建立糖果屋的。
不是貪心的題:
舉個反例:
當輸入 ????5
1 2
2 2
3 2
4 3
5 4
的時候,在2這個做標處,按照貪心處理的話不會在這個坐標建糖果屋,花費是: 2+1+2+3+4,?而選擇建的話是 2+2+1+2+3+4,所以貪心的思想是錯誤的。
點擊打開題解鏈接
HDU - 6025?:
Do you know what is called ``Coprime Sequence''? That is a sequence consists of?nnpositive integers, and the GCD (Greatest Common Divisor) of them is equal to 1.?``Coprime Sequence'' is easy to find because of its restriction. But we can try to maximize the GCD of these integers by removing exactly one integer. Now given a sequence, please maximize the GCD of its elements. InputThe first line of the input contains an integer? T(1≤T≤10)T(1≤T≤10), denoting the number of test cases.?
In each test case, there is an integer? n(3≤n≤100000)n(3≤n≤100000)?in the first line, denoting the number of integers in the sequence.?
Then the following line consists of? nn?integers? a1,a2,...,an(1≤ai≤109)a1,a2,...,an(1≤ai≤109), denoting the elements in the sequence.OutputFor each test case, print a single line containing a single integer, denoting the maximum GCD.Sample Input 3 3 1 1 1 5 2 2 2 3 2 4 1 2 4 8Sample Output 1 2 2
用公約數的前綴和和后綴方法:
總結:求一串數字的最大公約數和數字的順序無關,
//#include<iostream> //#include<algorithm> //#include <cstdio> #include <bits/stdc++.h> using namespace std;//long long dp[3005][3005]={0}; int num[100005]; int pre[100005]; int nre[100005]; int _gcd(int a,int b) {return b?_gcd(b,a%b):a; } int main() {int Case,n;cin>>Case;for(int t=0;t<Case;++t){scanf("%d",&n);for(int i=0;i<n;++i)scanf("%d",&num[i]);pre[0]=num[0];nre[n-1]=num[n-1];for(int i=1;i<n;++i){pre[i]=_gcd(pre[i-1],num[i]);}for(int i=n-2;i>=0;--i){nre[i]=_gcd(nre[i+1],num[i]);}int MAX=max(pre[n-2],nre[1]);for(int i=1;i<n-1;++i){MAX=max(MAX,_gcd(pre[i-1],nre[i+1]));}printf("%d\n",MAX);}return 0; }用的一種找規律的方法,這道題就讓去掉一個數字然后求他們的最大公約數,那么你不管怎么去(偏大的還是偏小的)那個數字,剩下的數字的最大公約數肯定是小于這個數字串的最小的那個數字,可以從數字串a的a1位置開始暴力,(要是去掉2個數字,可以從a2位置的數字開始枚舉,不過去除的數字太多的話,應該會TLE,所以不如上面的求前綴后綴的方法好)
#include<iostream> #include<algorithm> using namespace std;int main() {int t;cin>>t;int n;int a[100005];while(t--){cin>>n;for(int i=0;i<n;i++){cin>>a[i];}sort(a,a+n);int h=a[1];int flag=0;for(int j=h;j>=1;j--){int sum=0;for(int i=0;i<n;i++){if(a[i]%j==0)sum++;if(sum>=n-1){flag=1;cout<<j<<endl;break;}}if(flag==1)break;}}return 0; }(隊友提到一道題,給你n個數字求他們的最大公因子是多少:從他們的最小的那個數字n開始找到1要是中間的有個數字%==0這個數字串的次數為數字串的個數,就是這個串的最大公約數)HDU - 6027?
Given two integers?nn?and?kk. Let?f(i)=ikf(i)=ik, please evaluate the sum?f(1)+f(2)+...+f(n)f(1)+f(2)+...+f(n). The problem is simple as it looks, apart from the value of?nnin this question is quite large.?
Can you figure the answer out? Since the answer may be too large, please output the answer modulo?109+7109+7. InputThe first line of the input contains an integer? T(1≤T≤20)T(1≤T≤20), denoting the number of test cases.?
Each of the following? TT?lines contains two integers? n(1≤n≤10000)n(1≤n≤10000)?and? k(0≤k≤5)k(0≤k≤5).?
OutputFor each test case, print a single line containing an integer modulo? 109+7109+7.Sample Input 3 2 5 4 2 4 1Sample Output 33 30 10
大數,
import java.util.*; import java.math.*; public class Main{ public static void main(String args[]) { Scanner cin=new Scanner (System.in);int n;n=cin.nextInt();//BigInteger a;int a;int k;BigInteger MOD=new BigInteger("1000000007");for(int i=0;i<n;++i){BigInteger sum=BigInteger.ZERO;a=cin.nextInt();k=cin.nextInt();BigInteger j=BigInteger.ONE;//=new BigInteger("1");for(int m=0;m<a;m++){// System.out.println();sum=sum.add(j.pow(k));sum=sum.mod(MOD); j=j.add(BigInteger.ONE);}System.out.println(sum);} } }用快速冪:
#include<iostream> #include<algorithm> #include <cstdio> using namespace std; const int MOD = 7+1e9; //#define MOD 1e9+7 越界了 long long n,k; long long POW(long long p,long long x) {long long ans=1;while(x){if(x&1)ans=(ans*p)%MOD;p=(p*p)%MOD;x>>=1;}return ans; } int main() {long long t;cin>>t;while(t--){long long sum=0;cin>>n>>k;for(long long p=1;p<=n;++p){sum=(sum+POW(p,k))%MOD;}//printf("%lld\n",sum);cout<<sum<<endl;}return 0; }
Graph Theory
? HDU - 6029Little Q loves playing with different kinds of graphs very much. One day he thought about an interesting category of graphs called ``Cool Graph'', which are generated in the following way:?
Let the set of vertices be {1, 2, 3, ...,?nn}. You have to consider every vertice from left to right (i.e. from vertice 2 to?nn). At vertice?ii, you must make one of the following two decisions:?
(1) Add edges between this vertex and all the previous vertices (i.e. from vertex 1 to?i?1i?1).?
(2) Not add any edge between this vertex and any of the previous vertices.?
In the mathematical discipline of graph theory, a matching in a graph is a set of edges without common vertices. A perfect matching is a matching that each vertice is covered by an edge in the set.?
Now Little Q is interested in checking whether a ''Cool Graph'' has perfect matching. Please write a program to help him.?
InputThe first line of the input contains an integer? T(1≤T≤50)T(1≤T≤50), denoting the number of test cases.?
In each test case, there is an integer? n(2≤n≤100000)n(2≤n≤100000)?in the first line, denoting the number of vertices of the graph.?
The following line contains? n?1n?1?integers? a2,a3,...,an(1≤ai≤2)a2,a3,...,an(1≤ai≤2), denoting the decision on each vertice.OutputFor each test case, output a string in the first line. If the graph has perfect matching, output ''Yes'', otherwise output ''No''.?
Sample Input 3 2 1 2 2 4 1 1 2Sample Output Yes No No
意思是給你一個數n代表有n個頂點(1,2,3.....n),然后再給你n-1個數字代表對頂點2,3,4,,,,n所做的操作,操作有兩種形式,1和2
1代表把當前頂點(ai)及其之前的頂點都加上一條邊,可以放到一個集合里面(邊往集合哩放的條件是,這個邊的兩個頂點在這個集合里面都沒有出現過)2這個操作代表這個點是個孤立的頂點不給他加邊,然后問到最后那個集合里面是不是包含這n個所有頂點。mmp
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int main() {int t;cin>>t;while(t--){int n,op;cin>>n;// int edge[100005]={0};int mgj=0;//統計沒加進去的點的個數for(int i=2;i<=n;++i){scanf("%d",&op);//cin>>op;if(op==1){if(!mgj)mgj=0;elsemgj--;}elsemgj++;}if(n&1)//奇數構成的邊肯定要剩下一個頂點孤立printf("No\n");else if(!mgj)printf("Yes\n");// cout<<"Yes"<<endl;elseprintf("No\n");// cout<<"NO"<<endl;}return 0; } #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int main() {int t;cin>>t;while(t--){int n,order;cin>>n;int flag=-1,sum=0;//sum統計加進去的個數for(int i=2;i<=n;i++){cin>>order;if(order==1){if(sum<i-1)sum+=2;}}if(n%2==1)printf("No\n");else{if(sum==n) printf("Yes\n");else printf("No\n");}}return 0; }總結
以上是生活随笔為你收集整理的2018 Spring Team Contest D HDU - 6023、HDU - 6024、HDU - 6025 、HDU - 6027 、HDU - 6029的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Codeforces Round #47
- 下一篇: lightoj-1028 Trailin