锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 楂樼簿搴﹂櫎娉?br>---- //system("PAUSE"); Io and Ao are playing a word game. They say a word in the dictionary by turns. The word in the dictionary only contains lowercase letters. And the end character of the former said word should be the same as the start character of the current said word. They can start the game from any word in the dictionary. Any word shouldn't be said twice. Now, we define the complexity of the game that is the sum length of all words said in the game. Give you a dictionary, can you tell me the max complexity of this word game? The first line contains a single positive integer n(0 < n <=12). Next n lines are n words in the dictionary. The length of each word will not exceed 100. A single integer represents the complexity of the game.
Problem Source: provided by loon #include<iostream> //system("PAUSE"); 浠ヤ笂浠g爜瓚呮椂銆傚畬鍏ㄥ彲浠ュ壀鏋濄?br>涓句釜渚嬪瓙 //system("PAUSE"); 鍥犱負I錛?pre鍙堥敊浜嗗嚑涓嬨?br>浠ュ悗debug灝介噺鑷繁鐢ㄧ溂鐫涚湅錛屾洿鐪佹椂闂達紒錛侊紒錛侊紒錛侊紒錛侊紒#include<stdio.h>
void func(int i)
{
int m,n,k,j;
int temp,vmax=4*i+2;
for(m=1;m<i;m++)
for(n=m;n<=i;n++)
{
j=m*n;//閬垮厤浜嗕笁閲嶅驚鐜?/span>
if(j>i)
break;//鑳界渷涓嶅皯鏃墮棿
if(i%j==0)//鍒ゆ柇鏄惁鑳芥暣闄?/span>
{
k=i/j;
temp=2*(k*m+k*n+j);
if(temp<vmax)
vmax=temp;
}
}
printf("%d\n",vmax);
}
int main()
{
int num,n;
scanf("%d",&num);
getchar();
while(num--)
{
scanf("%d",&n);
getchar();
func(n);
}
//system("PAUSE");
return 0;
}
]]>Sample input
3
7
9901
Output for sample input
3
6
12
鍙互涓嶇敤楂樼簿搴﹂櫎娉曪紝鐩存帴濡備笅錛屽緢宸у錛屼笉閫氱敤銆?br>#include <stdio.h>
int main()
{
int n, a, b;
while (EOF != scanf("%d", &n))
{
a = 1;
b = 1;
while (a)
{
a = (a * 10 + 1) % n;
++b;
}
printf("%d\n", b);
}
}
#include<iostream>
#include<cstdlib>
using namespace std;
int a[9]={1,11,111,1111,11111,111111,1111111,11111111,111111111};
const int MOD=11111;
bool test(int i,int j)
{
if(j<10)
{
if(a[j-1]%i==0)
return true;
return false;
}
else
{
int temp=0;
int k=j%5;
int p=j/5;
if(k==0)
{
for(int m=1;m<=p;m++)
{
temp*=100000;
temp+=MOD;
temp%=i;
}
if(temp==0)
return true;
else
return false;
}
else
{
int m,f=1;
for(m=1;m<=p;m++)
{
temp*=100000;//鐢變簬5浣?浣嶉櫎錛屽簲褰撲箻涓?0^5;
temp+=MOD;
temp%=i;
}
for(m=0;m<k;m++)//鏈鍚庡簲褰撲箻浠ュ叾鍚庣殑浣嶆暟涔?0^n錛?br> f*=10;
if((temp*f+a[k-1])%i==0)
return true;
else
return false;
}
}
}
int main()
{
//freopen("s.txt","r",stdin);
//freopen("key.txt","w",stdout);
int i;
while(cin>>i)
{
if(i==1)
{
cout<<'1'<<endl;
continue;
}
for(int j=2;;j++)
{
if(test(i,j))
{
cout<<j<<endl;
break;
}
}
}
return 0;
}
]]>
Status
In/Out
TIME Limit
MEMORY Limit
Submit Times
Solved Users
JUDGE TYPE
stdin/stdout
3s
8192K
439
74
Standard
Input
Output
Sample Input
3
joj
jlu
acm
6
cctv
redcode
lindong
we
love
programming
3
daoyuanlee
come
on
Sample Output
6
11
10
#include<cstdlib>
using namespace std;
struct S
{
string a;
char begin;
char end;
int length;
}s[13];
int visited[13];
int temp;
void search(int a,int num,int pre)
{
for(int i=0;i<num;i++ )
{
if(s[i].begin==s[pre].end&&visited[i]==0)
{
visited[i]=1;
search(a+s[i].length,num,i);
if(a+s[i].length>temp)temp=a+s[i].length;
visited[i]=0;
}
}
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num;
while(cin>>num)
{
int i;
temp=0;
for( i=0;i<num;i++)
{
cin>>s[i].a;
s[i].length=(s[i].a).size();
s[i].begin=(s[i].a)[0];
s[i].end=(s[i].a)[s[i].length-1];
if(s[i].length>temp)
temp=s[i].length;
}
for(i=0;i<num;i++)
{
memset(visited,0,sizeof(visited));
visited[i]=1;
search(s[i].length,num,i);
}
cout<<temp<<endl;
}
return 0;
}
abc
cbd
dbm
dbacmdp
鎴戠殑紼嬪簭涓鐩存悳鍟婃悳錛屾瘡嬈℃悳瀹岄兘閲嶆柊寮濮嬨傛瘮濡傚湪浠寮澶村悗錛屾悳鍒癱錛屼笅嬈″啀鎼滅儲鏃剁洿鎺ュ埄鐢╟鐨勭粨鏋滐紝榪欐槸娣辨悳鐨勭壒鐐瑰喅瀹氱殑錛侊紒錛?br>*************************
榪欑綾諱技鐨勬湁搴忔悳绱㈤兘鍙互鐢?nbsp; * 澶囧繕褰曟柟娉?
**************************
#include<iostream>
#include<cstdlib>
using namespace std;
int num;
struct S
{
string a;
char begin;
char end;
int length;
}s[13];
int visited[13];
int temp;
int sum[13];
int search(int pre)//·µ»Ø´ÓpreµãÒÔºóµÄ×ܵÄÖµ
{
int j=s[pre].length,k=0;
for(int i=0;i<num;i++ )
{
if(s[i].begin==s[pre].end&&visited[i]==0&&i!=pre)//蹇呴』瑕佹湁I錛?pre
{
visited[i]=1;
k=search(i)+s[pre].length;
if(k>j)j=k;
}
else
{
if(s[i].begin==s[pre].end&&i!=pre)//蹇呴』瑕佹湁i!=pre
return sum[i]+s[pre].length;//鐩稿綋浜庡蹇樺綍錛岃屼笖鏃犻渶visited[i]=0;
}
}
sum[pre]=j;
return j;
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
while(cin>>num)
{
int i,j;
temp=0;
j=0;
for( i=0;i<num;i++)
{
cin>>s[i].a;
s[i].length=(s[i].a).size();
s[i].begin=(s[i].a)[0];
s[i].end=(s[i].a)[s[i].length-1];
if(s[i].length>temp)
temp=s[i].length;
}
for(i=0;i<num;i++)
{
memset(visited,0,sizeof(visited));
memset(sum,0,sizeof(sum));
visited[i]=1;
j=search(i);
if(j>temp)
temp=j;
}
cout<<temp<<endl;
}
return 0;
}
]]>
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() |
stdin/stdout | 3s | 10240K | 717 | 196 | Standard |
Mr. Jojer is a very famous chemist. He is doing a research about behavior of a group of atoms. Atoms may have different energy and energy can be positive or negative or zero, e.g. 18 or -9. Absolute value of energy can not be more than 100. Any number of continuous atoms can form an atom-group. Energy of an atom-group is defined by the sum of energy of all the atoms in the group. All the atoms form an atom-community which is a line formed by all the atoms one by one. Energy of an atom-community is defined by the greatest energy of an atom-group that can be formed by atoms in the atom-community. The problem is, given an atom-community, to calculate its energy.
The input contains several test cases. Each test case consists of two lines describing an atom-community. The first line of each test case contains an integer N(N<=1000000), the number of atoms in the atom-community. The second line of each test case contains N integers, separated by spaces, each representing energy of an atom, given in the order according to the atom-community. The last test case marks by N=-1, which you should not proceed.
For each test case(atom-community description), print a single line containing the energy.
5 8 0 6 4 -1 -1
18
鐞嗚В棰樻剰寰堥噸瑕侊紝棰樼洰鐨勬剰鎬濇槸璇?鍦╩涓腑閫?nbsp; 榪炵畫鐨刵涓猘tom鑳介噺鍊?浣垮叾鏈澶с?br>紼嬪簭涓璼umtemp錛宻um銆俿umtemp紜畾鐨勬槸鍏跺乏杈圭晫錛宻um紜畾鍏跺彸杈圭晫銆?br>sumtemp紜畾宸﹁竟鍓峮涓暟涔嬪拰涓鴻礋鐨勬渶澶х殑n錛屼笖絎琻涓暟鏄劇劧涓鴻礋錛岀劧鍚庝粠n+1寮濮嬮夋暟銆?br>sum紜畾浜嗗彸杈圭晫浣垮叾鏈澶с?br>姹傚拰鏈澶ч兘鍙互鐢ㄨ繖縐嶆濊礬錛侊紒錛侊紒錛侊紒錛侊紒錛侊紒
涓句緥
1錛?錛?4錛?錛?錛?2錛?錛?錛?6錛?錛?3錛?錛?0
璇風湅sumt1=1,sum=1
sumt2=2;sum=1+2=3;
sum3=sum2-4=-1;鍒檚umtemp=0;sum涓嶅彉銆?br>sum4=sumtemp+4;sum<sum4,sum=4;
sum5=sumtemp+2錛泂um<sum5,sum=6
鎬諱箣sum鍙湁鍦╯um<sumtemp鏃舵墠淇敼銆俿umtemp<0鍒欐竻0.
涓緇磀p.
#include"stdio.h"
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int n;
int a;
while(scanf("%ld",&n),n!=-1)
{
long long sum=-0x7fffffff,sumtemp=-0x7fffffff;
for(long i=0;i<n;i++)
{
scanf("%d",&a);
if(sumtemp>0)
sumtemp+=a;
else
sumtemp=a;
if(sumtemp>sum)
sum=sumtemp;
}
printf("%lld\n",sum);
}
return 0;
}
int dfs(int sum,int flag,int n,int time)//´Ótime¿ªÊ¼ËÑË÷
{
if(sum==0)return 1;
if(time<n)
{
for(int k=time;k<n;k++)
{
if(mark[k]<0&&sum-length[k]>=0)
{
mark[k]=flag;
if(dfs(sum-length[k],flag,n,k+1))
return 1;
else
mark[k]=-1;
}
}
}
return 0;
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num,n;
cin>>num;
while(num--)
{
int sum=0;
cin>>n;
for(int k=0;k<n;k++)
{
cin>>length[k];
mark[k]=-1;
sum+=length[k];
}
sort(length,length+n,cmp);
if(sum%4!=0||length[0]>sum/4)
cout<<"no"<<endl;
else
{
sum/=4;
if(dfs(sum,1,n,0))
{
if(dfs(sum,2,n,0))
{
if(dfs(sum,3,n,0))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
else
cout<<"no"<<endl;
}
else
cout<<"no"<<endl;
}
}
//system("PAUSE");
return 0;
}
姝g‘鐨勮В娉曞簲褰撴槸錛屽湪涓璧鋒悳绱€?br>涓嬮潰鏄竴孌佃秴鏃剁殑浠g爜
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
int length[21];
int mark[21];
int flag=0;
int target;
bool cmp(int x,int y)
{
return x>y;
}
int func(int i)
{
while(mark[i]>0)
i++;
return i;
}
void dfs(int sum,int n,int time,int level)//浠庡簭鍙蜂負time鐨勫紑濮嬫悳绱?
{
if(flag)return ;
if(sum==0&&level==3)
{
flag=1;
return;
}
if(sum==0)
{
level++;
dfs(target,n,func(0),level);
}
else if(time<n)
{
for(int k=time;k<n;k++)
{
if(k>1)
{
if(length[k]==length[k-1]&&!mark[k-1])
continue;//鏄劇劧
}
if(sum<length[n-1]) continue;//鏄劇劧
if(mark[k]<0&&sum-length[k]>=0)
{
mark[k]=1;
dfs(sum-length[k],n,func(k+1),level);
mark[k]=-1;
}
}
}
}
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num,n;
cin>>num;
while(num--)
{
int sum=0;
cin>>n;
for(int k=0;k<n;k++)
{
cin>>length[k];
mark[k]=-1;
sum+=length[k];
}
sort(length,length+n,cmp);
if(sum%4!=0||length[0]>sum/4)
cout<<"no"<<endl;
else
{
sum/=4;
target=sum;
dfs(sum,n,0,0);
if(flag==0)
cout<<"no"<<endl;
else
cout<<"yes"<<endl;
}
}
//system("PAUSE");
return 0;
}
涓婇潰瓚呮椂灝辮秴鏃跺師鍥犳槸鍑忔灊涓嶅交搴曘俵evel鏍囪涔熶笉澶ソ
鍐沖畾鍙﹀鐐夌伓,鍏堢湅鐪嬪埆浜虹殑浠g爜
#include<iostream>
#include<algorithm>
using namespace std;
int s[21];
int v[21];
int len;
int m;
int dfs(int cur,int num,int beg,int fin)
{
int solve(int );
if(num==1)
return 1;
if(cur==len)
{
return solve(num-1);
}
for(int i=beg;i>=fin;i--)
{
if(!v[i]&&cur+s[i]<=len)
{
v[i]=1;
if(dfs(cur+s[i],num,i-1,fin))
return 1;
v[i]=0;
}
}
return 0;
}
int solve(int edge_num)
{
int i;
for(i=m;i>=1;i--)
if(!v[i])
{
v[i]=1;
if(dfs( s[i],edge_num,i-1,1))
return 1;
v[i]=0;
}
return 0;
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>m;
int i;
int sum=0;
for(i=1;i<=m;i++)
{
cin>>s[i];
sum+=s[i];
v[i]=0;
}
len=sum/4;
if(4*len!=sum)
{
cout<<"no"<<endl;
continue;
}
sort(s+1,s+m+1);
if(s[m]>len)
{
cout<<"no"<<endl;
continue;
}
if(solve(4))
cout<<"yes"<<endl;
else
cout<<"no"<<endl;
}
return 0;
}//鍓灊榪樺彲浠ュ仛寰楁洿濂斤紒錛侊紒
浜哄鐨剆olve鍐欏緱濂斤紒錛侊紝緇撳悎dfs.
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() |
stdin/stdout | 15s | 8192K | 479 | 114 | Standard |
The first line of input contains n the number of people at the picnic. n lines follow. The first line gives the weight of person 1; the second the weight of person 2; and so on. Each weight is an integer between 1 and 450. There are at most 100 people at the picnic.
The input may contain several test cases.
Your output will be a single line containing 2 numbers: the total weight of the people on one team, and the total weight of the people on the other team. If these numbers differ, give the lesser first.
3 100 90 200
190 200
鍒╃敤dp鎬濇兂 錛宯涓哄伓鏁版椂姹傚嚭s(n,n/2)錛宯涓哄鏁版椂 涔熸槸s(2n,n/2)錛屽拰sum/2鏈鎺ヨ繎鐨勯偅涓傞潪甯哥粡鍏哥殑鎬濊礬銆?br>S(k, 1) = {A[i] | 1<= i <= k}
S(k, k) = {A[1]+A[2]+…+A[k]}
S(k, i) = S(k-1, i) U {A[k] + x | x灞炰簬S(k-1, i-1) }
//涓涓嬩唬鐮佸彧鑳界敤浜巗um鐗瑰埆灝忕殑鎯呭喌錛屽惁鍒欎細瓚呮椂錛侊紒錛侊紒錛侊紒錛侊紒錛侊紒錛?br>#include<iostream>
#include<cstdlib>
#define MAX 101
#define min(a,b) ((a)<(b) ? (a) : (b))
using namespace std;
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
int num;
int a[MAX],i,j,k,l,m,NUM;
bool s[MAX][2500];
while(cin>>num)
{
int sum=0;
for(i=1;i<=num;i++)
{
cin>>a[i];
sum+=a[i];
}
if(num%2==0)NUM=num/2;
else NUM=num/2+1;
for(i=0;i<=num;i++)
for(j=0;j<=sum/2;j++)
s[i][j]=false;//琛ㄧず鍙杋涓墿鍝佽兘鍚﹁揪鍒伴噸閲忔槸j.
s[0][0]=true;
for(k=1;k<=num;k++)//蹇呴』鍦ㄦ渶澶栧眰錛屽厓绱犱笉鑳介噸澶?
for(j=min(k,NUM);j>=1; j--)//閫掑噺鐨勭粨鏋滄槸浣垮緱涓嶄細鍑虹幇鍦ㄥ悓涓灞傛鐨勪簰涓哄洜鏋?銆併併併併併併併併併併佸閥濡欑殑瀹炵幇浜嗚鏈笂鐨勫簭鍋跺鐢熸垚娉曘?br> for(i=a[k];i<=sum/2;i++)
{
if(s[j-1][i-a[k]])
s[j][i]=true;
}
for(i=sum/2; i>=0; i--) {
if(s[NUM][i]) {
cout <<i<<" "<<sum-i<<endl;
break;
}
}
}
//system("PAUSE");
return 0;
}
涓嬩竴嬈″疄鐜頒竴涓簭鍋剁敓鎴愭硶銆?/p>
#include <iostream>
#include <functional>
using namespace std;
int a[101];
bool b[101][45002];
int main(){
// freopen("s.txt","r",stdin);
// freopen("key.txt","w",stdout);
int N,M,i,j,k;
while(scanf("%d",&N)!=EOF){
memset(b,0,sizeof(b));
a[0]=M=0;
for(i=1;i<=N;i++){
scanf("%d",a+i);
M+=a[i];
}
b[0][0]=1;
for(k=1;k<=N;k++){
for(i=1;i<=N/2;i++){
for(j=M/2;j>=0;j--){
if(b[i-1][j]){
b[i][j+a[k]]=1;
}
}
}
}
for(i=M/2,j=M/2+1;i>=0;i--,j++){
if(b[N/2][i]){
printf("%d %d\n",i,M-i);
break;
}
if(b[N/2][j]){
printf("%d %d\n",M-j,j);
break;
}
}
}
return 0;
}
Print a new line after each test case.
2 1 1 2 0 0 5 3 1 2 3 5 3 1 2 4 4 5 0 0 5 5 5 1 5 2 5 3 5 4 4 1 4 2 0 0 0
Case 1: The longest path from 1 has length 1, finishing at 2. Case 2: The longest path from 3 has length 4, finishing at 5. Case 3: The longest path from 5 has length 2, finishing at 1.
鎻愪氦浜?0嬈★紝AC 3嬈★紝瓚呮椂4嬈★紝wa 3嬈°?/pre>寰堟棤璇?/pre>搴旇鏄姩鎬佽鍒掓渶濂斤紝浣嗘槸鎴戜笉鏄緢鐔燂紝鐢ㄤ簡鎼滅儲銆備互涓嬫槸瓚呮椂鐨勪唬鐮?/pre>#include<iostream> #include<cstdlib> using namespace std; int path[101][101]; int mark[101]; int len[101];// int end[101]; int dfs(int start,int num)//榪斿洖浠庡綋鍓嶇偣鍑哄彂鐨勬渶澶ч暱搴? { if(mark[start]==1)return len[start]; mark[start]=1; end[start]=start; for(int i=1;i<=num;i++) { if(path[start][i]) { if(len[start]<dfs(i,num)+1) { len[start]=len[i]+1; end[start]=end[i]; } else if(len[start]==len[i]+1&&end[start]>end[i]) end[start]=end[i]; } } mark[start]=0;//榪欏彞鍧氬喅涓嶉渶瑕? return len[start]; } int main() { // freopen("s.txt","r",stdin); //freopen("key.txt","w",stdout); int j,k,turn=0; int start,num; while(cin>>num) { turn++; if(num==0)break; memset(path,0,sizeof(path)); memset(mark,0,sizeof(mark)); memset(len,0,sizeof(len)); memset(end,0,sizeof(end)); cin>>start; while(cin>>j>>k) { if(j==0)break; path[j][k]=1; } cout<<"Case "<<turn<<": The longest path from "<<start<<" has length "<<dfs(start,num)<<", finishing at "<<end[start]<<"."<<endl<<endl; } //system("PAUSE"); return 0; }
浠ヤ笅鏄痑c鐨勪唬鐮?/pre>#include<iostream> #include<cstdlib> using namespace std; int path[102][102]; int mark[102], len[102],end[102]; int dfs(int start,int num)//榪斿洖浠庡綋鍓嶇偣鍑哄彂鐨勬渶澶ч暱搴? { if(mark[start]==1)return len[start]; mark[start]=1; end[start]=start; len[start]=0; int i,t; for( i=1;i<=num;i++) { if(path[start][i]) { t=dfs(i,num)+1; if(t>len[start]) { len[start]=t; end[start]=end[i]; } else if(len[start]==t) { if(end[start]>end[i]) end[start]=end[i]; } } } return len[start]; } int main() { //freopen("s.txt","r",stdin); //freopen("key.txt","w",stdout); int j,k,turn=0; int start,num; while(cin>>num,num) { turn++; memset(path,0,sizeof(path)); memset(mark,0,sizeof(mark)); memset(len,0,sizeof(len)); memset(end,0,sizeof(end)); cin>>start; while(cin>>j>>k,j||k) { path[j][k]=1; } dfs(start,num); cout<<"Case "<<turn<<": The longest path from "<<start<<" has length "<<len[start]<<", finishing at "<<end[start]<<"."<<endl<<endl; } //system("PAUSE"); return 0; }涓嶅Θ鎵ц涓涓?/pre>5 3 1 2 3 5 3 1 2 4 4 5 0 0 鍏堟槸len[3]=0;end[3]=3;flag[3]=1;
鍐嶆墽琛宼=dfs(1)+1,杞叆dfs錛?錛夛紱len[1]=0;end[1]=1;flag[1]=1;鍐嶆墽琛?span style="COLOR: #0000ff">t=dfs(2)+1;杞叆dfs(2),len[2]=0;end[2]=2;flag[2]=1;鍐嶆墽琛宼=dfs(4)+1
杞叆dfs(4),len[4]=0;end[4]=4;flag[4]=1;鍐嶈漿鍏?span style="COLOR: red">t=dfs錛?)+1;
杞叆dfs(5),len[5]=0;end[5]=5;flag[5]=1;return(len[5]=0);鍒檛=1;t>len[4];len[4]=1;end[4]=end[5]=5;鍐嶇湅4娌′簡鍏朵粬鐩擱偦鍏冪礌銆俤fs(4)=return(len[4])=1;t=dfs(4)+1=2;len[2]=t=2;end[2]=end[4]=5;鍐嶇湅2娌′簡鍏朵粬鐩擱偦鍏冪礌錛宒fs(2)=return(len(2)=2;
鍐嶇湅t=dfs(2)+1=3;len[1]=t=3;end[1]=en[2]=5;鍐嶇湅1鏈夋病鏈夊叾浠栫浉閭誨厓绱狅紝dfs(1)=return(len(1)=3
鍐嶆墽琛宼=dfs(1)+1,len[3]=4;end[3]=end[1]=5;鍐嶇湅3鏈夋病鏈夊叾浠栫浉閭誨厓绱狅紝鏈塪fs(5),宸茬粡閬嶅巻鍒頒簡錛屾墍浠fs錛?錛塺eturn len銆?銆戙?br>娌℃湁褰卞搷銆?br>鍋囪鏀逛負
5 3 5 2 3 5 3 1 2 4 4 1 0 0 鎵ц鏃朵細璧?->1>榪欐椂鐨?緇撶偣len[1]宸茬粡姹傜殑 3>5>2>4>1len[1]宸茬煡浜?/span>
]]>
//system("PAUSE");
return 0;
}
Your teacher has given you some problems to solve. You must first solve problem 0. After solving each problem i, you must either move on to problem i+1 or skip ahead to problem i+2. You are not allowed to skip more than one problem. For example, {0, 2, 3, 5} is a valid order, but {0, 2, 4, 7} is not because the skip from 4 to 7 is too long.
You are given N integers, where the ith integer indicates how much you like problem i. The teacher will let you stop solving problems once the range of pleasantness you've encountered reaches a certain threshold. Specifically, you may stop once the difference between the maximum and minimum pleasantness of the problems you've solved is greater than or equal to a integer K. If this never happens, you must solve all the problems.
Input
The input contains several test cases. For each case it contains two positive integer N, K (1<=N<=50, 1<=K<=1000), then N integers follow in the next line, the ith integer ( >=1 and <= 1000 ) indicates how much you like problem i.
Output
For each test case, output the minimum number of problems you must solve to satisfy the teacher's requirements.
Sample Input
3 2
1 2 3
5 4
1 2 3 4 5Sample Output
2
3寰堢浣嗘槸寰堝疄鐢紒#include<stdio.h> int a[51]; int main() { int n,k; while(scanf("%d%d",&n,&k)!=EOF) { int i,j; for(i=0;i<n;i++) scanf("%d",a+i); int min=0xfffffff; int flag=0; for(i=1;i<n;i++) for(j=0;j<i;j++) { if(a[i]-a[j]>=k||a[j]-a[i]>=k) { flag=1; int step; if(j==0) { step=(i+1)/2+1; if(min>step) min=step; } else { step=(i-j+1)/2+(j+1)/2+1; if(min>step) min=step; } } } if(flag) printf("%d\n",min); else printf("%d\n",n); } return 0; }
灝嗕箻娉曡漿鍖栦負log鍔犳硶錛侊紒錛?/pre>
]]>
One curious child has a set of N little bricks. From these bricks he builds different staircases. Staircase consists of steps of different sizes in a strictly descending order. It is not allowed for staircase to have steps equal sizes. Every staircase consists of at least two steps and each step contains at least one brick. Picture gives examples of staircase for N=11 and N=5:
Your task is to write a program that reads from input numbers N and writes to output numbers Q - amount of different staircases that can be built from exactly N bricks.
3 5 0
1 2鏂規硶1錛屽姩鎬佽鍒?br>
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
freopen("s.txt","r",stdin);
freopen("key.txt","w",stdout);
string s;
int g=0;
while(getline(cin,s))
{
if (g) cout<<endl;
g=1;
int sum=0;
map<string,int> a;
while(s!="")
{
a[s]++;
sum++;
getline(cin,s);
}
cout.flags(ios::fixed);
cout.precision(4);
for (map<string,int>::iterator p=a.begin();p!=a.end();p++)
cout<<p->first<<" "<<100.0*p->second/sum<<endl;
}
return 0;
}
Status | In/Out | TIME Limit | MEMORY Limit | Submit Times | Solved Users | JUDGE TYPE |
---|---|---|---|---|---|---|
![]() |
stdin/stdout | 3s | 8192K | 424 | 182 | Standard |
Given a specifie d total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t = 4, n = 6, and the list is [4, 3, 2, 2, 1, 1], then there are four different sums that equal 4: 4, 3+1, 2+2, and 2+1+1. (A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
The input file will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers . If n = 0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12 (inclusive), and
will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
For each test case, first output a line containing `Sums of ', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line `NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distinct; the same sum cannot appear twice.
4 6 4 3 2 2 1 1 5 3 2 1 1 400 12 50 50 50 50 50 50 25 25 25 25 25 25 0 0
Sums of 4: 4 3+1 2+2 2+1+1 Sums of 5: NONE Sums of 400: 50+50+50+50+50+50+25+25+25+25 50+50+50+50+50+25+25+25+25+25+25
闅劇偣錛?錛屾湁搴忛掑噺錛岃繖涓ソ澶勭悊錛屽彧闇鍦ㄥ悜鍓嶆帹鏃禿fs(i)for(int j=i+1...)鍗沖彲