UVa668 - Parliament(贪心)

简介: UVa668 - Parliament(贪心)
importjava.io.BufferedReader;
importjava.io.InputStreamReader;
importjava.io.FileReader;
importjava.io.PrintWriter;
importjava.io.OutputStreamWriter;
importjava.io.StreamTokenizer;
importjava.io.IOException;
classMain{
publicstaticfinalbooleanDEBUG=false;
publicStreamTokenizertokenizer;
publicPrintWritercout;
publicvoidinit() throwsIOException    {
BufferedReadercin;
if (DEBUG) {
cin=newBufferedReader(newFileReader("d:\\OJ\\uva_in.txt"));
        } else {
cin=newBufferedReader(newInputStreamReader(System.in));
        }
tokenizer=newStreamTokenizer(cin);
cout=newPrintWriter(newOutputStreamWriter(System.out));
    }
publicintnext() throwsIOException    {
tokenizer.nextToken();
if (tokenizer.ttype==StreamTokenizer.TT_NUMBER) {
return (int)tokenizer.nval;
        }
return-1;
    }
privateintsum(intx)
    {
returnx* (x+1) /2-1;
    }
publicvoidsolve(intcas, intn)
    {
int[] num=newint[110];
for (inti=2; i<=n; i++) {
if (sum(i+1) >n) {
inttmp=sum(i);
intaverage= (n-tmp) / (i-1);
intremainder= (n-tmp) % (i-1);
for (intj=2; j<=i-remainder; j++) num[j] =j+average;
for (intj=i-remainder+1; j<=i; j++) num[j] =j+average+1;
for (intj=2; j<=i; j++) {
if (j!=2) cout.print(" ");
cout.print(num[j]);
                }
cout.println();
if (cas!=0) cout.println();
cout.flush();
return;
            }
        }
    }
publicstaticvoidmain(String[] args) throwsIOException    {
Mainsolver=newMain();
solver.init();
intt=solver.next();
while (t-->0) {
intn=solver.next();
solver.solve(t, n);
        }
    }
}
目录
相关文章
|
6月前
|
自然语言处理 算法
KMP算法(A + B for you again—HDU - 1867 )
KMP算法(A + B for you again—HDU - 1867 )
|
图形学 C++
ZOJ1117 POJ1521 HDU1053 Huffman编码
Huffman编码的思想就是贪心,我们这里使用stl里的优先队列,priority_queue使用堆进行优化,虽然自己也可以写一个堆,但我感觉对于这道题有点主次不分了,再次感觉到stl确实是一个很强大的东西。
53 0
UVa11420 - Chest of Drawers(动态规划)
UVa11420 - Chest of Drawers(动态规划)
47 0
UVa11710 - Expensive subway(最小生成树)
UVa11710 - Expensive subway(最小生成树)
41 0
HDU 4283 You Are the One(动态规划)
HDU 4283 You Are the One(动态规划)
76 0
HDU 4283 You Are the One(动态规划)
|
人工智能 BI 存储
Uva 10339 - Watching Watches【数论,暴力】
题目链接:10339 - Watching Watches 题意:两个时钟,一个每天慢a秒,一个每天慢b秒,问两钟重新相遇的时刻 1圈有12 * 60 * 60秒,然后1圈 / abs(a - b),就可以求出多少天会相遇,然后就能求出A钟一共慢了多少秒,进而可以求出该时刻的时和分! 下面给...
1164 0
|
并行计算 算法 Java
HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 53806    Accepted Submission(s): 20092 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路。
1086 0
ZOJ 1403&&HDU 1015 Safecracker【暴力】
Safecracker Time Limit: 2 Seconds      Memory Limit: 65536 KB === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library.
1213 0